为回归模型扩展数据集;不确定我是否误解了什么

问题描述

我有一个因变量和三个预测变量,如下所示:

> dependent_variables
 [1] -10.011505  93.986351  -9.402929  -1.671040  59.668559  -5.986114  11.941819   4.251151   6.483279
[10]  33.708654  44.832912  44.081070  74.167239  30.905949 -20.249819  -3.680097  40.409498 -19.432913
[19]  24.479506  45.760639  45.138610 -13.292553  65.312611  -3.008800
> predictor_variable_1
 [1]   1.557656  10.989129  41.381571  35.160487  16.078885  28.495891   3.345284  34.652224  18.176361
[10]   6.820499  25.915409  18.651739  10.205417  51.977162  30.728334  38.891255  34.962601  49.262164
[19]  31.324149   9.474330  35.713268 -30.032500  29.123236   4.177988
> predictor_variable_2
 [1]  1.044840183 -2.728273973  0.696246575  3.592968037  0.731515982  3.227150685  4.041872146
 [8]  1.092794521  0.959059361  0.179954338 -1.354063927 -0.899470320 -1.866000000  0.418447489
[15]  4.605780822 -0.100456621 -0.906191781  2.443050228  0.032694064  4.983479452  1.415643836
[22]  1.202410959  3.204767123 -0.002520548
> predictor_variable_3
 [1]  0.014670369  0.146630751  0.006168234 -0.006832658  0.155514080  0.057939914  0.018203521
 [8]  0.037142233 -0.056192767  0.039360773  0.097904730  0.073426121  0.173520353  0.099133960
[15] -0.036419115 -0.016519986  0.076531355  0.000000000  0.049570467  0.135577115  0.082536511
[22]  0.003195234  0.130335738 -0.004214761

然后我还将这些放入如下回归模型中,R 平方值约为 87%。

lm_test_out = lm(dependent_variables ~ poly(predictor_variable_1,2) + poly(predictor_variable_2,2) + predictor_variable_3)

我不确定我是否正确理解了这一点,但我接下来要做的是将回归模型应用于小数据集并查看它的预测结果。我有一种感觉,在 R 中有一种更简单的方法。就像我可以尝试创建一个可能不考虑残差的函数,例如:

apply_lm_model <- function(input_vector)
{
 cept = lm_test_out$coefficients[1]
 coeff1 = lm_test_out$coefficients[2]
 coeff2 = lm_test_out$coefficients[3]
 coeff3 = lm_test_out$coefficients[4]
 coeff4 = lm_test_out$coefficients[5]
 coeff5 = lm_test_out$coefficients[6]

 fitting = cept + coeff1*(input_vector[1]) + coeff2*(input_vector[1]^2) + coeff3*(input_vector[2]) + coeff4*(input_vector[2^2]) + coeff5*input_vector[3]

return(fitting)
}

显然,即使我将此函数应用于原始数据集中的数据,也会遗漏残差。我希望能够预测模型提供的内容作为对任意数据点的答案。我试过使用“predict.lm”,但我想我不太了解语法。

那么,基本上,我如何使用新的任意数据点扩展我的数据集?有这样的解决方案吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)