开始使用免费开始使用

对取对数变换的金额型输出建模

在本练习中,您将练习对取对数变换的金额型输出进行建模,然后把"对数金额"的预测值转换回金额单位。已加载的数据记录了受试者在 2005 年的收入(Income2005),以及这些受试者在 1981 年参加的多项能力测验成绩:

  • Arith
  • Word
  • Parag
  • Math
  • AFQT(Armed Forces Qualifying Test 的百分位)

数据已预先划分为训练集和测试集(分别为 income_trainincome_test),并已加载。您将基于这些输入构建 log(income) 的模型,然后再将 log(income) 转回收入。

本练习是课程的一部分

R 中的监督学习:回归

查看课程

练习说明

  • 对训练集中的收入调用 summary()income_train$Income2005,查看其汇总统计。
  • 写出一个公式,将 log(Income2005) 表达为五个测试成绩的函数,保存为变量 fmla.log。打印它。
  • income_train 数据上拟合 log(Income2005) 的线性模型:model.log
  • 使用 model.logincome_test 数据集上进行收入预测。将结果放入列 logpred
    • 检查 logpredsummary(),体会其数值量级与 Income2005 很不一样。
  • 逆转对数变换,将预测值转换回"金额单位":exp(income_test$logpred)
    • 检查 pred.incomesummary(),可以看到其量级现在与 Income2005 相近。
  • 填空绘制测试集中"预测收入 vs 实际收入"的散点图。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Examine Income2005 in the training set
___

# Write the formula for log income as a function of the tests and print it
(fmla.log <- ___)

# Fit the linear model
model.log <-  ___

# Make predictions on income_test
income_test$logpred <- ___
summary(income_test$logpred)

# Convert the predictions to monetary units
income_test$pred.income <- ___
summary(income_test$pred.income)

#  Plot predicted income (x axis) vs income
ggplot(___, aes(x = ___, y = ___)) + 
  geom_point() + 
  geom_abline(color = "blue")
编辑并运行代码