Re: [R] Data manipulation for random intercept GLMM

2008-05-02 Thread Ben Bolker
Giovanni Petris GPetris at uark.edu writes:

  ## Example: Ratings of prime minister (Agresti, Table 12.1, p.494)
  rating - matrix(c(794, 86, 150, 570), 2, 2)
  dimnames(rating) - list(First = c(approve, disapprove),
 +  Second = c(approve, disapprove))
  rating
 Second
 Firstapprove disapprove
   approve794150
   disapprove  86570

[snip]
 
  approval - factor(c(Approve, Disapprove),
 +levels = c(Disapprove, Approve))
  survey - factor(c(First, Second))
  tmp - data.frame(approval = unlist(expand.grid(approval, approval)),
 +   survey = rep(survey, each = 4))
  rat.df - cbind(tmp[rep(1:8, rep(rating, 2)), ],
 + id = factor(rep(1:sum(rating), 2)))
  row.names(rat.df) - NULL
 
 That does the job, since now I can call lmer:
 
  m1 - lmer(approval ~ survey + (1 | id), family = binomial, data = rat.df,
 +method = Laplace)
 
[snip]

 
 Consider also that the next simplest example is the following, in
 which there are three items on a questionnaire and gender is included
 in the model:
 
  ### Example: Support for legalizing abortion (Agresti, Table 10.13, p.441)
  legalize - matrix(c(342, 440, 26, 25, 6, 14, 21, 18, 11, 14,
 +  32, 47, 19, 22, 356, 457), nr = 2)
  dimnames(legalize) - list(Gender = c(Male, Female),
 +Three = c(111, 112, 211, 212, 121,
 +122, 221, 222))
  legalize
 Three
 Gender   111 112 211 212 121 122 221 222
   Male   342  26   6  21  11  32  19 356
   Female 440  25  14  18  14  47  22 457
 
 (Here '111' means (Yes, Yes, Yes) on the three items, etc.)
 
 How can I tranform elegantly this table into a dataframe that I can
 feed to lmer?
 

  I would like to know the answers to this as well.  I thought
I was going to be able to get away with as.data.frame.table()
[which is handy and underappreciated], but it doesn't expand
counts.
   I hope you get an elegant solution: in the meantime, I guess
my advice is to try to write a reasonably general (if not necessarily
absolutely elegant) function that you can supply your students with.
If they have an expand.table() function, they won't care how complex
it is internally ...

  Ben Bolker

__
R-help@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.


Re: [R] Data manipulation for random intercept GLMM

2008-05-02 Thread Greg Snow
Is this what you are trying to do?

 tt - matrix( 5:10, ncol=2 )
 df - as.data.frame.table(tt)
 df2 - df[ rep( 1:nrow(df), df$Freq ), ]

And is that elegant enough?

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
[EMAIL PROTECTED]
(801) 408-8111
 
 

 -Original Message-
 From: [EMAIL PROTECTED] 
 [mailto:[EMAIL PROTECTED] On Behalf Of Ben Bolker
 Sent: Friday, May 02, 2008 7:36 AM
 To: [EMAIL PROTECTED]
 Subject: Re: [R] Data manipulation for random intercept GLMM
 
 Giovanni Petris GPetris at uark.edu writes:
 
   ## Example: Ratings of prime minister (Agresti, Table 
 12.1, p.494) 
   rating - matrix(c(794, 86, 150, 570), 2, 2)
   dimnames(rating) - list(First = c(approve, disapprove),
  +  Second = c(approve, disapprove))
   rating
  Second
  Firstapprove disapprove
approve794150
disapprove  86570
 
 [snip]
  
   approval - factor(c(Approve, Disapprove),
  +levels = c(Disapprove, Approve))
   survey - factor(c(First, Second)) tmp - 
 data.frame(approval = 
   unlist(expand.grid(approval, approval)),
  +   survey = rep(survey, each = 4))
   rat.df - cbind(tmp[rep(1:8, rep(rating, 2)), ],
  + id = factor(rep(1:sum(rating), 2)))
   row.names(rat.df) - NULL
  
  That does the job, since now I can call lmer:
  
   m1 - lmer(approval ~ survey + (1 | id), family = 
 binomial, data = 
   rat.df,
  +method = Laplace)
  
 [snip]
 
  
  Consider also that the next simplest example is the following, in 
  which there are three items on a questionnaire and gender 
 is included 
  in the model:
  
   ### Example: Support for legalizing abortion (Agresti, 
 Table 10.13, 
   p.441) legalize - matrix(c(342, 440, 26, 25, 6, 14, 21, 
 18, 11, 14,
  +  32, 47, 19, 22, 356, 457), nr = 2)
   dimnames(legalize) - list(Gender = c(Male, Female),
  +Three = c(111, 112, 211, 212, 121,
  +122, 221, 222))
   legalize
  Three
  Gender   111 112 211 212 121 122 221 222
Male   342  26   6  21  11  32  19 356
Female 440  25  14  18  14  47  22 457
  
  (Here '111' means (Yes, Yes, Yes) on the three items, etc.)
  
  How can I tranform elegantly this table into a dataframe that I can 
  feed to lmer?
  
 
   I would like to know the answers to this as well.  I 
 thought I was going to be able to get away with 
 as.data.frame.table() [which is handy and underappreciated], 
 but it doesn't expand counts.
I hope you get an elegant solution: in the meantime, I 
 guess my advice is to try to write a reasonably general (if 
 not necessarily absolutely elegant) function that you can 
 supply your students with.
 If they have an expand.table() function, they won't care how 
 complex it is internally ...
 
   Ben Bolker
 
 __
 R-help@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.
 

__
R-help@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.


[R] Data manipulation for random intercept GLMM

2008-05-01 Thread Giovanni Petris

Hello,

I am working on some examples of GLMM for my students but I am afraid
that my way of preparing a dataframe to pass to lmer will make them
think that R is a very difficult and un-natural language. Here is for
example a simple data set about approval ratings on two different
surveys for a random sample of 1600 individuals. 

 ## Example: Ratings of prime minister (Agresti, Table 12.1, p.494)
 rating - matrix(c(794, 86, 150, 570), 2, 2)
 dimnames(rating) - list(First = c(approve, disapprove),
+  Second = c(approve, disapprove))
 rating
Second
Firstapprove disapprove
  approve794150
  disapprove  86570

It seems to me that, in order to fit a model using lmer, I cannot use
the table directly, but I need a dataframe with 1600 x 2 rows and
columns response (Approve/Disapprove), survey (First/Second), and
subject id. So I proceeded to create such a dataframe:

 approval - factor(c(Approve, Disapprove),
+levels = c(Disapprove, Approve))
 survey - factor(c(First, Second))
 tmp - data.frame(approval = unlist(expand.grid(approval, approval)),
+   survey = rep(survey, each = 4))
 rat.df - cbind(tmp[rep(1:8, rep(rating, 2)), ],
+ id = factor(rep(1:sum(rating), 2)))
 row.names(rat.df) - NULL

That does the job, since now I can call lmer:

 m1 - lmer(approval ~ survey + (1 | id), family = binomial, data = rat.df,
+method = Laplace)

The issue I have is that creating the 'rat.df' dataframe above will
likely make all of my students look for a different software. So my
question is the following. Is there a more elegant way to create the
dataframe needed by lmer from the tabular form in which one is more
likely to find these kind of data?

Consider also that the next simplest example is the following, in
which there are three items on a questionnaire and gender is included
in the model:

 ### Example: Support for legalizing abortion (Agresti, Table 10.13, p.441)
 legalize - matrix(c(342, 440, 26, 25, 6, 14, 21, 18, 11, 14,
+  32, 47, 19, 22, 356, 457), nr = 2)
 dimnames(legalize) - list(Gender = c(Male, Female),
+Three = c(111, 112, 211, 212, 121,
+122, 221, 222))
 legalize
Three
Gender   111 112 211 212 121 122 221 222
  Male   342  26   6  21  11  32  19 356
  Female 440  25  14  18  14  47  22 457

(Here '111' means (Yes, Yes, Yes) on the three items, etc.)

How can I tranform elegantly this table into a dataframe that I can
feed to lmer?

Thank you in advance for your replies!

Giovanni Petris

-- 

Giovanni Petris  [EMAIL PROTECTED]
Department of Mathematical Sciences
University of Arkansas - Fayetteville, AR 72701
Ph: (479) 575-6324, 575-8630 (fax)
http://definetti.uark.edu/~gpetris/

__
R-help@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.