用於預測的建模問題

在 Tidyverse 中進行資料建模

Albert Y. Kim

Assistant Professor of Statistical and Data Sciences

建模問題

考慮 $y = f(\vec{x}) + \epsilon$。

  1. $f()$ 與 $\epsilon$ 未知
  2. 已知/給定 $y$ 與 $\vec{x}$ 的 $n$ 筆觀測
  3. 目標:擬合模型 $\hat{f}()$,在忽略 $\epsilon$ 下近似 $f()$
  4. 換句話說:把「訊號」與「雜訊」分開
  5. 之後可產生「擬合/預測」值 $\hat{y} = \hat{f}(\vec{x})$
在 Tidyverse 中進行資料建模

解釋 vs. 預測的差異

建模目標的關鍵差異:

  1. 解釋:我們在意 $\hat{f}()$ 的形式,特別是量化 $y$ 與 $\vec{x}$ 關係的參數
  2. 預測:我們不太在意 $\hat{f}()$ 的形式,只在乎它能否根據 $\vec{x}$ 產生對 $y$ 的「良好」預測 $\hat{y}$
在 Tidyverse 中進行資料建模

屋況(condition)

house_prices %>% 
  select(log10_price, condition) %>% 
  glimpse()
Observations: 21,613
Variables: 2
$ log10_price <dbl> 5.346157, 5.730782, 5.255273...
$ condition   <fct> 3, 3, 3, 5, 3, 3, 3, 3, 3, 3, 3...
在 Tidyverse 中進行資料建模

探索式資料視覺化:盒鬚圖

library(ggplot2)
library(dplyr)
library(moderndive)

# Apply log10-transformation to outcome variable
house_prices <- house_prices %>%
  mutate(log10_price = log10(price))

# Boxplot ggplot(house_prices, aes(x = condition, y = log10_price)) + geom_boxplot() + labs(x = "house condition", y = "log10 price", title = "log10 house price over condition")
在 Tidyverse 中進行資料建模

探索式資料視覺化:盒鬚圖

在 Tidyverse 中進行資料建模

探索式資料摘要

house_prices %>% 
  group_by(condition) %>% 
  summarize(mean = mean(log10_price),
            sd = sd(log10_price), n = n())
# A tibble: 5 x 4
  condition  mean    sd     n
  <fct>     <dbl> <dbl> <int>
1 1          5.42 0.293    30
2 2          5.45 0.233   172
3 3          5.67 0.224 14031
4 4          5.65 0.228  5679
5 5          5.71 0.244  1701
在 Tidyverse 中進行資料建模

探索式資料摘要

# Prediction for new house with condition 4 in dollars
10^(5.65)
446683.6
在 Tidyverse 中進行資料建模

一起來練習吧!

在 Tidyverse 中進行資料建模

Preparing Video For Download...