FrAiello
FrAiello

Reputation: 93

How to include nested effects in mixed effect model with lme4 package

I am studying mixed models and have a doubt about nested random effects..

In the example, we tested subjects variable X and outcome Y and want to see if X is correlated with Y. The data were collected on 8 different mountains in 3 different site (i.e. mountain A was collected on site 1 2 and 3, mountain B site 1 2 and 3, etc). Site 1 of mountain A is not related at all with site 1 of mountain B. Then we want to analyse the relationship Y ~ X controlling for mountain and site.

Since that site and mountain are nested, we created a variable "sample" that is

sample <- factor(mountainRange:site)*

Now, here is my doubt. The course suggest to do the analysis as following

mixed.lmer2 <- lmer(Y~ X + (1|mountain) + (1|sample), data = data)

But I don't understand why we include (1|mountain). I thought I don't need to include it as it is already included in the variable (1|sample) What I mean is that I would run the analysis as following:

mixed.lmer3 <- lmer(Y~ X + (1|sample), data = data)

Could anyone explain why am I wrong? Thanks

Upvotes: 2

Views: 5375

Answers (1)

Dylan_Gomes
Dylan_Gomes

Reputation: 2280

You simply embedded mountain and site within the same term sample, which makes it nested. Alternatively, you could have done

mixed.lmer2 <- lmer(Y~ X + (1|mountain/site), data = data)

OR

mixed.lmer2 <- lmer(Y~ X + (1|mountain) + (1|mountain:site), data = data)

to represent the same thing. Again, you just did it outside the model formula.

Do see https://stats.stackexchange.com/q/228800/238878 for a more thorough answer.

Upvotes: 1

Related Questions