简体   繁体   English

使用lm指定具有选定术语的模型

[英]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.

相关问题 如何指定lm模型矩阵 - How to specify lm model matrix 将交互项序列添加到线性模型中,lm() - Add sequence of interaction terms to linear model, lm() LM无法正常工作(ExtractVars中的模型公式无效(terms.formula(formula,data = data)中的错误) - LM not working (Error in terms.formula(formula, data = data) invalid model formula in ExtractVars 执行多元线性回归分析,包括交互项,使用 summary() 解释结果,使用 lm() 诊断图 - Perform multiple linear regression analysis including interaction terms, interpret results using summary() and diagnostic plots using lm() 如何在 R 中为 lm() 保留 fit$model 中的变量,我*不*在 lm 调用本身中使用它? - How to keep a variable in fit$model for lm() in R that I'm *not* using within the lm call itself? 使用lm()时,在交互项中控制虚拟变量的值(TRUE或FALSE) - controlling the value (TRUE or FALSE) of dummy variables in interaction terms when using lm() 在术语中出现错误..公式:'.' 在公式中,在 lm 中使用 train() function 时没有数据参数 - Getting Error in terms..formula: '.' in formula and no data argument in using train() function in lm 汇总和lm模型错误 - Summarise and lm model error lm()线性模型-属性 - lm() linear model - attributes R lm 捕获交互项,但不是分类变量 - R lm Capture interaction terms, but not categorical variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM