library(tidyverse)
library(broom)
library(table1)
library(GGally)
library(car)
library(easystats)
library(gt)
library(janitor)
library(lmtest)
library(datawizard)
library(marginaleffects)
library(modelsummary)
library(broom.mixed)
library(lmerTest)
library(faux)
library(arm)obs_per_cluster <- 20
n_cluster <- 30
# Fixed intercept
alpha_0 <- 2
# Fixed slope
beta_0 <- 1.5
# Residuals st.dev (level 1)
sig <- 1
# Random intercept st.dev (level 2)
sig_u <- 1.5
# Random slope st.dev (level 2)
sig_v <- 1.8
# Independent random effects
cor <- 0.5
# Create a dataset that can be reproduced
set.seed(888)
# step 1: create the units
df <- add_random(
cluster = n_cluster, # first the clusters
obs = obs_per_cluster # second, the observations
)
# Print the result
# try to figure out what just happened
df |>
print(n = 15)## # A tibble: 600 × 2
## cluster obs
## <chr> <chr>
## 1 cluster01 obs01
## 2 cluster01 obs02
## 3 cluster01 obs03
## 4 cluster01 obs04
## 5 cluster01 obs05
## 6 cluster01 obs06
## 7 cluster01 obs07
## 8 cluster01 obs08
## 9 cluster01 obs09
## 10 cluster01 obs10
## 11 cluster01 obs11
## 12 cluster01 obs12
## 13 cluster01 obs13
## 14 cluster01 obs14
## 15 cluster01 obs15
## # ℹ 585 more rows
# step 2: Add anything at level 2
# Here we have random intercept/slope
df <- df |>
add_ranef(
"cluster",
u_i = sig_u,
v_i = sig_v,
.cors = cor
) |> mutate(
a_i = alpha_0 + u_i,
b_i = beta_0 + v_i
)
# Print the result
# try to figure out what just happened
df |>
print(n = 15)## # A tibble: 600 × 6
## cluster obs u_i v_i a_i b_i
## <chr> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 cluster01 obs01 -3.82 -2.20 -1.82 -0.700
## 2 cluster01 obs02 -3.82 -2.20 -1.82 -0.700
## 3 cluster01 obs03 -3.82 -2.20 -1.82 -0.700
## 4 cluster01 obs04 -3.82 -2.20 -1.82 -0.700
## 5 cluster01 obs05 -3.82 -2.20 -1.82 -0.700
## 6 cluster01 obs06 -3.82 -2.20 -1.82 -0.700
## 7 cluster01 obs07 -3.82 -2.20 -1.82 -0.700
## 8 cluster01 obs08 -3.82 -2.20 -1.82 -0.700
## 9 cluster01 obs09 -3.82 -2.20 -1.82 -0.700
## 10 cluster01 obs10 -3.82 -2.20 -1.82 -0.700
## 11 cluster01 obs11 -3.82 -2.20 -1.82 -0.700
## 12 cluster01 obs12 -3.82 -2.20 -1.82 -0.700
## 13 cluster01 obs13 -3.82 -2.20 -1.82 -0.700
## 14 cluster01 obs14 -3.82 -2.20 -1.82 -0.700
## 15 cluster01 obs15 -3.82 -2.20 -1.82 -0.700
## # ℹ 585 more rows
# step 3: add anything at level 1
df <- df |>
mutate(
x = rnorm(n_cluster * obs_per_cluster),
epsilon = rnorm(n_cluster * obs_per_cluster, 0, sig),
y = a_i + b_i * x + epsilon
)
# Print the result
# try to figure out what just happened
df |>
print(n = 20)## # A tibble: 600 × 9
## cluster obs u_i v_i a_i b_i x epsilon y
## <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 cluster01 obs01 -3.82 -2.20 -1.82 -0.700 -0.456 0.136 -1.37
## 2 cluster01 obs02 -3.82 -2.20 -1.82 -0.700 0.0190 -0.262 -2.10
## 3 cluster01 obs03 -3.82 -2.20 -1.82 -0.700 -0.318 0.211 -1.39
## 4 cluster01 obs04 -3.82 -2.20 -1.82 -0.700 -0.550 0.239 -1.20
## 5 cluster01 obs05 -3.82 -2.20 -1.82 -0.700 1.47 1.02 -1.83
## 6 cluster01 obs06 -3.82 -2.20 -1.82 -0.700 -0.287 0.527 -1.09
## 7 cluster01 obs07 -3.82 -2.20 -1.82 -0.700 0.422 0.145 -1.97
## 8 cluster01 obs08 -3.82 -2.20 -1.82 -0.700 -0.0691 0.881 -0.891
## 9 cluster01 obs09 -3.82 -2.20 -1.82 -0.700 -1.76 -0.238 -0.826
## 10 cluster01 obs10 -3.82 -2.20 -1.82 -0.700 -2.52 0.748 0.692
## 11 cluster01 obs11 -3.82 -2.20 -1.82 -0.700 1.36 -1.41 -4.18
## 12 cluster01 obs12 -3.82 -2.20 -1.82 -0.700 0.445 -0.679 -2.81
## 13 cluster01 obs13 -3.82 -2.20 -1.82 -0.700 -0.868 -0.583 -1.80
## 14 cluster01 obs14 -3.82 -2.20 -1.82 -0.700 0.760 -2.15 -4.50
## 15 cluster01 obs15 -3.82 -2.20 -1.82 -0.700 -1.04 0.500 -0.596
## 16 cluster01 obs16 -3.82 -2.20 -1.82 -0.700 -0.187 -1.91 -3.60
## 17 cluster01 obs17 -3.82 -2.20 -1.82 -0.700 0.404 1.54 -0.561
## 18 cluster01 obs18 -3.82 -2.20 -1.82 -0.700 1.27 0.687 -2.02
## 19 cluster01 obs19 -3.82 -2.20 -1.82 -0.700 -1.25 0.868 -0.0797
## 20 cluster01 obs20 -3.82 -2.20 -1.82 -0.700 0.164 -0.675 -2.61
## # ℹ 580 more rows
# For the models, we really only need
# the cluster id, the y and the x
df <- df %>%
dplyr::select(cluster, x, y)
# Print the result
# try to figure out what just happened
df |>
print(n = 20)## # A tibble: 600 × 3
## cluster x y
## <chr> <dbl> <dbl>
## 1 cluster01 -0.456 -1.37
## 2 cluster01 0.0190 -2.10
## 3 cluster01 -0.318 -1.39
## 4 cluster01 -0.550 -1.20
## 5 cluster01 1.47 -1.83
## 6 cluster01 -0.287 -1.09
## 7 cluster01 0.422 -1.97
## 8 cluster01 -0.0691 -0.891
## 9 cluster01 -1.76 -0.826
## 10 cluster01 -2.52 0.692
## 11 cluster01 1.36 -4.18
## 12 cluster01 0.445 -2.81
## 13 cluster01 -0.868 -1.80
## 14 cluster01 0.760 -4.50
## 15 cluster01 -1.04 -0.596
## 16 cluster01 -0.187 -3.60
## 17 cluster01 0.404 -0.561
## 18 cluster01 1.27 -2.02
## 19 cluster01 -1.25 -0.0797
## 20 cluster01 0.164 -2.61
## # ℹ 580 more rows
## [1] 600 3
library(Matrix)
library(lme4)
# Complete pooling model (assuming no group-specific effects)
mod_complete_pooling = lm(y ~ x,df)
# No pooling
## with an interaction
mod_no_pooling_inter = lm(y ~ x + cluster, df)
## without an interaction
mod_no_pooling_wo_inter = lm(y ~ x + cluster + x:cluster, df)
# Multileve model (partial pooling)
## random intercept model
mod_partial_pooling_intercept = lmer(y ~ x + (1 | cluster), df)
## random intercept and slope model
mod_partial_pooling_intercept_slope = lmer(y ~ x + (x | cluster), df)
# Summary of the model
modelsummary(list("COMPLETE POOLING"=mod_complete_pooling,
"NO\nPOLLING"=mod_no_pooling_inter,
"NO POLLING+\nINTERACTION"=mod_no_pooling_wo_inter,
"MULTILEVEL\nMODEL"=mod_partial_pooling_intercept,
"MULTILEVEL\nMODEL +\nRANDOM\nSLOPES"=mod_partial_pooling_intercept_slope
),
star=TRUE)| COMPLETE POOLING | NO POLLING | NO POLLING+ INTERACTION | MULTILEVEL MODEL | MULTILEVEL MODEL + RANDOM SLOPES | |
|---|---|---|---|---|---|
| (Intercept) | 1.844*** | -1.557*** | -1.868*** | 1.844*** | 1.896*** |
| (0.108) | (0.463) | (0.232) | (0.317) | (0.321) | |
| x | 1.184*** | 1.196*** | -0.883*** | 1.195*** | 1.374*** |
| (0.115) | (0.092) | (0.231) | (0.092) | (0.366) | |
| clustercluster02 | 2.940*** | 3.252*** | |||
| (0.654) | (0.327) | ||||
| clustercluster03 | 3.945*** | 4.338*** | |||
| (0.654) | (0.327) | ||||
| clustercluster04 | 1.937** | 2.253*** | |||
| (0.654) | (0.327) | ||||
| clustercluster05 | 0.384 | 1.710*** | |||
| (0.656) | (0.345) | ||||
| clustercluster06 | 4.384*** | 5.100*** | |||
| (0.655) | (0.334) | ||||
| clustercluster07 | 0.143 | 0.945** | |||
| (0.655) | (0.330) | ||||
| clustercluster08 | 4.088*** | 3.751*** | |||
| (0.656) | (0.339) | ||||
| clustercluster09 | 4.348*** | 4.952*** | |||
| (0.654) | (0.333) | ||||
| clustercluster10 | 4.774*** | 4.695*** | |||
| (0.654) | (0.329) | ||||
| clustercluster11 | 3.690*** | 4.125*** | |||
| (0.654) | (0.328) | ||||
| clustercluster12 | 5.212*** | 5.548*** | |||
| (0.654) | (0.327) | ||||
| clustercluster13 | 5.445*** | 6.117*** | |||
| (0.654) | (0.335) | ||||
| clustercluster14 | 6.087*** | 6.425*** | |||
| (0.655) | (0.331) | ||||
| clustercluster15 | 3.042*** | 3.387*** | |||
| (0.654) | (0.327) | ||||
| clustercluster16 | 3.037*** | 2.825*** | |||
| (0.654) | (0.333) | ||||
| clustercluster17 | 7.059*** | 7.378*** | |||
| (0.654) | (0.327) | ||||
| clustercluster18 | 1.405* | 1.692*** | |||
| (0.654) | (0.327) | ||||
| clustercluster19 | 1.503* | 1.234*** | |||
| (0.654) | (0.329) | ||||
| clustercluster20 | 3.150*** | 3.581*** | |||
| (0.656) | (0.348) | ||||
| clustercluster21 | 2.420*** | 2.921*** | |||
| (0.654) | (0.333) | ||||
| clustercluster22 | 3.509*** | 3.827*** | |||
| (0.654) | (0.327) | ||||
| clustercluster23 | 2.986*** | 3.140*** | |||
| (0.654) | (0.328) | ||||
| clustercluster24 | 4.555*** | 4.975*** | |||
| (0.654) | (0.327) | ||||
| clustercluster25 | 1.664* | 2.176*** | |||
| (0.654) | (0.337) | ||||
| clustercluster26 | 3.352*** | 3.681*** | |||
| (0.654) | (0.328) | ||||
| clustercluster27 | 2.846*** | 3.424*** | |||
| (0.654) | (0.342) | ||||
| clustercluster28 | 5.431*** | 6.551*** | |||
| (0.654) | (0.330) | ||||
| clustercluster29 | 3.532*** | 3.544*** | |||
| (0.655) | (0.331) | ||||
| clustercluster30 | 5.168*** | 5.409*** | |||
| (0.654) | (0.335) | ||||
| x × clustercluster02 | -0.957** | ||||
| (0.311) | |||||
| x × clustercluster03 | 3.590*** | ||||
| (0.319) | |||||
| x × clustercluster04 | 2.395*** | ||||
| (0.351) | |||||
| x × clustercluster05 | -0.702+ | ||||
| (0.378) | |||||
| x × clustercluster06 | 0.890** | ||||
| (0.303) | |||||
| x × clustercluster07 | -0.879* | ||||
| (0.360) | |||||
| x × clustercluster08 | 3.893*** | ||||
| (0.338) | |||||
| x × clustercluster09 | 3.448*** | ||||
| (0.369) | |||||
| x × clustercluster10 | 4.814*** | ||||
| (0.344) | |||||
| x × clustercluster11 | 3.341*** | ||||
| (0.317) | |||||
| x × clustercluster12 | 4.665*** | ||||
| (0.364) | |||||
| x × clustercluster13 | 3.128*** | ||||
| (0.313) | |||||
| x × clustercluster14 | 1.969*** | ||||
| (0.315) | |||||
| x × clustercluster15 | 1.250*** | ||||
| (0.304) | |||||
| x × clustercluster16 | -0.459 | ||||
| (0.391) | |||||
| x × clustercluster17 | 3.436*** | ||||
| (0.327) | |||||
| x × clustercluster18 | 3.263*** | ||||
| (0.385) | |||||
| x × clustercluster19 | -1.156*** | ||||
| (0.299) | |||||
| x × clustercluster20 | 1.800*** | ||||
| (0.360) | |||||
| x × clustercluster21 | 2.795*** | ||||
| (0.332) | |||||
| x × clustercluster22 | 1.738*** | ||||
| (0.354) | |||||
| x × clustercluster23 | 0.587 | ||||
| (0.368) | |||||
| x × clustercluster24 | 3.730*** | ||||
| (0.347) | |||||
| x × clustercluster25 | 3.221*** | ||||
| (0.516) | |||||
| x × clustercluster26 | 2.277*** | ||||
| (0.335) | |||||
| x × clustercluster27 | 3.044*** | ||||
| (0.435) | |||||
| x × clustercluster28 | 7.722*** | ||||
| (0.397) | |||||
| x × clustercluster29 | 3.311*** | ||||
| (0.320) | |||||
| x × clustercluster30 | 1.724*** | ||||
| (0.428) | |||||
| SD (Intercept cluster) | 1.672 | 1.744 | |||
| SD (x cluster) | 1.988 | ||||
| Cor (Intercept~x cluster) | 0.616 | ||||
| SD (Observations) | 2.068 | 1.028 | |||
| Num.Obs. | 600 | 600 | 600 | 600 | 600 |
| R2 | 0.151 | 0.506 | 0.884 | ||
| R2 Adj. | 0.150 | 0.480 | 0.871 | ||
| R2 Marg. | 0.152 | 0.181 | |||
| R2 Cond. | 0.487 | 0.886 | |||
| AIC | 2873.1 | 2606.6 | 1794.9 | 2663.2 | 1978.3 |
| BIC | 2886.3 | 2747.3 | 2063.1 | 2680.8 | 2004.6 |
| ICC | 0.4 | 0.9 | |||
| Log.Lik. | -1433.550 | -1271.312 | -836.443 | ||
| F | 106.498 | 19.407 | 69.756 | ||
| RMSE | 2.64 | 2.01 | 0.98 | 2.02 | 0.98 |
| + p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001 | |||||
The intraclass correlation coefficient (ICC) is a statistical measure used to assess the reliability or consistency of measurements made by different observers, raters, or methods. It is particularly relevant in the context of reliability analysis for assessing the consistency of measurements or judgments made on the same set of subjects or objects. The ICC ranges from 0 to 1, where: 0 indicates no agreement or reliability. 1 indicates perfect agreement or reliability. In general, higher ICC values suggest better reliability. Researchers often use ICC to evaluate the extent to which measurements can be trusted and to determine the appropriateness of using certain measures in their studies
In this case, ICC increase from 0.4 to 0.9 from multilevel model to
multilevel model + random slopes. Since ICC measures the proportion of
total variance in a dependent variable that is due to between-group
variance relative to the total variance. This could be
• Improved Model Fit: Random slopes provide a more flexible model that
allows for variations in the slopes across different groups or
individuals. This increased flexibility can lead to a better fit of the
model to the observed data, which may result in a higher ICC.
• Capturing More Complexity: Random slopes introduce additional
parameters that can capture more complexity in the data. If the data
indeed show substantial variability at the individual level, the model
with random slopes may be better equipped to capture this variability,
leading to an increased ICC.
• Reduced Residual Variance: By accounting for more sources of
variability through random slopes, the residual variance (unexplained
variance) may be reduced. As ICC is calculated as the ratio of
betweengroup variance to total variance, a decrease in residual variance
can contribute to an increase in the ICC.
In the model summary, multilevel model + random slopes shows the lowest
AIC and BIC, should be the optimal model for this simulated data (30
clusters, 20 observations per cluster).
# Caterpillar plot Multileve Random Intercept
## First, we need the list of clusters
clusters <- df |>
pull(cluster) |>
unique()
mdl_multilevel <- lmer(y ~ x + (x | cluster), df)
## predictions of y for each cluster at x = 0
predict_y <- mdl_multilevel |>
ranef() |>
# Used the broom.mixed::augment function to calculate the confidence intervals
augment() |>
# convert to a tibble
as_tibble() |>
# Include only the random intercept
filter(variable == "(Intercept)") |>
# Now we order the results by ascending estimates
arrange(estimate) |>
mutate(cluster = fct_inorder(level))
## Finally, let's plot our caterpillar!
predict_y |>
ggplot(aes(xmin = -4, xmax = 4, y = cluster, x = estimate)) +
geom_pointrange(aes(xmin = lb, xmax = ub)) +
theme(plot.background = element_rect(fill = "white"))+ # Change the background color
labs(x = "Estimate of the intercept (Y_hat when X=0)", y = NULL,
title = "Random effects caterpillar plot (Intercepts)")# lattice plot
## random intercept
predictions(mdl_multilevel) |>
ggplot(xmin = -2, xmax = 2, aes(x=x, y=y)) +
geom_point() +
geom_line(aes(y = estimate)) +
labs(title = "Simulated data: lattice plot Random intercept")+
# Use facets to create the lattice plot
facet_wrap(~cluster)Caterpillar plots and lattice plots are two visualization techniques used in statistical modeling to provide insights into the estimated parameters of different groups or levels within a model. Caterpillar plots display individual estimates for each group or level (in this case, cluster), offering a clear visual representation of variability between clusters. Error bars or confidence intervals around estimates can be easily incorporated, providing information about the precision of the estimates. However, since caterpillar plots can visualizing one parameter at a time, if there are a large number of groups, the plot can become cluttered, making it challenging to interpret. This can be covered by lattice plot, which can handle multiple parameters simultaneously, providing a comprehensive view of the relationships between different factors.
radon <- read.table ("srrs2.dat", header=T, sep=",")
mn <- radon$state=="MN"
radon_clean <- radon[mn,] |>
mutate(county = fct_drop(county))
radon_clean$room <- as.factor(radon_clean$room)
radon_clean$region <- as.factor(radon_clean$region)
radon_clean$floor <- as.factor(radon_clean$floor)
radon_clean$activity <- as.numeric(radon_clean$activity)
radon_clean$log.radon <- log(ifelse(radon_clean$activity == 0, 0.1,
radon_clean$activity))
county.name <- as.vector(radon_clean$county)
uniq <- unique(county.name)
J <- length(uniq)
county <- rep (NA, J)
for (i in 1:J){
county[county.name==uniq[i]] <- i
}table1( ~ activity + log.radon + county + wave + rep +
basement + room + region | floor, data = radon_clean)| 0 (N=766) |
1 (N=153) |
Overall (N=919) |
|
|---|---|---|---|
| activity | |||
| Mean (SD) | 5.07 (4.54) | 3.28 (3.83) | 4.77 (4.48) |
| Median [Min, Max] | 3.90 [0.100, 48.2] | 2.10 [0, 25.4] | 3.60 [0, 48.2] |
| log.radon | |||
| Mean (SD) | 1.33 (0.783) | 0.713 (0.999) | 1.22 (0.853) |
| Median [Min, Max] | 1.36 [-2.30, 3.88] | 0.742 [-2.30, 3.23] | 1.28 [-2.30, 3.88] |
| county | |||
| AITKIN | 3 (0.4%) | 1 (0.7%) | 4 (0.4%) |
| ANOKA | 49 (6.4%) | 3 (2.0%) | 52 (5.7%) |
| BECKER | 1 (0.1%) | 2 (1.3%) | 3 (0.3%) |
| BELTRAMI | 3 (0.4%) | 4 (2.6%) | 7 (0.8%) |
| BENTON | 3 (0.4%) | 1 (0.7%) | 4 (0.4%) |
| BIG STONE | 3 (0.4%) | 0 (0%) | 3 (0.3%) |
| BLUE EARTH | 12 (1.6%) | 2 (1.3%) | 14 (1.5%) |
| BROWN | 2 (0.3%) | 2 (1.3%) | 4 (0.4%) |
| CARLTON | 9 (1.2%) | 1 (0.7%) | 10 (1.1%) |
| CARVER | 3 (0.4%) | 3 (2.0%) | 6 (0.7%) |
| CASS | 5 (0.7%) | 0 (0%) | 5 (0.5%) |
| CHIPPEWA | 4 (0.5%) | 0 (0%) | 4 (0.4%) |
| CHISAGO | 6 (0.8%) | 0 (0%) | 6 (0.7%) |
| CLAY | 10 (1.3%) | 4 (2.6%) | 14 (1.5%) |
| CLEARWATER | 2 (0.3%) | 2 (1.3%) | 4 (0.4%) |
| COOK | 2 (0.3%) | 0 (0%) | 2 (0.2%) |
| COTTONWOOD | 1 (0.1%) | 3 (2.0%) | 4 (0.4%) |
| CROW WING | 9 (1.2%) | 3 (2.0%) | 12 (1.3%) |
| DAKOTA | 59 (7.7%) | 4 (2.6%) | 63 (6.9%) |
| DODGE | 3 (0.4%) | 0 (0%) | 3 (0.3%) |
| DOUGLAS | 8 (1.0%) | 1 (0.7%) | 9 (1.0%) |
| FARIBAULT | 5 (0.7%) | 1 (0.7%) | 6 (0.7%) |
| FILLMORE | 1 (0.1%) | 1 (0.7%) | 2 (0.2%) |
| FREEBORN | 7 (0.9%) | 2 (1.3%) | 9 (1.0%) |
| GOODHUE | 12 (1.6%) | 2 (1.3%) | 14 (1.5%) |
| HENNEPIN | 94 (12.3%) | 11 (7.2%) | 105 (11.4%) |
| HOUSTON | 4 (0.5%) | 2 (1.3%) | 6 (0.7%) |
| HUBBARD | 2 (0.3%) | 3 (2.0%) | 5 (0.5%) |
| ISANTI | 3 (0.4%) | 0 (0%) | 3 (0.3%) |
| ITASCA | 11 (1.4%) | 0 (0%) | 11 (1.2%) |
| JACKSON | 5 (0.7%) | 0 (0%) | 5 (0.5%) |
| KANABEC | 4 (0.5%) | 0 (0%) | 4 (0.4%) |
| KANDIYOHI | 4 (0.5%) | 0 (0%) | 4 (0.4%) |
| KITTSON | 1 (0.1%) | 2 (1.3%) | 3 (0.3%) |
| KOOCHICHING | 3 (0.4%) | 4 (2.6%) | 7 (0.8%) |
| LAC QUI PARLE | 1 (0.1%) | 1 (0.7%) | 2 (0.2%) |
| LAKE | 8 (1.0%) | 1 (0.7%) | 9 (1.0%) |
| LAKE OF THE WOODS | 2 (0.3%) | 2 (1.3%) | 4 (0.4%) |
| LE SUEUR | 4 (0.5%) | 1 (0.7%) | 5 (0.5%) |
| LINCOLN | 3 (0.4%) | 1 (0.7%) | 4 (0.4%) |
| LYON | 7 (0.9%) | 1 (0.7%) | 8 (0.9%) |
| MAHNOMEN | 1 (0.1%) | 0 (0%) | 1 (0.1%) |
| MARSHALL | 4 (0.5%) | 5 (3.3%) | 9 (1.0%) |
| MARTIN | 6 (0.8%) | 1 (0.7%) | 7 (0.8%) |
| MCLEOD | 9 (1.2%) | 4 (2.6%) | 13 (1.4%) |
| MEEKER | 5 (0.7%) | 0 (0%) | 5 (0.5%) |
| MILLE LACS | 1 (0.1%) | 1 (0.7%) | 2 (0.2%) |
| MORRISON | 8 (1.0%) | 1 (0.7%) | 9 (1.0%) |
| MOWER | 11 (1.4%) | 2 (1.3%) | 13 (1.4%) |
| MURRAY | 1 (0.1%) | 0 (0%) | 1 (0.1%) |
| NICOLLET | 4 (0.5%) | 0 (0%) | 4 (0.4%) |
| NOBLES | 3 (0.4%) | 0 (0%) | 3 (0.3%) |
| NORMAN | 2 (0.3%) | 1 (0.7%) | 3 (0.3%) |
| OLMSTED | 20 (2.6%) | 3 (2.0%) | 23 (2.5%) |
| OTTER TAIL | 5 (0.7%) | 3 (2.0%) | 8 (0.9%) |
| PENNINGTON | 1 (0.1%) | 2 (1.3%) | 3 (0.3%) |
| PINE | 5 (0.7%) | 1 (0.7%) | 6 (0.7%) |
| PIPESTONE | 3 (0.4%) | 1 (0.7%) | 4 (0.4%) |
| POLK | 2 (0.3%) | 2 (1.3%) | 4 (0.4%) |
| POPE | 2 (0.3%) | 0 (0%) | 2 (0.2%) |
| RAMSEY | 29 (3.8%) | 3 (2.0%) | 32 (3.5%) |
| REDWOOD | 4 (0.5%) | 1 (0.7%) | 5 (0.5%) |
| RENVILLE | 2 (0.3%) | 1 (0.7%) | 3 (0.3%) |
| RICE | 10 (1.3%) | 1 (0.7%) | 11 (1.2%) |
| ROCK | 2 (0.3%) | 0 (0%) | 2 (0.2%) |
| ROSEAU | 6 (0.8%) | 8 (5.2%) | 14 (1.5%) |
| SCOTT | 9 (1.2%) | 4 (2.6%) | 13 (1.4%) |
| SHERBURNE | 8 (1.0%) | 0 (0%) | 8 (0.9%) |
| SIBLEY | 4 (0.5%) | 0 (0%) | 4 (0.4%) |
| ST LOUIS | 100 (13.1%) | 16 (10.5%) | 116 (12.6%) |
| STEARNS | 21 (2.7%) | 4 (2.6%) | 25 (2.7%) |
| STEELE | 10 (1.3%) | 0 (0%) | 10 (1.1%) |
| STEVENS | 2 (0.3%) | 0 (0%) | 2 (0.2%) |
| SWIFT | 4 (0.5%) | 0 (0%) | 4 (0.4%) |
| TODD | 2 (0.3%) | 1 (0.7%) | 3 (0.3%) |
| TRAVERSE | 3 (0.4%) | 1 (0.7%) | 4 (0.4%) |
| WABASHA | 6 (0.8%) | 1 (0.7%) | 7 (0.8%) |
| WADENA | 3 (0.4%) | 2 (1.3%) | 5 (0.5%) |
| WASECA | 3 (0.4%) | 1 (0.7%) | 4 (0.4%) |
| WASHINGTON | 41 (5.4%) | 5 (3.3%) | 46 (5.0%) |
| WATONWAN | 1 (0.1%) | 2 (1.3%) | 3 (0.3%) |
| WILKIN | 1 (0.1%) | 0 (0%) | 1 (0.1%) |
| WINONA | 10 (1.3%) | 3 (2.0%) | 13 (1.4%) |
| WRIGHT | 12 (1.6%) | 1 (0.7%) | 13 (1.4%) |
| YELLOW MEDICINE | 2 (0.3%) | 0 (0%) | 2 (0.2%) |
| wave | |||
| 1 | 24 (3.1%) | 1 (0.7%) | 25 (2.7%) |
| 2 | 16 (2.1%) | 8 (5.2%) | 24 (2.6%) |
| 3 | 16 (2.1%) | 5 (3.3%) | 21 (2.3%) |
| 4 | 19 (2.5%) | 6 (3.9%) | 25 (2.7%) |
| 21 | 25 (3.3%) | 1 (0.7%) | 26 (2.8%) |
| 22 | 32 (4.2%) | 3 (2.0%) | 35 (3.8%) |
| 23 | 25 (3.3%) | 3 (2.0%) | 28 (3.0%) |
| 24 | 24 (3.1%) | 4 (2.6%) | 28 (3.0%) |
| 25 | 22 (2.9%) | 3 (2.0%) | 25 (2.7%) |
| 26 | 26 (3.4%) | 7 (4.6%) | 33 (3.6%) |
| 27 | 17 (2.2%) | 5 (3.3%) | 22 (2.4%) |
| 28 | 25 (3.3%) | 6 (3.9%) | 31 (3.4%) |
| 29 | 21 (2.7%) | 4 (2.6%) | 25 (2.7%) |
| 30 | 23 (3.0%) | 7 (4.6%) | 30 (3.3%) |
| 31 | 19 (2.5%) | 3 (2.0%) | 22 (2.4%) |
| 32 | 17 (2.2%) | 3 (2.0%) | 20 (2.2%) |
| 33 | 18 (2.3%) | 8 (5.2%) | 26 (2.8%) |
| 34 | 32 (4.2%) | 3 (2.0%) | 35 (3.8%) |
| 35 | 23 (3.0%) | 4 (2.6%) | 27 (2.9%) |
| 36 | 23 (3.0%) | 8 (5.2%) | 31 (3.4%) |
| 37 | 23 (3.0%) | 5 (3.3%) | 28 (3.0%) |
| 38 | 19 (2.5%) | 6 (3.9%) | 25 (2.7%) |
| 39 | 21 (2.7%) | 5 (3.3%) | 26 (2.8%) |
| 40 | 25 (3.3%) | 4 (2.6%) | 29 (3.2%) |
| 41 | 23 (3.0%) | 4 (2.6%) | 27 (2.9%) |
| 42 | 20 (2.6%) | 4 (2.6%) | 24 (2.6%) |
| 43 | 21 (2.7%) | 6 (3.9%) | 27 (2.9%) |
| 44 | 28 (3.7%) | 3 (2.0%) | 31 (3.4%) |
| 45 | 22 (2.9%) | 5 (3.3%) | 27 (2.9%) |
| 46 | 17 (2.2%) | 6 (3.9%) | 23 (2.5%) |
| 47 | 25 (3.3%) | 5 (3.3%) | 30 (3.3%) |
| 48 | 24 (3.1%) | 3 (2.0%) | 27 (2.9%) |
| 49 | 27 (3.5%) | 1 (0.7%) | 28 (3.0%) |
| 50 | 24 (3.1%) | 4 (2.6%) | 28 (3.0%) |
| rep | |||
| 1 | 167 (21.8%) | 27 (17.6%) | 194 (21.1%) |
| 2 | 154 (20.1%) | 42 (27.5%) | 196 (21.3%) |
| 3 | 154 (20.1%) | 25 (16.3%) | 179 (19.5%) |
| 4 | 138 (18.0%) | 31 (20.3%) | 169 (18.4%) |
| 5 | 153 (20.0%) | 28 (18.3%) | 181 (19.7%) |
| basement | |||
| 31 (4.0%) | 10 (6.5%) | 41 (4.5%) | |
| N | 8 (1.0%) | 51 (33.3%) | 59 (6.4%) |
| Y | 727 (94.9%) | 92 (60.1%) | 819 (89.1%) |
| room | |||
| 0 | 49 (6.4%) | 8 (5.2%) | 57 (6.2%) |
| 1 | 29 (3.8%) | 18 (11.8%) | 47 (5.1%) |
| 2 | 188 (24.5%) | 14 (9.2%) | 202 (22.0%) |
| 3 | 12 (1.6%) | 94 (61.4%) | 106 (11.5%) |
| 4 | 422 (55.1%) | 3 (2.0%) | 425 (46.2%) |
| 5 | 11 (1.4%) | 1 (0.7%) | 12 (1.3%) |
| 7 | 55 (7.2%) | 15 (9.8%) | 70 (7.6%) |
| region | |||
| 1 | 110 (14.4%) | 17 (11.1%) | 127 (13.8%) |
| 2 | 105 (13.7%) | 55 (35.9%) | 160 (17.4%) |
| 3 | 272 (35.5%) | 26 (17.0%) | 298 (32.4%) |
| 4 | 120 (15.7%) | 24 (15.7%) | 144 (15.7%) |
| 5 | 159 (20.8%) | 31 (20.3%) | 190 (20.7%) |
ggpairs(radon_clean,
columns = c('activity', 'log.radon',
'room', 'rep',
'basement', 'region', 'floor'),
ggplot2::aes(colour=floor))lm.unpooled <- lm(log.radon ~ floor + county, data = radon_clean)
sample.size <- as.vector (table (radon_clean$county))
sample.size.jittered <- sample.size*exp (runif (J, -.1, .1))
{
par (mfrow=c(1,1), mar=c(5,5,4,2)+.1)
plot (sample.size.jittered, coef(lm.unpooled)[-1], cex.lab=1.5, cex.axis=1.5,
xlab="sample size in county j", ylab=expression (paste
("est. intercept, ", alpha[j], " (no pooling)")),
pch=20, log="x", ylim=c(-1,3.5), yaxt="n", xaxt="n")
axis (1, c(1,3,10,30,100), cex.axis=1.5)
axis (2, seq(0,3), cex.axis=1.5)
for (j in 1:J){
lines (rep(sample.size.jittered[j],2),
coef(lm.unpooled)[j+1] + c(-1,1)*se.coef(lm.unpooled)[j+1], lwd=.5)
}
avg_intercept <- mean(coef(lm.unpooled)[-1])
abline(h=avg_intercept, col="red", lty=2, lwd=1)
}M0 <- lmer(log.radon ~ 1 + (1 | county), data = radon_clean)
M1 <- lmer(log.radon ~ floor + (1 | county), data = radon_clean)
display (M1)## lmer(formula = log.radon ~ floor + (1 | county), data = radon_clean)
## coef.est coef.se
## (Intercept) 1.46 0.05
## floor1 -0.69 0.07
##
## Error terms:
## Groups Name Std.Dev.
## county (Intercept) 0.33
## Residual 0.76
## ---
## number of obs: 919, groups: county, 85
## AIC = 2179.3, DIC = 2156
## deviance = 2163.7
options (digits=2)
a.hat.M1 <- fixef(M1)[1] + ranef(M1)$county
b.hat.M1 <- fixef(M1)[2]
a.se.M1 <- se.coef(M1)$county
lm.pooled <- lm(log.radon ~ floor, data = radon_clean)
{
par (mfrow=c(1,1), mar=c(5,5,4,2)+.1)
plot (sample.size.jittered, t(a.hat.M1), cex.lab=1.5, cex.axis=1.5,
xlab="sample size in county j", ylab=expression (paste
("est. intercept, ", alpha[j], " (multilevel model)")),
pch=20, log="x", ylim=c(.15,2.5), yaxt="n", xaxt="n")
axis (1, c(1,3,10,30,100), cex.axis=1.5)
axis (2, seq(0,3), cex.axis=1.5)
for (j in 1:J){
lines (rep(sample.size.jittered[j],2),
a.hat.M1[j,] + c(-1,1)*a.se.M1[j], lwd=.5, col="black")
}
abline(h=coef(lm.pooled)[1], col="red", lty=2, lwd=1)
}Counties with fewer measurements have more variable estimates and larger standard errors for several reasons:
Limited Sample Size: With fewer measurements, the sample size is limited, resulting in less information about the underlying population. Small sample sizes are more susceptible to random fluctuations, leading to greater variability in estimates.
Greater Sensitivity to Outliers: Individual measurements can have a larger impact on the overall estimate when the sample size is small. Outliers or extreme values in a small dataset can disproportionately influence the estimated parameters, leading to increased variability.
Increased Uncertainty: Smaller sample sizes lead to larger standard errors. Standard errors represent the precision of the estimates, and when based on a limited number of observations, they tend to be larger, indicating greater uncertainty about the true population parameter.
Regarding the comparison between no-pooling analysis (a) and the multilevel model (b):
No-pooling Analysis (a):
Advantage: It allows for estimating county-specific effects without assuming a common structure. Disadvantage: It is more prone to extreme estimates for counties with smaller sample sizes, leading to biased or misleading conclusions about the true population parameters. It does not effectively borrow information across counties.
Multilevel Model (b):
Advantage: It incorporates a hierarchical structure, allowing information to be shared across counties. This reduces the impact of extreme estimates in counties with small sample sizes by borrowing strength from the overall population distribution. Disadvantage: It introduces assumptions about the commonality of effects across counties, which may not be appropriate if there are substantial variations.
In summary, the main advantage of the multilevel model is its ability to provide more robust estimates by sharing information across groups, reducing the impact of small sample sizes. The disadvantage lies in the assumptions made about the commonality of effects, which may not always hold true. The no-pooling analysis, while more flexible, is more susceptible to variability induced by small sample sizes and may lead to less reliable inferences.
# complete pooling, no pooling, multilevel (partial pooling)
mdl_complete_pooling <- radon_clean |>
lm(log.radon ~ floor, data = _)
mdl_no_pooling <- radon_clean |>
lm(log.radon ~ floor + county - 1, data = _)
mdl_multilevel_pooling <- radon_clean |>
lmer(log.radon ~ floor + (floor|county), data = _)
pred_complete_pooling <- mdl_complete_pooling |>
predictions(
newdata = datagrid(
floor = c(0, 1)
)
) |>
as_tibble() |>
dplyr::select(floor, estimate, conf.low, conf.high)
pred_no_pooling <- mdl_no_pooling |>
predictions(
newdata = datagrid(
floor = c(0, 1),
county = c("DOUGLAS ", "LAKE OF THE WOODS ",
"CLAY ", "CLEARWATER ")
)
) |>
dplyr::select(county, floor, estimate, conf.low, conf.high) |>
as_tibble()
pred_multilevel_pooling <- mdl_multilevel_pooling |>
predictions(
newdata = datagrid(
floor = c(0, 1),
county = c("DOUGLAS ", "LAKE OF THE WOODS ",
"CLAY ", "CLEARWATER ")
)
) |>
dplyr::select(county, floor, estimate, conf.low, conf.high) |>
as_tibble()
radon_clean |>
filter(county %in% c("DOUGLAS ", "LAKE OF THE WOODS ",
"CLAY ", "CLEARWATER ")) |>
ggplot(aes(x = floor, y = log.radon)) +
geom_point() +
geom_line(data = pred_complete_pooling, aes(floor, estimate, group = 1), color = "red", linetype = "solid") +
geom_line(data = pred_no_pooling, aes(x = floor, y = estimate, group = 1), color = "black", linetype = "solid") +
geom_line(data = pred_multilevel_pooling, aes(x = floor, y = estimate, group = 1), color = "blue", linetype = "dashed") +
facet_wrap(~county)
The estimates for “log.radon” based on the analysis of complete pooling,
no pooling, and multilevel (partial pooling) are represented by the red,
black, and blue lines, respectively, in relation to the “first
floor.”
For CLAY, CLEARWATER, DOUGLAS and LAKE OF THE WOODS, there are 14, 4, 9
and 4 observations. A larger cluster size and a smaller standard error
between group observations results in increased similarity between the
blue line and the red line. Similarly, a larger cluster size and a
smaller standard error within group observations leads to greater
resemblance between the blue line and the black line. More pronounced
differences are observed among the three lines, with the blue line
typically serving as a balanced estimate positioned between the black
and red lines. This is particularly noticeable in counties with a
smaller number of observations.
# new data for uranium
radon_mn <- read_rds("https://bit.ly/BMLR-Radon") |>
filter(state == "MN") |>
# drop counties without measurements
mutate(county = fct_drop(county))
#
md.lm <- radon_mn |>
lm(log.radon ~ u , data = _)
md.lvl12 <- radon_mn |>
lmer(log.radon ~ u + (1|county), data = _)
pred.lm <- predictions(
md.lm,
)
pred.lvl12 <- predictions(
md.lvl12,
by = "u")
pred.lvl12 |>
ggplot(aes(x = u, xend = u, y = conf.low, yend = conf.high)) +
geom_segment() +
geom_point(aes(y = estimate)) +
geom_line(
data = pred.lm,
aes(x = u, y = estimate),
) +
xlab("Uranium (county level)") +
ylab("County level coefficients")
When we add the fixed effect of uranium levels at the county level
(level 2), it means we think the impact of uranium on radon differs
between various counties (level 2). We also assume that the influence of
uranium on radon stays the same for different houses (level 1) within a
county.
On the other hand, if we believe the effects of uranium on radon are not linked to different counties (level 2), we use a complete pooling analysis. This looks like a continuous, gradually upward line in our illustration. The estimates we get by treating uranium as a group-level factor mostly match those from the complete pooling approach. There are just a few counties that noticeably deviate from this pattern.
#NEW synthetic Data Set
set.seed(123)
obs_per_cluster <- 20
n_cluster <- 30
alpha_0 <- 2
beta_0 <- 1.5
sig <- 1
sig_u <- 1.5
sig_v <- 1.8
cor <- 0.5
synthetic_df <- add_random(
cluster = n_cluster,
obs = obs_per_cluster
) %>%
add_ranef(
"cluster",
u_i = sig_u,
v_i = sig_v,
.cors = cor
) %>%
mutate(
a_i = alpha_0 + u_i,
b_i = beta_0 + v_i,
x = rnorm(n_cluster * obs_per_cluster),
epsilon = rnorm(n_cluster * obs_per_cluster, 0, sig),
y = a_i + b_i * x + epsilon
) %>%
dplyr::select(cluster, x, y)
mod_complete_pooling_synthetic <- lm(y ~ x, synthetic_df)
mod_no_pooling_inter_synthetic <- lm(y ~ x + cluster, synthetic_df)
mod_no_pooling_wo_inter_synthetic <- lm(y ~ x + cluster + x:cluster, synthetic_df)
mod_partial_pooling_intercept_synthetic <- lmer(y ~ x + (1 | cluster), synthetic_df)
mod_partial_pooling_intercept_slope_synthetic <- lmer(y ~ x + (x | cluster), synthetic_df)
modelsummary(list(
"COMPLETE POOLING" = mod_complete_pooling_synthetic,
"NO\nPOLLING" = mod_no_pooling_inter_synthetic,
"NO POLLING+\nINTERACTION" = mod_no_pooling_wo_inter_synthetic,
"MULTILEVEL\nMODEL" = mod_partial_pooling_intercept_synthetic,
"MULTILEVEL\nMODEL +\nRANDOM\nSLOPES" = mod_partial_pooling_intercept_slope_synthetic
),
star = TRUE)| COMPLETE POOLING | NO POLLING | NO POLLING+ INTERACTION | MULTILEVEL MODEL | MULTILEVEL MODEL + RANDOM SLOPES | |
|---|---|---|---|---|---|
| (Intercept) | 1.827*** | 0.876* | 0.817*** | 1.827*** | 1.817*** |
| (0.097) | (0.413) | (0.231) | (0.285) | (0.284) | |
| x | 1.635*** | 1.620*** | 1.131*** | 1.621*** | 1.578*** |
| (0.099) | (0.079) | (0.241) | (0.079) | (0.310) | |
| clustercluster02 | 0.718 | 1.105** | |||
| (0.586) | (0.342) | ||||
| clustercluster03 | 1.069+ | 2.385*** | |||
| (0.585) | (0.347) | ||||
| clustercluster04 | 0.443 | 0.528 | |||
| (0.584) | (0.325) | ||||
| clustercluster05 | 0.802 | 0.952** | |||
| (0.584) | (0.327) | ||||
| clustercluster06 | 3.145*** | 2.748*** | |||
| (0.585) | (0.327) | ||||
| clustercluster07 | 0.864 | 1.083*** | |||
| (0.584) | (0.327) | ||||
| clustercluster08 | -0.416 | -0.226 | |||
| (0.585) | (0.325) | ||||
| clustercluster09 | 0.422 | 0.436 | |||
| (0.584) | (0.325) | ||||
| clustercluster10 | 1.292* | 1.318*** | |||
| (0.584) | (0.325) | ||||
| clustercluster11 | 3.898*** | 3.681*** | |||
| (0.585) | (0.329) | ||||
| clustercluster12 | 1.881** | 1.880*** | |||
| (0.586) | (0.339) | ||||
| clustercluster13 | 3.033*** | 3.091*** | |||
| (0.584) | (0.325) | ||||
| clustercluster14 | -0.580 | -0.615+ | |||
| (0.585) | (0.325) | ||||
| clustercluster15 | -0.854 | -0.806* | |||
| (0.584) | (0.325) | ||||
| clustercluster16 | 4.011*** | 3.997*** | |||
| (0.584) | (0.325) | ||||
| clustercluster17 | 2.090*** | 2.225*** | |||
| (0.584) | (0.327) | ||||
| clustercluster18 | 0.301 | -0.434 | |||
| (0.584) | (0.330) | ||||
| clustercluster19 | 1.779** | 1.440*** | |||
| (0.585) | (0.328) | ||||
| clustercluster20 | 0.714 | 0.730* | |||
| (0.584) | (0.325) | ||||
| clustercluster21 | -0.852 | -0.381 | |||
| (0.585) | (0.334) | ||||
| clustercluster22 | 0.797 | 1.099** | |||
| (0.585) | (0.334) | ||||
| clustercluster23 | 0.476 | -0.061 | |||
| (0.585) | (0.335) | ||||
| clustercluster24 | -1.118+ | -0.873** | |||
| (0.585) | (0.333) | ||||
| clustercluster25 | 0.387 | 0.556+ | |||
| (0.585) | (0.326) | ||||
| clustercluster26 | -2.338*** | -2.350*** | |||
| (0.584) | (0.325) | ||||
| clustercluster27 | 3.618*** | 3.778*** | |||
| (0.584) | (0.329) | ||||
| clustercluster28 | 0.350 | 0.540 | |||
| (0.584) | (0.330) | ||||
| clustercluster29 | -0.141 | -0.508 | |||
| (0.584) | (0.328) | ||||
| clustercluster30 | 2.740*** | 2.721*** | |||
| (0.585) | (0.325) | ||||
| x × clustercluster02 | -0.386 | ||||
| (0.372) | |||||
| x × clustercluster03 | 3.988*** | ||||
| (0.413) | |||||
| x × clustercluster04 | 1.179*** | ||||
| (0.324) | |||||
| x × clustercluster05 | 1.017** | ||||
| (0.332) | |||||
| x × clustercluster06 | 3.218*** | ||||
| (0.326) | |||||
| x × clustercluster07 | 1.686*** | ||||
| (0.353) | |||||
| x × clustercluster08 | -1.721*** | ||||
| (0.331) | |||||
| x × clustercluster09 | -0.656+ | ||||
| (0.374) | |||||
| x × clustercluster10 | -0.918** | ||||
| (0.343) | |||||
| x × clustercluster11 | 1.807*** | ||||
| (0.344) | |||||
| x × clustercluster12 | 0.640+ | ||||
| (0.340) | |||||
| x × clustercluster13 | 0.472 | ||||
| (0.318) | |||||
| x × clustercluster14 | 2.354*** | ||||
| (0.337) | |||||
| x × clustercluster15 | 0.346 | ||||
| (0.311) | |||||
| x × clustercluster16 | 3.053*** | ||||
| (0.339) | |||||
| x × clustercluster17 | 1.014** | ||||
| (0.347) | |||||
| x × clustercluster18 | -3.255*** | ||||
| (0.366) | |||||
| x × clustercluster19 | 2.413*** | ||||
| (0.314) | |||||
| x × clustercluster20 | -0.697* | ||||
| (0.320) | |||||
| x × clustercluster21 | -0.878* | ||||
| (0.352) | |||||
| x × clustercluster22 | -0.413 | ||||
| (0.369) | |||||
| x × clustercluster23 | -1.676*** | ||||
| (0.383) | |||||
| x × clustercluster24 | -0.249 | ||||
| (0.374) | |||||
| x × clustercluster25 | -0.604+ | ||||
| (0.334) | |||||
| x × clustercluster26 | -1.237*** | ||||
| (0.357) | |||||
| x × clustercluster27 | 0.899** | ||||
| (0.320) | |||||
| x × clustercluster28 | 0.973** | ||||
| (0.323) | |||||
| x × clustercluster29 | -1.630*** | ||||
| (0.322) | |||||
| x × clustercluster30 | 2.655*** | ||||
| (0.368) | |||||
| SD (Intercept cluster) | 1.503 | 1.538 | |||
| SD (x cluster) | 1.679 | ||||
| Cor (Intercept~x cluster) | 0.635 | ||||
| SD (Observations) | 1.848 | 1.024 | |||
| Num.Obs. | 600 | 600 | 600 | 600 | 600 |
| R2 | 0.313 | 0.602 | 0.884 | ||
| R2 Adj. | 0.312 | 0.581 | 0.871 | ||
| R2 Marg. | 0.307 | 0.281 | |||
| R2 Cond. | 0.583 | 0.876 | |||
| AIC | 2740.9 | 2471.8 | 1789.8 | 2529.2 | 1959.6 |
| BIC | 2754.1 | 2612.5 | 2058.0 | 2546.8 | 1986.0 |
| ICC | 0.4 | 0.8 | |||
| Log.Lik. | -1367.449 | -1203.894 | -833.910 | ||
| F | 273.069 | 28.689 | 69.783 | ||
| RMSE | 2.36 | 1.80 | 0.97 | 1.80 | 0.97 |
| + p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001 | |||||
clusters_synthetic <- synthetic_df %>%
pull(cluster) %>%
unique()
mdl_multilevel_synthetic <- lmer(y ~ x + (x | cluster), synthetic_df)
predict_y_synthetic <- mdl_multilevel_synthetic %>%
ranef() %>%
augment() %>%
as_tibble() %>%
filter(variable == "(Intercept)") %>%
arrange(estimate) %>%
mutate(cluster = fct_inorder(level))
predict_y_synthetic %>%
ggplot(aes(xmin = -4, xmax = 4, y = cluster, x = estimate)) +
geom_pointrange(aes(xmin = lb, xmax = ub)) +
theme(plot.background = element_rect(fill = "white")) +
labs(x = "Estimate of the intercept (Y_hat when X=0)", y = NULL, title = "Random effects caterpillar plot (Intercepts)")predictions(mdl_multilevel_synthetic) %>%
ggplot(xmin = -2, xmax = 2, aes(x = x, y = y)) +
geom_point() +
geom_line(aes(y = estimate)) +
labs(title = "Synthetic data: lattice plot Random intercept") +
facet_wrap(~cluster)Though our results show strong similarities, there are differences. This is likely because the simulated data is well-balanced and follows a clear structure, whereas the real data has missing values and fewer observations in each cluster. However, the estimates are surprisingly close despite these variations in the data.