Jason R. Finley
2009-Aug-19 01:47 UTC
[R] lmer with random slopes for 2 or more first-level factors?
I have data from a design in which items are completely nested within subjects. Subject is the only second-level factor, but I have multiple first-level factors (IVs). Say there are 2 such independent variables that I am interested in. What is the proper syntax to fit a mixed-effects model with a by-subject random intercept, and by-subject random slopes for both the 2 IVs? I can think of at least two possibilities: lmer(DV ~ IV1 + IV2 + (1 + IV1 + IV2 | Subject)) lmer(DV ~ IV1 + IV2 + (1 + IV1 | Subject) + (1 + IV2 | Subject)) Or maybe there is some other way to do it? Maybe the correct syntax depends on whether the random effect of subjects on the intercept and slopes are correlated or not? (If so, how do I proceed?) Finally, what would be the syntax if I wanted to include a random subject effect for the INTERACTION of IV1 and IV2? Thanks very much, ~jason PS: additional search terms: multi-level linear model, MLM, hierarchical, repeated measures ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Jason R. Finley Department of Psychology University of Illinois, Urbana-Champaign ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ONKELINX, Thierry
2009-Aug-19 08:47 UTC
[R] lmer with random slopes for 2 or more first-level factors?
Dear Jason, Both models have a correct syntax. Although I would write the last model rather as lmer(DV ~ IV1 + IV2 + (1|Subject) + (IV1 - 1| Subject) + (IV2 - 1| Subject)) The only difference is indeed the correlation between the random effects. The random effects in the model I wrote are all independent (not correlated). In your first model all random effects are correlated. In your second model the first random intercept is correlated with the random slope of IV1. The second random intercept with the random slope of IV2. Depending on if you want your interaction to be indepentend or not your can use either lmer(DV ~ IV1 + IV2 + (IV1 * IV2 | Subject)) lmer(DV ~ IV1 + IV2 + (1 | Subject) + (IV1 -1 | Subject) + (IV2 - 1| Subject) + (IV1:IV2 - 1| Subject)) Note that IV1 * IV2 is equivalent to 1 + IV1 + IV2 + IV1:IV2 HTH, Thierry PS R-Sig-mixed-models is a better list for this kind of questions. ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx at inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens Jason R. Finley Verzonden: woensdag 19 augustus 2009 3:47 Aan: r-help at r-project.org Onderwerp: [R] lmer with random slopes for 2 or more first-level factors? I have data from a design in which items are completely nested within subjects. Subject is the only second-level factor, but I have multiple first-level factors (IVs). Say there are 2 such independent variables that I am interested in. What is the proper syntax to fit a mixed-effects model with a by-subject random intercept, and by-subject random slopes for both the 2 IVs? I can think of at least two possibilities: lmer(DV ~ IV1 + IV2 + (1 + IV1 + IV2 | Subject)) lmer(DV ~ IV1 + IV2 + (1 + IV1 | Subject) + (1 + IV2 | Subject)) Or maybe there is some other way to do it? Maybe the correct syntax depends on whether the random effect of subjects on the intercept and slopes are correlated or not? (If so, how do I proceed?) Finally, what would be the syntax if I wanted to include a random subject effect for the INTERACTION of IV1 and IV2? Thanks very much, ~jason PS: additional search terms: multi-level linear model, MLM, hierarchical, repeated measures ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Jason R. Finley Department of Psychology University of Illinois, Urbana-Champaign ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ______________________________________________ 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. Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document. The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.
Jonathan Baron
2009-Aug-19 17:52 UTC
[R] lmer with random slopes for 2 or more first-level factors?
Since nobody has answered yet, let me try. I'm not 100% sure. (So why bother? To check my own understanding.) On 08/18/09 20:47, Jason R. Finley wrote:> I have data from a design in which items are completely nested within > subjects. Subject is the only second-level factor, but I have > multiple first-level factors (IVs). Say there are 2 such independent > variables that I am interested in. What is the proper syntax to fit a > mixed-effects model with a by-subject random intercept, and by-subject > random slopes for both the 2 IVs? > > I can think of at least two possibilities: > > lmer(DV ~ IV1 + IV2 + (1 + IV1 + IV2 | Subject)) > > lmer(DV ~ IV1 + IV2 + (1 + IV1 | Subject) + (1 + IV2 | Subject)) > > Or maybe there is some other way to do it? Maybe the correct syntax > depends on whether the random effect of subjects on the intercept and > slopes are correlated or not? (If so, how do I proceed?)I think that the first way assumes correlations of IV1, IV2, and the Subject intercept. The second way assumes that each of IV1 and IV2 is correlated with the Subject intercept, and the intercept is computed separately for IV1 and IV2. There are other ways. You can do, for example, lmer(DV ~ IV1 + IV2 + (1 | Subject) + (1 | Subject)) + (0 + IV1 | Subject) + (0 + IV2| Subject)) This assumes, I think, that intercepts and slopes are uncorrelated. If you can tolerate that assumption, an advantage of doing it this way is that you can use mcmc sampling, and thus you can use pvals.fnc() in the languageR package. (What I do not know are the consequences of making this assumption if it is false.)> Finally, what would be the syntax if I wanted to include a random > subject effect for the INTERACTION of IV1 and IV2?lmer(DV ~ IV1 + IV2 + (1 + IV1 * IV2 | Subject)) -- Jonathan Baron, Professor of Psychology, University of Pennsylvania Home page: http://www.sas.upenn.edu/~baron