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!
Using this answer from another question:
You could do this:
And then set the row and column names manually. (Note that this returns a matrix, not a data frame, if that matters to you.)