Reputation: 53
Function lm(...) returns an object of class 'lm'. How do I create an array of such objects? I want to do the following:
my_lm_array <- rep(as.lm(NULL), 20)
#### next, populate this array by running lm() repeatedly:
for(i in 1:20) {
my_lm_array[i] <- lm(my_data$results ~ my_data[i,])
}
Obviously the line "my_lm <- rep(as.lm(NULL), 20)" does not work. I'm trying to create an array of objects of type 'lm'. How do I do that?
Upvotes: 1
Views: 137
Reputation: 226087
Well, you can create an array of empty/meaningless lm
objects as follows:
z <- NA
class(z) <- "lm"
lm_array <- replicate(20,z,simplify=FALSE)
but that's probably not the best way to solve the problem. You could just create an empty list of the appropriate length (vector("list",20)
) and fill in the elements as you go along: R is weakly enough typed that it won't mind you replacing NULL
values with lm
objects. More idiomatically, though, you can run lapply
on your list of predictor names:
my_data <- data.frame(result=rnorm(10), v1=rnorm(10), v2=rnorm(10))
prednames <- setdiff(names(my_data),"result") ## extract predictor names
lapply(prednames,
function(n) lm(reformulate(n,response="result"),
data=my_data))
Or, if you don't feel like creating an anonymous function, you can first generate a list of formulae (using lapply
) and then run lm
on them:
formList <- lapply(prednames,reformulate,response="result") ## create formulae
lapply(formList,lm,data=my_data) ## run lm() on each formula in turn
will create the same list of lm
objects as the first strategy above.
In general it is good practice to avoid using syntax such as my_data$result
inside modeling formulae; instead, try to set things up so that all the variables in the model are drawn from inside the data
object. That way methods like predict
and update
are more likely to work correctly ...
Upvotes: 1
Reputation: 49033
Not sure it will answer your question, but if what you want to do is run a series of lm
from a variable against different columns of a data frame, you can do something like this :
data <- data.frame(result=rnorm(10), v1=rnorm(10), v2=rnorm(10))
my_lms <- lapply(data[,c("v1","v2")], function(v) {
lm(data$result ~ v)
})
Then, my_lms
would be a list of elements of class lm
.
Upvotes: 2