R中使用“留一法"的线性回归预测 [英] Linear Regression prediction in R using Leave One out Approach

查看:170
本文介绍了R中使用“留一法"的线性回归预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个使用mtcar构建的线性回归模型,并希望使用这些模型为mtcars表的每一行生成预测.这些预测应作为mtcars数据帧的附加列(3个附加列)添加,并应使用留一法"在for循环中生成.此外,应该通过对模型1和模型2进行分组"来执行对模型1和模型2的预测.圆柱数使用模型3做出的预测应该在不进行任何分组的情况下完成.

I have 3 linear regression models built using the mtcars and would like to use those models to generate predictions for each rows of the mtcars tables. Those predictions should be added as additional columns (3 additional columns) of the mtcars dataframe and should be generated in a for loop using the leave one out approach. Furthermore predictions for model1 and model2 should be performed by "grouping" the cyl numbers whiles predictions made with the model 3 should be accomplished without doing any grouping.

到目前为止,我已经能够通过循环中的单个模型获得一些东西:

So far I've been able to somewhat get something with a single model in the loop:

model1 =lm(hp ~ mpg, data = mtcars)

model2 =lm(hp ~ mpg + hp, data = mtcars)

model3 =lm(hp ~ mpg + hp + wt, data = mtcars)

fitted_value <- NULL

for(i in 1:nrow(mtcars)){
  

  validation<-mtcars[i,]

  training<-mtcars[-i,]

  model1<-lm(mpg ~ hp, data = training)

  fitted_value[i] <-predict(model1, newdata = validation)

   }```


I would like to be able to generate all the model predictions by first putting all the models in a list or vector and attaching the result to the mtcars dataframe. Somthing lke thislike this:

```model1 =lm(hp ~ mpg, data = mtcars)

model2 =lm(hp ~ mpg + hp, data = mtcars)

model3 =lm(hp ~ mpg + hp + wt, data = mtcars)

models <- list(model1, model2, model3)

fitted_value <- NULL

for(i in 1:nrow(mtcars)){
  

  validation<-mtcars[i,]

  training<-mtcars[-i,]

  fitted_value[i] <-predict(models, newdata = validation)

   }```

Thank you for you help

推荐答案

您可以使用嵌套的 map 来为每行 i 拟合三个公式中的每一个.然后只需 bind_cols mtcars 来附加预测.

You can use a nested map to fit each of the three formulas for each row i. Then just bind_cols with mtcars to attach the predictions.

library(tidyverse)

frml_1 <- as.formula("hp ~ mpg")
frml_2 <- as.formula("hp ~ mpg + drat")
frml_3 <- as.formula("hp ~ mpg + drat + wt")
frmls <- list(frml_1 = frml_1, frml_2 = frml_2, frml_3 = frml_3)

mtcars %>%
  bind_cols(
    map(1:nrow(mtcars), function(i) {
      map_dfc(frmls, function(frml) {
        training <- mtcars[-i, ]
        fit <- lm(frml, data = training)
        
        validation <- mtcars[i, ]
        predict(fit, newdata = validation)
      })
    }) %>%
    bind_rows()
  )

                     mpg cyl  disp  hp drat    wt  qsec vs am gear carb    frml_1    frml_2    frml_3
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4 138.65796 138.65796 140.61340
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4 138.65796 138.65796 139.55056
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1 122.76445 122.76445 124.91348
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1 135.12607 135.12607 134.36670
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2 158.96634 158.96634 158.85438
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1 164.26418 164.26418 164.42112
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4 197.81716 197.81716 199.74665
...

请注意,这些公式已从RHS中删除了 hp ,因为 hp 也是响应.我将 drat 用作演示目的.

Note that the formulas have hp removed from RHS, as hp is also the response. I used drat instead for demonstration purposes.

这篇关于R中使用“留一法"的线性回归预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆