用于预测的建模问题

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 的数据建模

解释与预测的区别

建模目标的关键差异:

  1. 解释:关注 $\hat{f}()$ 的形式,尤其量化 $y$ 与 $\vec{x}$ 关系的数值
  2. 预测:不太在意 $\hat{f}()$ 的形式,只在意其基于 $\vec{x}$ 对 $y$ 给出"好"的预测 $\hat{y}$
Tidyverse 的数据建模

房屋状况

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)

# 对因变量取 log10 变换
house_prices <- house_prices %>%
  mutate(log10_price = log10(price))

# 箱线图 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 的数据建模

探索性数据汇总

# 预测状况为 4 的新房价格(美元)
10^(5.65)
446683.6
Tidyverse 的数据建模

¡Vamos a practicar!

Tidyverse 的数据建模

Preparing Video For Download...