2

I have a list of variable names that I would like to sequentially exclude from a best fitted model using the function update from lm. Because the list of variables are likely to change I want to loop through a given list but I can not get the elements of the list to be read as dependent variable. I found some code that I thought it could work:

Example code

hsb2 <-read.csv("www.ats.ucla.edu/stat/data/hsb2.csv")
names(hsb2)
varlist <- names(hsb2)[8:11]
models <- lapply(varlist, function(x) {
lm(substitute(read ~ i, list(i = as.name(x))), data = hsb2)
})

But not if I use the update function on a previous lm object

words<-c('Age','Sex', 'Residuals')
models <- lapply(words, function(x){update(substitute(
 lmobject,~.-i,list(i = as.name(x))),data =data_complete)})

I also tried

re<-c()
for (i in 1:3) {
lmt<-update(lmobject,~.-words[i])
r2no_i<-summary(lmt)$r.squared
re<-c(re, r2no_i)
}

I think this is pretty simple but I could not make the variable to be read properly Any tip is highly appreciated

Konrad
  • 17,740
  • 16
  • 106
  • 167
Laura
  • 21
  • 3
  • Where does `lmobject` come from? – MrFlick Mar 09 '16 at 15:25
  • Thanks for reply. lmobject comes from using the stepAIC from MASS package (lm function). lmobject is the object storing the final model chosen using backward method. Now I want to look at the contribution of some variables to the final model. That's when I got stuck. I do not want to rewrite a formula but to use update function to remove the variable of interest from the final model. – Laura Mar 09 '16 at 15:33
  • well, then please make sure your example is [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) such that all variables are defined. This makes it easier to help you. You can use `update(y~a+b+c+d, ~.-b)` to remove variables from a model but you need that second parameter to be a formula. Something like: `update(y~a+b+c+d, paste0("~.-", "b"))` – MrFlick Mar 09 '16 at 15:39

1 Answers1

0

Is it possible that the built-in stats::drop1() function would do what you need?

Read data (note "http://..." is needed)

hsb2 <- read.csv("http://www.ats.ucla.edu/stat/data/hsb2.csv")
varlist <- names(hsb2)[8:11]

Fit all models: construct list of formulas:

formList <- lapply(varlist,reformulate,response="read")

Construct models (a little bit of fancy footwork is needed to get the $call component to look right)

modList <- lapply(formList,
                  function(x) {
                    m <- lm(x,data=hsb2)
                    m$call$formula <- eval(m$call$formula)
                    return(m)
})

words <- c('Age','Sex', 'Residuals')

pick one of the fitted models:

lmobject <- modList[[1]]

construct formulas of the form . ~ . - w

minus_form <- function(w) 
    reformulate(c(".",paste0("-",w)),response=".")

minus_form("abc")  ## . ~ . + -abc

Refit models with dropped terms:

newMods <- lapply(words,
                  function(w) {
                     update(lmobject,minus_form(w))
              })
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453