以預測為目的的建模背景

在 Tidyverse 中進行資料建模

Albert Y. Kim

Assistant Professor of Statistical and Data Sciences

預測建模範例

位於華盛頓州金郡(西雅圖附近)的房價資料集(可於 Kaggle.com 取得)。

問題:能否根據房屋特徵來預測成交價?

變數

  • $y$:房屋 price(美元)
  • $\vec{x}$:sqft_livingconditionbedroomsyr_builtwaterfront 等特徵
在 Tidyverse 中進行資料建模

預測建模範例

來自 ModernDivemoderndive 套件:

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...