用年龄解释教学评分

Tidyverse 的数据建模

Albert Y. Kim

Assistant Professor of Statistical and Data Sciences

复习:探索性数据可视化

Tidyverse 的数据建模

回归线

# 创建散点图的代码
ggplot(evals, aes(x = age, y = score)) +
  geom_point() + 
  labs(x = "age", y = "score", 
       title = "教学评分随年龄变化")

# 添加"最佳拟合"直线 ggplot(evals, aes(x = age, y = score)) + geom_point() + labs(x = "age", y = "score", title = "教学评分随年龄变化") + geom_smooth(method = "lm", se = FALSE)
Tidyverse 的数据建模

回归线

Tidyverse 的数据建模

复习:一般建模

  • 真实:假定模型为 $y = f(\vec{x}) + \epsilon$
  • 目标:给定 $y$ 和 $\vec{x}$,拟合 $\hat{f}(\vec{x})$ 以近似 $f(\vec{x})$,其中 $\hat{y} = \hat{f}(\vec{x})$ 为观测值 $y$ 的拟合/预测值
Tidyverse 的数据建模

基础线性回归建模

  • 真实:
    • 假定 $f(x) = \beta_0 + \beta_1 \cdot x$
    • 观测值 $y = f(x) + \epsilon = \beta_0 + \beta_1 \cdot x + \epsilon$
  • 拟合:
    • 假定 $\hat{f}(x) = \hat{\beta}_0 + \hat{\beta}_1 \cdot x$
    • 拟合/预测值 $\hat{y} = \hat{f}(x) = \hat{\beta}_0 + \hat{\beta}_1 \cdot x$
Tidyverse 的数据建模

回到回归线

拟合的蓝色回归线方程:$\hat{y} = \hat{f}(\vec{x}) = \hat{\beta}_0 + \hat{\beta}_1 \cdot x$

Tidyverse 的数据建模

计算回归线的斜率与截距

使用公式形式 y ~ x

# 使用 y ~ x 形式拟合回归模型
model_score_1 <- lm(score ~ age, data = evals)

# 输出内容 model_score_1
Call:
lm(formula = score ~ age, data = evals)

Coefficients:
(Intercept)          age  
   4.461932    -0.005938
Tidyverse 的数据建模

计算回归线的斜率与截距

使用公式形式 y ~ x,相当于 $\hat{y}= \hat{f}(\vec{x})$

# 使用 y ~ x 形式拟合回归模型
model_score_1 <- lm(score ~ age, data = evals)

# 用包装函数输出回归表:
get_regression_table(model_score_1)
# A tibble: 2 x 7
  term      estimate std_error statistic p_value...
  <chr>        <dbl>     <dbl>     <dbl>   <dbl>... 
1 intercept    4.46      0.127     35.2    0...
2 age         -0.006     0.003     -2.31   0.021...
Tidyverse 的数据建模

让我们练习吧!

Tidyverse 的数据建模

Preparing Video For Download...