预测建模背景

Tidyverse 的数据建模

Albert Y. Kim

Assistant Professor of Statistical and Data Sciences

预测建模示例

华盛顿州金县(西雅图附近)的房价数据集(见 Kaggle.com)。

问题:能否基于房屋特征预测其售价?

变量:

  • y:房屋销售 price,单位美元
  • \vec{x}:sqft_livingconditionbedroomsyr_builtwaterfront 等特征
Tidyverse 的数据建模

预测建模示例

来自 moderndive 包(见 ModernDive):

library(dplyr)
library(moderndive)
glimpse(house_prices)
Observations: 21,613
Variables: 21
$ id            <chr> "7129300520", "6414100192"...
$ date          <dttm> 2014-10-13, 2014-12-09, 2015...
$ price         <dbl> 221900, 538000, 180000, 604000...
...
Tidyverse 的数据建模

探索性数据分析

library(ggplot2)
ggplot(house_prices, aes(x = price)) +
  geom_histogram() + 
  labs(x = "house price", y = "count")
Tidyverse 的数据建模

因变量直方图

Tidyverse 的数据建模

Gapminder 数据

Tidyverse 的数据建模

x 轴取 log10 重标

Tidyverse 的数据建模

log10 变换

# log10() transform price and size
house_prices <- house_prices %>%
  mutate(log10_price = log10(price)) %>% 
  select(price, log10_price)
# A tibble: 21,613 x 2
     price log10_price
     <dbl>       <dbl>
 1  221900        5.35
 2  538000        5.73
 3  180000        5.26
 4  604000        5.78
 5  510000        5.71
 6 1225000        6.09
Tidyverse 的数据建模

新因变量的直方图

# Histogram of original outcome variable
ggplot(house_prices, aes(x = price)) +
  geom_histogram() + 
  labs(x = "house price", y = "count")
# Histogram of new, log10-transformed outcome variable
ggplot(house_prices, aes(x = log10_price)) +
  geom_histogram() + 
  labs(x = "log10 house price", y = "count")
Tidyverse 的数据建模

log10 变换前后对比

Tidyverse 的数据建模

让我们练习吧!

Tidyverse 的数据建模

Preparing Video For Download...