[R] Help using mapply to run multiple models

David Winsemius dwinsemius at comcast.net
Wed Dec 18 04:12:21 CET 2013


On Dec 17, 2013, at 5:53 PM, Simon Kiss wrote:

> I think I'm missing something.  I have a data frame that looks below.  
> sample.df<-data.frame(var1=rbinom(50, size=1, prob=0.5), var2=rbinom(50, size=2, prob=0.5), var3=rbinom(50, size=3, prob=0.5), var4=rbinom(50, size=2, prob=0.5), var5=rbinom(50, size=2, prob=0.5))
> 
> I'd like to run a series of univariate general linear models where var1 is always the dependent variable and each of the other variables is the independent. Then I'd like to summarize each in a table.
> I've tried : 
> 
> sample.formula=list(var1~var2, var1 ~var3, var1 ~var4, var1~var5)
> mapply(glm, formula=sample.formula, data=list(sample.df), family='binomial')
> 
> And that works pretty well, except, I'm left with a matrix that contains all the information I need. I can't figure out how to use summary() properly on this information to usefully report that information. 

The default for mapply's SIMPLIFY argument is TRUE. If you do not want a matrix, then set it to FALSE and the list items will retain their glm-object status.

(The summary function applied to the resulting list is still a bit strange, but it is recognizable as having class 'glm' at the end. You should be able to extract the bits that you want and ignore the strange $call item.)

-- 
David Winsemius
Alameda, CA, USA



More information about the R-help mailing list