3

I think I'm on to a good idea, but don't know how to implement it. I am currently running a number of nested models. For example:

y<-c(1, 0, 1, 1, 1, 1, 1, 1, 0)
x1<-c(1, 0, 9, 9, 1, 9, 2, 1, 0)
x2<-c(1, 2, 3, 5, 4, 2, 4, 5, 1)
x3<-c(1, 2, 1, 4, 1, 3, 4, 8, 3)

model1 <-lm(y~x1)
model2 <-lm(y~x1+x2)
model3 <-lm(y~x1+x2+x3)

model1_output<-(summary(model1 )$coefficients[1])
model2_output<-(summary(model1 )$coefficients[1])
model3_output<-(summary(model1 )$coefficients[1])

I would then like to put all of my output in a data table that matches along the rows (variable names) but inserts the new coefficients in their own columns. I'd like the data.frame presenting the output from my example as:

             b(Model 1)  b(Model 2) b(Model 3)
 (Intercept)  0.59217    0.2555     0.27983
 x1           0.05220    0.04116    0.0375
 x2              NA      0.12530    0.15142
 x3              NA      NA        -0.02994

I'm sure there must be some smart way to do this using the plyr() package (or some other package!), but can't seem to figure it out. Thanks!

IRTFM
  • 258,963
  • 21
  • 364
  • 487
roody
  • 2,633
  • 5
  • 38
  • 50
  • This answer is very relevant and likely more flexible than some of what has been discussed here: http://stackoverflow.com/questions/16962576/how-can-i-rbind-vectors-matching-their-column-names – Brandon Feb 18 '15 at 22:29

2 Answers2

4

Using this answer from another question:

cbind.fill<-function(...){
    nm <- list(...) 
    nm<-lapply(nm, as.matrix)
    n <- max(sapply(nm, nrow)) 
    do.call(cbind, lapply(nm, function (x) 
    rbind(x, matrix(, n-nrow(x), ncol(x))))) 
}

You could do this:

do.call(cbind.fill,lapply(list(model1,model2,model3),function(x){coef(x)}))

                  [,1]       [,2]        [,3]
(Intercept) 0.59216966 0.25551154  0.27982881
x1          0.05220228 0.04116431  0.03754457
                    NA 0.12530141  0.15142340
                    NA         NA -0.02993768

And then set the row and column names manually. (Note that this returns a matrix, not a data frame, if that matters to you.)

Community
  • 1
  • 1
joran
  • 169,992
  • 32
  • 429
  • 468
  • Quick question--How would I add multiple columns relating to my coefficients? I tried: `do.call(cbind.fill,lapply(list(model1,model2,model3),function(x){coef[1:4](x)}))` but that didn't seem to work. – roody Jul 27 '12 at 18:17
0

Try the following. I tried to keep it as general as possible, and borrowed some help from here.

models <- list(model1=model1,model2=model2,model3=model3)    

#Find out which model (by index) has the most coefficients.
max.model <- which.max(unlist(lapply(models, function(x) length(x[[1]]))))

#How many coefficients there are in the 'biggest' model (including intercept)
max <- length(models[[max.model]][[1]])

#Create list of coefficients, replacing blank spaces with NA
coeff <- lapply(models, function(x) c(x[[1]],rep(NA,max-length(x[[1]]))))

#See the link above
result <- do.call(cbind,coeff)

colnames(result) <- names(models)

rownames(result) <- names(models[[max.model]][[1]])

Result:

> result
                model1     model2      model3
(Intercept) 0.59216966 0.25551154  0.27982881
x1          0.05220228 0.04116431  0.03754457
x2                  NA 0.12530141  0.15142340
x3                  NA         NA -0.02993768

What I mean by trying to keep it general is that this would work with any number of models; only the first line of code needs to change.

Community
  • 1
  • 1
Edward
  • 5,367
  • 1
  • 20
  • 17