我想循环包含/排除某些变量,但我遇到了错误。这是一些样本数据的问题。
mtcars = data('mtcars')
for(i in 0:1) {
fitlm = lm(mpg ~ cyl + i * drat, data = mtcars)
}
Error in model.frame.default(formula = mpg ~ cyl + i * drat, data = mtcars, : variable lengths differ (found for 'i')
但是这会毫无问题地运行:
fitlm = lm(mpg ~ cyl + 0 * drat, data = mtcars)
fitlm = lm(mpg ~ cyl + 1 * drat, data = mtcars)
如果变量有一个数字乘数,为什么回归会起作用,但如果它是i
则会失败?
答案 0 :(得分:0)
尝试使用as.formula
,如下所示:
# create an empty list to store the results
fitlm <- list()
# loop, fit the model and assign the result to a new list in fitlm
for(i in 0:1) {
fitlm[[i+1]] <- lm(as.formula(paste("mpg ~ cyl +", i, "* drat")), data = mtcars)
}
您还可以使用purrr::map
代替循环,如下所示:
fitlm <- purrr::map(c(0,1), ~lm(as.formula(paste("mpg ~ cyl +", .x, "* drat")), data = mtcars))
结果将是:
> fitlm
[[1]]
Call:
lm(formula = as.formula(paste("mpg ~ cyl +", i, "* drat")),
data = mtcars)
Coefficients:
cyl
2.79
[[2]]
Call:
lm(formula = as.formula(paste("mpg ~ cyl +", i, "* drat")),
data = mtcars)
Coefficients:
(Intercept) cyl
37.885 -2.876
答案 1 :(得分:0)
这有点像黑客,但你可以尝试一些形式
fitlm = list()
for(i in 0:1) {
idrat = i*mtcars$drat
fitlm[[i+1]] = lm(mpg ~ cyl + idrat, data = mtcars)
}
给出结果
fitlm
## [[1]]
##
## Call:
## lm(formula = mpg ~ cyl + idrat, data = mtcars)
##
## Coefficients:
## (Intercept) cyl idrat
## 37.885 -2.876 NA
##
##
## [[2]]
##
## Call:
## lm(formula = mpg ~ cyl + idrat, data = mtcars)
##
## Coefficients:
## (Intercept) cyl idrat
## 28.725 -2.484 1.872
这可以绕过lm()
函数,在查看*
字符时查找交互,就像您在使用数字时找到的那样。