[英]specify model with selected terms using lm
对于那些熟悉R的人来说非常简单
full <- lm(hello~., hellow)
在以上规范中,使用了线性回归,并针对数据集hellow
中的所有变量对hello进行了建模。
我在33个变数hellow
; 我希望将其中一些指定为自变量。 这些变量的名称带有含义,所以我真的不想将它们重命名为x1
x2
等。
我不必键入变量的单个名称(因为这很繁琐),如何在整个变量集中指定一定数量的变量?
我试过了
full <- lm(hello~hellow[,c(2,5:9)]., hellow)
但这给了我一个错误"Error in model.frame.default(formula = hello ~ hellow[, : invalid type (list) for variable 'hellow[, c(2, 5:9)]'
reformulate
构造将根据给定的变量名称构造一个公式,如下所示:
(首先构建数据):
set.seed(101)
hellow <- setNames(as.data.frame(matrix(rnorm(1000),ncol=10)),
c("hello",paste0("v",1:9)))
现在运行代码:
ff <- reformulate(names(hellow)[c(2,5,9)],response="hello")
full <- lm(ff, data=hellow)
应该管用。 (在此示例中工作正常。)
我想到了一个更简单的解决方案。 只需先选择要使用的列/变量:
hellow_red <- hellow[,c(1,2,5,9)]
full2 <- lm(hello~., data=hellow_red)
all.equal(coef(full),coef(full2)) ## TRUE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.