Friday, 23 August 2013

Rename terms in fitted model object

Rename terms in fitted model object

I have a list of regression models which all the same number of terms
(that is, the same number of predictive variables). Substantively, that
they all have different model terms is right. But when it comes to putting
them in a regression table, I want them all the models to share a single
formula, simply for the sake of presentation.
Some indicative data
library(plyr)
d1 <- data.frame(y = rnorm(100),
x1 = runif(100),
x2 = runif(100),
x3 = runif(100),
x4 = runif(100))
Fit the models
mods.form <- paste("y ~ x", 1:4, sep = "")
mod.list <- llply(mods.form, function(i) lm(i, d1))
Here are the terms I want to modify
llply(mod.list, function(i) attr(terms(i), "variables"))
[[1]]
list(y, x1)
[[2]]
list(y, x2)
[[3]]
list(y, x3)
[[4]]
list(y, x4)
I want every model in the list to have the same variable names as the
first model, so I tried:
mod.list2 <- llply(mod.list, function(i) attr(terms(i), "variables") =
list("y", "x1"))
which provides this error
Error in attr(terms(i), "variables") = list("y", "x1") :
could not find function "terms<-"
Is there a simple solution here?

No comments:

Post a Comment