2

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

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
oivemaria
  • 453
  • 4
  • 20
  • 1
    closely related: http://stackoverflow.com/questions/22253688/add-formula-terms-programmatically/22254121#22254121 – Ben Bolker Feb 07 '15 at 17:30

1 Answers1

3

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
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Thanks, I got this error : Error in eval(expr, envir, enclos) : object 'hello ' not found" – oivemaria Feb 07 '15 at 22:48
  • do you have a `hello` variable in your data set `hellow` ??? Probably can't help resolve this without a reproducible example ... – Ben Bolker Feb 08 '15 at 00:16
  • Yes, absolutely - I have hello in hellow as a variable. What do you mean reproducible example? Isn't the code considered reproducible example? – oivemaria Feb 08 '15 at 01:05
  • A [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) is one that is self-contained, so that other people can actually run it and get the same error you're seeing. Remote debugging is really hard. – Ben Bolker Feb 08 '15 at 01:19
  • Thanks alot again. Both solutions work. I had made a specification error which was mucking up the code. – oivemaria Feb 08 '15 at 03:31