[R] Reshaping grouped data

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Tue Feb 16 16:45:10 CET 2010


Whoops, forgot to cc. the list...

-pd

Myrland Øystein wrote:
> Dear R-help list,
> 
>  
> 
> I have grouped data, looking like this:
> 
>  
> 
> cases <- c(23,12,56,81)
> 
> total <- c(123,234,248,390)
> 
> x1 <- c(0,0,1,1)
> 
> x2 <- c(0,1,0,1)
> 
>  
> 
> Data <- as.data.frame(cbind(cases,total,x1,x2))
> 
> Data
> 
>  
> 
> I would like to run a logistic regression with group weights on these,
> where cases and (total-cases) are equal to the group weights (w).
> 
>  
> 
> My final data would look like:
> 
>  
> 
> w <- c(100,23,222,12,192,56,309,81)
> 
> y <- c(0,1,0,1,0,1,0,1)
> 
> x1 <- c(0,0,0,0,1,1,1,1)
> 
> x2 <- c(0,0,1,1,0,0,1,1)
> 
>  
> 
> Data.long <- as.data.frame(cbind(w,y,x1,x2))
> 
> Data.long
> 
>  
> 
> Any suggestions?

Er, why?

summary(glm(cbind(cases,total-cases)~x1+x2, data=Data, family=binomial))
summary(glm(cases/total~x1+x2,weights=total,data=Data, family=binomial))

seems to work just fine, with same results as

summary(glm(y~x1+x2,weights=w, data=Data.long, family=binomial))


(BTW, beware the residual deviance in the Data,long analysis. The
saturated model is not what you might think.)

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907



More information about the R-help mailing list