[英]specify model with selected terms using lm
A pretty straightforward for those with intimate knowledge of R 对于那些熟悉R的人来说非常简单
full <- lm(hello~., hellow)
In the above specification, linear regression is being used and hello is being modeled against all variables in dataset hellow
. 在以上规范中,使用了线性回归,并针对数据集hellow
中的所有变量对hello进行了建模。
I have 33 variables in hellow
; 我在33个变数hellow
; I wish to specify some of those as independent variable. 我希望将其中一些指定为自变量。 These variables have names that carry a meaning so I really don't want to rename them to x1
x2
etc. 这些变量的名称带有含义,所以我真的不想将它们重命名为x1
x2
等。
How can I, without having to type the individual names of the variables (since that is pretty tedious), specify a select number of variables from the whole bunch? 我不必键入变量的单个名称(因为这很繁琐),如何在整个变量集中指定一定数量的变量?
I tried 我试过了
full <- lm(hello~hellow[,c(2,5:9)]., hellow)
but it gave me an error "Error in model.frame.default(formula = hello ~ hellow[, : invalid type (list) for variable 'hellow[, c(2, 5:9)]'
但这给了我一个错误"Error in model.frame.default(formula = hello ~ hellow[, : invalid type (list) for variable 'hellow[, c(2, 5:9)]'
reformulate
will construct a formula given the names of the variables, so something like: reformulate
构造将根据给定的变量名称构造一个公式,如下所示:
(Construct data first): (首先构建数据):
set.seed(101)
hellow <- setNames(as.data.frame(matrix(rnorm(1000),ncol=10)),
c("hello",paste0("v",1:9)))
Now run the code: 现在运行代码:
ff <- reformulate(names(hellow)[c(2,5,9)],response="hello")
full <- lm(ff, data=hellow)
should work. 应该管用。 (Works fine with this example.) (在此示例中工作正常。)
An easier solution just occurred to me; 我想到了一个更简单的解决方案。 just select the columns/variables you want first: 只需先选择要使用的列/变量:
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.