Re: [R] GLMM and data manipulation (2nd try)

From: Giovanni Petris <GPetris_at_uark.edu>
Date: Fri, 02 May 2008 16:05:35 -0500 (CDT)

Andrew,

Thank you for your reply. In fact, I had a way of unrolling the table but I think yours look much nicer - much readable to me. Below is what I did, but I was afraid I would scare my students away from R!

> 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

Thank you again,
Giovanni

> Date: Sat, 03 May 2008 06:43:22 +1000
> From: Andrew Robinson <A.Robinson_at_ms.unimelb.edu.au>
> Cc: r-help_at_r-project.org
> User-Agent: Mutt/1.4.2.3i
>
> On Fri, May 02, 2008 at 03:06:31PM -0500, Giovanni Petris wrote:
> >
> > Hello,
> >
> > I posted a question yesterday but I got no replies, so I'll try to
> > reformulate it in a more concise way.
> >
> > I have the following data, summarizing 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
> > First approve disapprove
> > approve 794 150
> > disapprove 86 570
> >
> > I would like to fit a logit model with approve/disapprove as response,
> > survey (first/second) as a fixed effect, and subject as a random
> > effect.
> >
> > 1) Is it possible to fit such a model directly using "lmer"?
> >
> > or
> >
> > 2) Should I unroll the table above into a dataframe containing also
> > fictitious subject id's? If this is the case, what is a clean way
> > of doing it?
>
>
> Unroll it.
>
> Asking for a "clean" way to do something is a disincentive because it
> implies that you know how to do it but not cleanly. In the future I
> would suggest that you do one of two things
>
> a) post your on dirty version and ask for a cleaner one, or
>
> b) just ask for something that works.
>
> Something like ...
>
> ratings <-
> data.frame(
> response = c(
> rep(c(1,1), 794),
> rep(c(1,0), 150),
> rep(c(0,1), 86),
> rep(c(0,0), 570)),
> time = rep(c(1,2), 1600),
> subject=rep(1:1600, each=2))
>
> test.lmer <- lmer(response ~ time + (1|subject), data=ratings,
> family=binomial)
>
> but I don't know if you think that's clean or not.
>
> Andrew
>
> > Thank you in advance,
> > Giovanni Petris
> >
> > --
> >
> > Giovanni Petris <GPetris_at_uark.edu>
> > Associate Professor
> > Department of Mathematical Sciences
> > University of Arkansas - Fayetteville, AR 72701
> > Ph: (479) 575-6324, 575-8630 (fax)
> > http://definetti.uark.edu/~gpetris/
> >
> > ______________________________________________
> > 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.
>
> --
> Andrew Robinson
> Department of Mathematics and Statistics Tel: +61-3-8344-6410
> University of Melbourne, VIC 3010 Australia Fax: +61-3-8344-4599
> http://www.ms.unimelb.edu.au/~andrewpr
> http://blogs.mbs.edu/fishing-in-the-bay/
>



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 02 May 2008 - 21:43:52 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 02 May 2008 - 22:30:36 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.

list of date sections of archive