1

I want to run a whole batch of regressions over every variable in a data frame, and then store the residual deviance value from each regression in a new vector as the loop goes along.

The frame is called "cw". The first few variables are just metadata, so ignore those. I try the following:

deviances<-c()
for (x in colnames(cw)[1:8]){deviances[x]<-NA}
for (x in colnames(cw)[8:27]){
  model<-glm(cwonset ~ x, fmaily = binomial, data = cw)
  append(deviances, model$deviance)
}

However, it gives the error:

Error in model.frame.default(formula = cwonset ~ x, data = cw, drop.unused.levels = TRUE) : 
  variable lengths differ (found for 'x')

Any idea why?

1 Answers1

3

without data, i had to rely on mtcars to help you out, no need of for loop also. I assumed mpg as the dependent variable

Logic : sapply helps me o loop through each colname at a time and then I just regress that. It internally is a for loop though

sapply(colnames(mtcars[-1]), function(x) {
                                        form <- as.formula(paste0("mpg~", x))
                                        model <- glm(form, data = mtcars)
                                        model$deviance})
#      cyl     disp       hp     drat       wt     qsec       vs       am     gear     carb 
# 308.3342 317.1587 447.6743 603.5667 278.3219 928.6553 629.5193 720.8966 866.2980 784.2711 
joel.wilson
  • 8,243
  • 5
  • 28
  • 48