我如何创建多个数据模型,然后选择经评估最合适的模型?
How can I create multiple data models, then pick the model(s) that are evaluated to be the best fitting?
我在 R 中设置了一个训练测试函数,它获取一组数据,排除其中的一部分(以防止模型过度拟合数据),然后在大约一半的数据上训练一个线性模型在另一半上测试模型之前的剩余数据。
我应该注意到数据集是基于 PCA 分数的,这就是为什么线性模型设置为包括七个 PCA 组件。
splitprob = 0.7
trainindex = createDataPartition(scores$y, p=splitprob, list=F)
trainingset = scores[trainindex,]
testset = scores[-trainindex,]
model = glm(y ~ PC1 + PC2 + PC3 + PC4 + PC5 + PC6 + PC7, data=trainingset)
summary(model)
prediction = predict.lm(model, trainingset, se.fit=T)
现在,我想做的是运行 多次执行此脚本,生成多个模型,然后选择一个或多个模型用于进行未来预测。虽然我已经将函数设置为 运行 一定次数,但我不知道如何设置它以便我可以将不同的模型相互比较(可能通过使用 AIC),也不是我确定我应该如何捕获模型的参数以便将它们导出到文本文件或 .csv 文件。
我尝试实现 glmulti 包,但由于在使用 Java、rJava 和 Mac OsSX 时出现各种问题,我在获取它时遇到了很多问题正确安装。谁能推荐我解决这个问题的另一种方法?
我每次都更新(修改)答案以包含不同的预测变量,
# build data
set.seed(1)
y = runif(100)
x = matrix(runif(1000), nrow = 100, ncol = 10)
重复训练-测试分裂的函数,returns所有迭代的汇总统计(它需要数据、迭代次数、公式和训练测试中的分裂百分比),
glm_train_test = function(y, x, num_iters, Formula, split_percentage = 0.5) {
list_models = list()
aic_models = matrix(0, nrow = num_iters, ncol = 2)
rmse_models = matrix(0, nrow = num_iters, ncol = 2)
for (i in 1:num_iters) { # train - test - splits
spl = sample(nrow(x), round(nrow(x) * split_percentage), replace = F)
train = data.frame(y = y[spl], x[spl, ])
test = data.frame(y = y[-spl], x[-spl, ])
tmp_formula = as.formula(Formula)
fit = glm(formula = tmp_formula, data = train)
# pred_train = predict(fit, newdata = train)
pred_test = predict(fit, newdata = test)
aic_models[i, ] = c(i, summary(fit)$aic)
rmse_models[i, ] = c(i, Metrics::rmse(y[-spl], pred_test))
}
# convert the resulted aic-rmse matrices to data frames
sort_aic = as.data.frame(aic_models)
colnames(sort_aic) = c('iteration', 'aic_value')
sort_rmse = as.data.frame(rmse_models)
colnames(sort_rmse) = c('iteration', 'rmse_value')
tmp_aic = c(summary(sort_aic[, 2]), sd(sort_aic[, 2]))
names(tmp_aic) = c(names(summary(sort_aic[, 2])), 'std.dev')
tmp_rmse = c(summary(sort_rmse[, 2]), sd(sort_rmse[, 2]))
names(tmp_rmse) = c(names(summary(sort_rmse[, 2])), 'std.dev')
return(list(summary_aic = tmp_aic, summary_rmse = tmp_rmse))
}
第一个模型仅包含三个预测变量,
first_mod = glm_train_test(y, x, num_iters = 100, Formula = "y ~ X1 + X2 + X3", split_percentage = 0.5)
示例输出
first_mod
$summary_aic
Min. 1st Qu. Median Mean 3rd Qu. Max. std.dev
-6.193321 10.527612 13.317441 13.320968 17.019571 26.792904 5.798970
$summary_rmse
Min. 1st Qu. Median Mean 3rd Qu. Max. std.dev
0.23862643 0.26628405 0.27568030 0.27713722 0.28616462 0.33223873 0.01730974
第二个模型也包括交互,
second_model = glm_train_test(y, x, num_iters = 100, Formula = "y ~ X1 * X4 + X2 + X3", split_percentage = 0.5)
第二个模型的示例输出,
second_model
$summary_aic
Min. 1st Qu. Median Mean 3rd Qu. Max. std.dev
-0.04232767 13.37572489 16.92720680 16.81206625 20.79921756 29.67830243 5.96477080
$summary_rmse
Min. 1st Qu. Median Mean 3rd Qu. Max. std.dev
0.23912727 0.27026791 0.28117878 0.28177088 0.29526944 0.32674985 0.01819308
您可以使用适合您数据的评估指标,也可以查看 another Whosebug question 中 'aic' 和 'bic' 之间的区别。
我在 R 中设置了一个训练测试函数,它获取一组数据,排除其中的一部分(以防止模型过度拟合数据),然后在大约一半的数据上训练一个线性模型在另一半上测试模型之前的剩余数据。
我应该注意到数据集是基于 PCA 分数的,这就是为什么线性模型设置为包括七个 PCA 组件。
splitprob = 0.7
trainindex = createDataPartition(scores$y, p=splitprob, list=F)
trainingset = scores[trainindex,]
testset = scores[-trainindex,]
model = glm(y ~ PC1 + PC2 + PC3 + PC4 + PC5 + PC6 + PC7, data=trainingset)
summary(model)
prediction = predict.lm(model, trainingset, se.fit=T)
现在,我想做的是运行 多次执行此脚本,生成多个模型,然后选择一个或多个模型用于进行未来预测。虽然我已经将函数设置为 运行 一定次数,但我不知道如何设置它以便我可以将不同的模型相互比较(可能通过使用 AIC),也不是我确定我应该如何捕获模型的参数以便将它们导出到文本文件或 .csv 文件。
我尝试实现 glmulti 包,但由于在使用 Java、rJava 和 Mac OsSX 时出现各种问题,我在获取它时遇到了很多问题正确安装。谁能推荐我解决这个问题的另一种方法?
我每次都更新(修改)答案以包含不同的预测变量,
# build data
set.seed(1)
y = runif(100)
x = matrix(runif(1000), nrow = 100, ncol = 10)
重复训练-测试分裂的函数,returns所有迭代的汇总统计(它需要数据、迭代次数、公式和训练测试中的分裂百分比),
glm_train_test = function(y, x, num_iters, Formula, split_percentage = 0.5) {
list_models = list()
aic_models = matrix(0, nrow = num_iters, ncol = 2)
rmse_models = matrix(0, nrow = num_iters, ncol = 2)
for (i in 1:num_iters) { # train - test - splits
spl = sample(nrow(x), round(nrow(x) * split_percentage), replace = F)
train = data.frame(y = y[spl], x[spl, ])
test = data.frame(y = y[-spl], x[-spl, ])
tmp_formula = as.formula(Formula)
fit = glm(formula = tmp_formula, data = train)
# pred_train = predict(fit, newdata = train)
pred_test = predict(fit, newdata = test)
aic_models[i, ] = c(i, summary(fit)$aic)
rmse_models[i, ] = c(i, Metrics::rmse(y[-spl], pred_test))
}
# convert the resulted aic-rmse matrices to data frames
sort_aic = as.data.frame(aic_models)
colnames(sort_aic) = c('iteration', 'aic_value')
sort_rmse = as.data.frame(rmse_models)
colnames(sort_rmse) = c('iteration', 'rmse_value')
tmp_aic = c(summary(sort_aic[, 2]), sd(sort_aic[, 2]))
names(tmp_aic) = c(names(summary(sort_aic[, 2])), 'std.dev')
tmp_rmse = c(summary(sort_rmse[, 2]), sd(sort_rmse[, 2]))
names(tmp_rmse) = c(names(summary(sort_rmse[, 2])), 'std.dev')
return(list(summary_aic = tmp_aic, summary_rmse = tmp_rmse))
}
第一个模型仅包含三个预测变量,
first_mod = glm_train_test(y, x, num_iters = 100, Formula = "y ~ X1 + X2 + X3", split_percentage = 0.5)
示例输出
first_mod
$summary_aic
Min. 1st Qu. Median Mean 3rd Qu. Max. std.dev
-6.193321 10.527612 13.317441 13.320968 17.019571 26.792904 5.798970
$summary_rmse
Min. 1st Qu. Median Mean 3rd Qu. Max. std.dev
0.23862643 0.26628405 0.27568030 0.27713722 0.28616462 0.33223873 0.01730974
第二个模型也包括交互,
second_model = glm_train_test(y, x, num_iters = 100, Formula = "y ~ X1 * X4 + X2 + X3", split_percentage = 0.5)
第二个模型的示例输出,
second_model
$summary_aic
Min. 1st Qu. Median Mean 3rd Qu. Max. std.dev
-0.04232767 13.37572489 16.92720680 16.81206625 20.79921756 29.67830243 5.96477080
$summary_rmse
Min. 1st Qu. Median Mean 3rd Qu. Max. std.dev
0.23912727 0.27026791 0.28117878 0.28177088 0.29526944 0.32674985 0.01819308
您可以使用适合您数据的评估指标,也可以查看 another Whosebug question 中 'aic' 和 'bic' 之间的区别。