Write a generative model for an ecological process with a single covariate and a random intercept:
A random intercept is appropriate here because each plot has the possibility of having different starting carbon content due to unmeasured variables, like differing microbes, soil type, spatial variability, etc…
In this example, the response variable is normal.
The equation in model form for differing intercepts:
\[y_i \sim Normal(\mu, \sigma)\]
and
\[\mu = \alpha + b_{j[i]}+\beta_{xi}\]
The plot-level random effects are assumed to come from a normal distribution:
\[b_j\sim Normal(0, \sigma^2_{beach})\]
This means that each plot gets its own intercept shift but those shifts are drawn from a shared population distribution centered on zero.
n <- 100
n_plots <- 10
nitrogen <- runif(n, min=0.1, max=0.5)
intercept <- 2
slope <- 1.2
Plot <- as.factor(sample(1:n_plots, size=n, replace=T))
plot_effects <- rnorm(n_plots, mean=0, sd=0.8)
mu_C <- intercept+slope*nitrogen+plot_effects[Plot]
soil_C <- rnorm(n, mean=mu_C, sd=0.15)
data <- data.frame(soil_C, nitrogen, Plot)
ggplot(data, aes(nitrogen, soil_C, color = Plot)) +
geom_smooth(method = "lm", se = FALSE)+
geom_point()+
labs(title="Same slope, different intercepts", x="Soil Nitrogen", y="Soil Carbon")
Write down the model formula for a model with a random slope and describe how the plot would change if you had a random slope, rather than a random intercept.
In the example above where there is a varying intercept, all lines are parallel even though they all cross the intercept in different locations. In this example, the slopes vary but the intercepts change. This means each plot will cross the y-axis at the same location, but they will each have a different slope and the lines will no longer be parallel.
The equation in model form for differing slopes:
Everything should stay the same in the model except the linear model part.
\[\mu = \alpha + (\beta+b_{j[i]})x_i\]
Random slopes modify the effect of the covariate while the random intercept only shifts the intercept. So here with larger slopes, you get a larger effect from nitrogen on the soil carbon.
slope_effects <- rnorm(n_plots, mean=0, sd=1)
mu_C <- intercept + (slope + slope_effects[Plot]) * nitrogen
soil_C <- rnorm(n, mean=mu_C, sd=0.15)
data2 <- data.frame(soil_C, nitrogen, Plot)
ggplot(data2, aes(nitrogen, soil_C, color = Plot)) +
geom_smooth(method = "lm", se = FALSE)+
geom_point()+
labs(title="Same intercept, different slopes", x="Soil Nitrogen", y="Soil Carbon")
Generative Model
\[y_i \sim Poisson(\lambda_i)\]
and
\[log(\lambda_i) = \beta_0+\beta_1x_i+b_{j[i]}\]
where
The beach-level random effects are assumed to come from a normal distribution:
\[b_j \sim Normal(0, \sigma^2_{beach})\]
n=100
n_beaches=10
seagrass_cover<-runif(100)
intercept=5
slope=1.3
beach_id=sample(x=c(1:10),size=100,replace=TRUE)
beach_effects<-rnorm(n=n_beaches,0,sd=2)
turtle_lambda<-exp(intercept+slope*seagrass_cover+beach_effects[beach_id])
turtle_eggs<-rpois(n=100,lambda=turtle_lambda)
plot(turtle_eggs~seagrass_cover,pch=19,col=beach_id)