R手册(Model Tools)–broom and modelr
broom:Convert statistical analysis objects into tidy format
tidy: 构建一个总结模型或检验整洁的统计信息data.frame。这包括回归中coefficients and p-values,聚类中的每个集群信息per-cluster或功能的每个测试信息per-test等
augment: 为已建模的原始数据添加列。这包括预测,残差和集群分配等(predictions, residuals, and cluster assignments)
glance: 构建模型的简洁一行摘要。这通常包含构建整个模型的计算值such as R^2, adjusted R^2, and residual standard error
modelr :辅助管道建模
分区和抽样
resample(data, idx)
抽取idx向量指定的观测值
resample_partition(data, p)
按p向量给定的值分区
bootstrap(data, n)
生成n个bootstrap副本
bootstrap(mtcars, 100)%>%
map( ~ lm(mpg ~ wt, data = .))%>%
map_df( broom::tidy)
交叉验证
crossv_mc(data, n, test = 0.2)
生成n组数据,交叉验证模型,test为测试集和训练集的比值
crossv_mc(mtcars, 100)%>%
map(cv2$train, ~ lm(mpg ~ wt, data = .))%>% #模型
map2_dbl(cv2$test, rmse)%>%
hist()
模型质量指标
rmse(model, data)
均方根误差
mae(model, data)
平均绝对误差
rsquare(model, data)
R^2预测的方差除以响应的方差
qae(model, data, probs)
误差的分位数
添加预测和残差
add_predictions(data, model)
向数据框添加预测
add_residuals(data, model)
向数据框添加残差
data_grid(data)
展开数据框,包含所有值的组合,对可视化模型有用