The statistical model:
\(y_t = \beta_0 + \beta_1 * (Elevation_s)_t + \beta_2 * Slope_t + (b_s)_t + \epsilon_t\)
Where:
Let’s define the parameters:
nstand = 5
nplot = 4
b0 = -1
b1 = .005
b2 = .1
sds = 2
sd = 1
Simulate other variables:
set.seed(16)
stand = rep(LETTERS[1:nstand], each = nplot)
standeff = rep( rnorm(nstand, 0, sds), each = nplot)
ploteff = rnorm(nstand*nplot, 0, sd)
Simulate elevation and slope:
elevation = rep( runif(nstand, 1000, 1500), each = nplot)
slope = runif(nstand*nplot, 2, 75)
Simulate response variable:
resp2 = b0 + b1*elevation + b2*slope + standeff + ploteff
Your tasks (complete each task in its’ own code chunk, make sure to use echo=TRUE so I can see your code):
library(lme4)
library(lmerTest)
m1 <- lmer(resp2 ~ 1 + elevation + slope + (1|stand))
summary(m1)
cat("b0 = ", b0, sep = "")
cat("b1 = ", b1, sep = "")
cat("b2 = ", b2, sep = "")
### b0 = -1, b1 = 0.005, b2 = 0.1
### b1 = 0.005 is less than intial parameter. b2 = 0.1 is similar to intial parameter.
lmm_simul <- function(nstand = 5, nplot = 4, b0 = -1, b1 = 0.005, b2 = 0.1, sds = 2, sd = 1) {
stand <- rep(LETTERS[1:nstand], each = nplot)
standeff <- rep(rnorm(nstand, 0, sds), each = nplot)
ploteff <- rnorm(nstand * nplot, 0, sd)
elevation <- rep(runif(nstand, 1000, 1500), each = nplot)
slope <- runif(nstand * nplot, 2, 75)
resp2 <- b0 + b1 * elevation + b2 * slope + standeff + ploteff
dat <- data.frame(resp2, elevation, slope, stand)
lmer(resp2 ~ 1 + elevation + slope + (1|stand), data = dat)
}
lmm_simul()
sim_result <- replicate(n = 1000, expr = lmm_simul())
length(sim_result)
library(tidyverse)
library(broom)
library(broom.mixed)
variances = sim_result %>% map_dfr(tidy, effects = "ran_pars", scales = "vcov")
variances %>% print(n = 6)
library(ggplot2)
library(furrr)
simul_result_list <- c(5, 30, 100) %>%
set_names(c("size_5", "size_30", "size_100")) %>%
future_map(function(.size) replicate(n = 1000, expr = lmm_simul(nstand = .size)))
simul_result_stand_df <- simul_result_list %>%
modify_depth(.depth = 2, function(x) tidy(x, effects = "ran_pars", scales = "vcov")) %>%
map_dfr(bind_rows, .id = "id") %>%
filter(group == "stand")
simul_result_stand_df %>%
mutate(
id = case_when(
id == "size_5" ~ "size = 5",
id == "size_30" ~ "size = 30",
id == "size_100" ~ "size = 100"
)
) %>%
ggplot(aes(x = estimate) ) +
geom_density(fill = "blue", alpha = .25) +
facet_wrap(~ id) +
geom_vline(xintercept = 4) +
theme_bw()
### Based on the graph, the variance of stand is 4. The larger the sample size is, the more accurate the result is.
simul_estimates <- simul_result %>%
future_map(tidy, effects = "fixed") %>%
bind_rows()
simul_estimates
simul_estimates %>%
dplyr::filter(term %in% c("elevation", "slope")) %>%
group_by(term) %>%
mutate(x = 1:1000) %>%
ungroup() %>%
mutate(real_value = ifelse(term == "elevation", 0.005, 0.1)) %>%
ggplot(aes(x = x, y = estimate)) +
geom_line() +
facet_wrap(~ term) +
geom_hline(aes(yintercept = real_value, color = term), linetype = 2, size = 0.8) +
theme_bw()
### b1 and b2 fluctuate round the real value.