使用具有线性回归模型的函数

时间:2014-03-22 07:17:02

标签: r function linear-regression

我可以运行多个线性回归,并且在每个模型中通过从data.frame中移除一个观察来估计系数,如下所示:

library(plyr)
as.data.frame(laply(1:nrow(mtcars), function(x) coef(lm(mpg ~ hp + wt, mtcars[-x,]))))

   (Intercept)          hp        wt
1     37.48509 -0.03207047 -3.918260
2     37.33931 -0.03219086 -3.877571
3     37.56512 -0.03216482 -3.939386
4     37.22292 -0.03171010 -3.880721
5     37.22437 -0.03185754 -3.876831
6     37.23686 -0.03340464 -3.781698
7     37.21965 -0.03030994 -3.927877
8     37.17190 -0.03004264 -3.956131
9     37.19513 -0.03126773 -3.899208
10    37.23247 -0.03210973 -3.856147
11    37.24180 -0.03271464 -3.817199
12    37.27110 -0.03172052 -3.900789
13    37.23371 -0.03180418 -3.881005
14    37.17627 -0.03161969 -3.852229
15    37.23772 -0.03174926 -3.882692
16    37.50095 -0.03123959 -3.999952
17    38.57947 -0.03054970 -4.419658
18    36.33970 -0.02919481 -3.780739
19    36.97369 -0.03146134 -3.825266
20    36.05264 -0.03036368 -3.640124
21    37.59383 -0.03236419 -3.933150
22    37.22107 -0.03221683 -3.822311
23    37.25783 -0.03210603 -3.832542
24    37.17881 -0.03059583 -3.902879
25    37.32141 -0.03175235 -3.932869
26    37.28836 -0.03186673 -3.889049
27    37.23322 -0.03177585 -3.879156
28    36.55294 -0.03346756 -3.621153
29    37.26387 -0.03041372 -3.942066
30    37.33342 -0.03099339 -3.933609
31    37.23918 -0.03955498 -3.562963
32    37.35656 -0.03212351 -3.885988

但是当我尝试在函数中使用它时,我得到一个错误:

statRemoveOne <- function(df, response, predictors){
    as.data.frame(laply(1:nrow(df), function(x) coef(lm(response ~ predictors, df[-x,]))))
}

statRemoveOne(mtcars, response = "mpg", predictors = paste("+ hp", "wt", sep = " + "))

Warning message:
In model.response(mf, "numeric") : NAs introduced by coercion
Error in as.data.frame(laply(1:nrow(df), function(x) coef(lm(response ~  : 
  error in evaluating the argument 'x' in selecting a method for function 'as.data.frame': Error in coef(lm(response ~ predictors, df[-x, ])) : 
  error in evaluating the argument 'object' in selecting a method for function 'coef': Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels

如何让这个功能起作用?

1 个答案:

答案 0 :(得分:2)

在此处查看问题和答案,以构建动态公式:Formula with dynamic number of variables

如果我是你,我会采用reformulate建议:

statRemoveOne <- function(df, response, predictors, intercept){
  formula <- reformulate(predictors, response, intercept)
  as.data.frame(laply(1:nrow(df),
                      function(x) coef(lm(formula, df[-x,]))))
}

statRemoveOne(mtcars, response = "mpg",
                      predictors = c("hp", "wt"),
                      intercept = TRUE)