Skip to content
Back to formatted view

Raw Message

Message-ID: <BA328A0C-AB0A-4DD8-8C9A-A949B6264EC0@comcast.net>
Date: 2013-12-18T03:12:21Z
From: David Winsemius
Subject: Help using mapply to run multiple models
In-Reply-To: <B7739A56-FC85-4BD9-A375-A317550EBC16@gmail.com>

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