I am analyzing plant Functional Diversity as function of environmental variables. The nature of my data is hierarchical, I have a variable x measured at plant scale, and another one, z, measured at Site scale. Therefore, I chose to use Mixed Models to account for spatial structure.

My doubts are related to the structure of the model. I can't figure out whether adding x and z as fixed effect and Site as random effect is correct or not. I have a unique value of z for each Site.

Here a synthetic example in R:

sites <- as.factor(c("A","B","C","D","E"))
n_sites <- length(sites)
n_samples <- 100
n_obs <- n_sites*n_sample

#Response variable
y <- runif(n_obs, 0, 1)

#Continuous fixed predictors
x <- y + runif(n_obs, -0.1, 0.5)
z <- rep(runif(n_sites,0,1),each=n_samples)

#Categorical random variable
group <- rep(sites,n_samples)

lmer( y ~ z + x + ( x | group ) )

Group affects the difference in y when x=0 as well as the rate at which y is affected by x.

Using this model structure, is z correctly explaining regional scale variance (among sites) and x the variance within sites?

Is it redundant / incorrect to add as fixed factor a variable (z) having the same number of observations of the random factor (group)?

I would greatly appreciate any advice aimed at solving my doubts!

  • $\begingroup$ Note that your code doesn't run as-is at the moment. You define "n_samples" but then use "n_sample" throughout the rest of the code. Just make it consistent so it runs. $\endgroup$
    – cauchy
    Jun 21, 2015 at 22:56

1 Answer 1


No, z is not redundant. In much of the hierarchical modeling literature (e.g., Gelman and Hill), z is known as a group-level predictor. In the model as you've specified it, z predicts variation across groups (the varying intercepts).

If you expect the varying effect of x across groups to be explained by z, you can include an interaction between x and z, but it sounds like you're only interested in using z to predict the site-level variation, so your model is correct as it is now.

  • $\begingroup$ To be honest, I'm not at all sure about the random effect of x. In the question it doesn't indicate that its effect is to vary across groups. Therefore, I'm not sure if it is possible (using the information given in the question) to label this model as "correct" or not. $\endgroup$
    – SimonG
    Jun 21, 2015 at 23:05
  • $\begingroup$ @ cauchy: thanks for the reply. Do you mean that adding an interaction term between x and z, it will explain the variation in the slope of x across sites? $\endgroup$
    – Mattma
    Jun 21, 2015 at 23:42
  • $\begingroup$ @ SimonG: I edited the question: "Group affects the difference in y when x=0 as well as the rate at which y is affected by x." $\endgroup$
    – Mattma
    Jun 22, 2015 at 0:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.