使用线性回归模型的函数 [英] Use a function with a linear regression model

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

问题描述

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

I can run multiple linear regressions, and in each model estimate coefficients by removing one observation from the data.frame like this:

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

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

But when I try to use this inside a function I get an error:

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

如何让这个函数起作用?

How can I get this function to work?

推荐答案

查看这里提出的建立动态公式的问题和答案:具有动态变量数的公式

Have a look at the question and answers here for building a dynamic formula: Formula with dynamic number of variables

如果我是你,我会犯

If I were you, I would go with the reformulate suggestion:

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)

这篇关于使用线性回归模型的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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