# linear mixed models - fixed and random effects

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)

#Model
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!

• 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.
– cauchy
Jun 21, 2015 at 22:56

• 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. Jun 21, 2015 at 23:05