oivemaria
oivemaria

Reputation: 463

specify model with selected terms using lm

A pretty straightforward for those with intimate knowledge of 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.

I have 33 variables in 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.

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)]'

Upvotes: 2

Views: 324

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226057

reformulate will construct a formula given the names of the variables, so something like:

(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

Upvotes: 3

Related Questions