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 First approve disapprove approve 794 150 disapprove 86 570 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.