对取对数变换的金额型输出建模
在本练习中,您将练习对取对数变换的金额型输出进行建模,然后把"对数金额"的预测值转换回金额单位。已加载的数据记录了受试者在 2005 年的收入(Income2005),以及这些受试者在 1981 年参加的多项能力测验成绩:
ArithWordParagMathAFQT(Armed Forces Qualifying Test 的百分位)
数据已预先划分为训练集和测试集(分别为 income_train 和 income_test),并已加载。您将基于这些输入构建 log(income) 的模型,然后再将 log(income) 转回收入。
本练习是课程的一部分
R 中的监督学习:回归
练习说明
- 对训练集中的收入调用
summary():income_train$Income2005,查看其汇总统计。 - 写出一个公式,将
log(Income2005)表达为五个测试成绩的函数,保存为变量fmla.log。打印它。 - 在
income_train数据上拟合log(Income2005)的线性模型:model.log。 - 使用
model.log在income_test数据集上进行收入预测。将结果放入列logpred。- 检查
logpred的summary(),体会其数值量级与Income2005很不一样。
- 检查
- 逆转对数变换,将预测值转换回"金额单位":
exp(income_test$logpred)。- 检查
pred.income的summary(),可以看到其量级现在与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")