Re: [R] Same regression per sub-group: apply?

From: Romain Francois <rfrancois_at_mango-solutions.com>
Date: Fri, 07 Dec 2007 09:16:59 +0000

What about ?by, something like this (still untested):

model.per.country <- by( data, data$COUNTRY, function (x) {

    glm(dependent.var ~ FEMALE + AGE + EDUCLIN, family = binomial, data = x) })

Or

model.per.country <- by( data, data$COUNTRY,   glm , dependent.var ~ FEMALE + AGE + EDUCLIN, family = binomial )

Cheers,

Romain

Dimitris Rizopoulos wrote:
> try something like this (untested):
>
> dataCountry <- split(data, data$COUNTRY)
> model.per.country <- lapply(dataCountry, function (x) {
> glm(dependent.var ~ FEMALE + AGE + EDUCLIN, family = binomial,
> data = x)
> })
>
>
> I hope it helps.
>
> Best,
> Dimitris
>
> ----
> Dimitris Rizopoulos
> Ph.D. Student
> Biostatistical Centre
> School of Public Health
> Catholic University of Leuven
>
> Address: Kapucijnenvoer 35, Leuven, Belgium
> Tel: +32/(0)16/336899
> Fax: +32/(0)16/337015
> Web: http://med.kuleuven.be/biostat/
> http://www.student.kuleuven.be/~m0390867/dimitris.htm
>
>
> ----- Original Message -----
> From: "Rense Nieuwenhuis" <r.nieuwenhuis_at_student.ru.nl>
> To: <r-help_at_stat.math.ethz.ch>
> Sent: Friday, December 07, 2007 9:32 AM
> Subject: [R] Same regression per sub-group: apply?
>
>
>
>> Dear helpers,
>>
>> I've come up with what is probably a simple problem, but I cannot
>> find the solution. I have a data-set containing survey-data from
>> several countries. What I want to do is to perform some regression
>> analyses, for each country separately. The question is, how to do
>> this nicely (thus without repeating the same syntax with another
>> `subset' argument).
>>
>> I thought of the following:
>>
>> model.per.country <- tapply(data, data$COUNTRY, function(x) glm
>> (dependent.var ~ FEMALE + AGE + EDUCLIN + (), family=binomial,
>> data=capital))
>>
>> But this does not work. What goes wrong, I think, is that the
>> dependent variable is clustered according to `Country', but not so
>> for the predictors. The error message I received:
>>
>> Error in tapply(dat, dat$COUNTRY, function(x) glm(participate ~
>> FEMALE + :
>> arguments must have same length
>>
>> Could you please help me solving this little problem?
>>
>> Thanks in advance:
>>
>> Rense Nieuwenhuis
>>
>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help_at_r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>
>
> Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>

-- 
Mango Solutions
data analysis that delivers

Tel:  +44(0) 1249 467 467
Fax:  +44(0) 1249 467 468
Mob:  +44(0) 7813 526 123

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Fri 07 Dec 2007 - 09:26:37 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 07 Dec 2007 - 09:30:17 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.