library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(table1)
##
## Attaching package: 'table1'
##
## The following objects are masked from 'package:base':
##
## units, units<-
library(GGally)
## Registered S3 method overwritten by 'GGally':
## method from
## +.gg ggplot2
library(ggforce)
library(broom)
library(broom.mixed)
library(lmerTest)
## Loading required package: lme4
## Loading required package: Matrix
##
## Attaching package: 'Matrix'
##
## The following objects are masked from 'package:tidyr':
##
## expand, pack, unpack
##
##
## Attaching package: 'lmerTest'
##
## The following object is masked from 'package:lme4':
##
## lmer
##
## The following object is masked from 'package:stats':
##
## step
library(faux)
##
## ************
## Welcome to faux. For support and examples visit:
## https://debruine.github.io/faux/
## - Get and set global package options with: faux_options()
## ************
library(marginaleffects)
library(modelsummary)
## `modelsummary` has built-in support to draw text-only (markdown) tables.
## To generate tables in other formats, you must install one or more of
## these libraries:
##
## install.packages(c(
## "kableExtra",
## "gt",
## "flextable",
##
## "huxtable",
## "DT"
## ))
##
## Alternatively, you can set markdown as the default table format to
## silence this alert:
##
## config_modelsummary(factory_default = "markdown")
#simulation
radon <- read_rds("https://bit.ly/BMLR-Radon")
obs_per_cluster <- 20
n_cluster <- 30
alpha_0 <- 1
beta_0 <- 0.5
sig <- 1
sig_u <- 0.5
sig_v <- 0.25
cor <- 0.5
set.seed(888)
df <- add_random(
cluster = n_cluster,
obs = obs_per_cluster
)
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
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
)
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 1.07 -0.0340 2.07 0.466
## 2 cluster01 obs02 1.07 -0.0340 2.07 0.466
## 3 cluster01 obs03 1.07 -0.0340 2.07 0.466
## 4 cluster01 obs04 1.07 -0.0340 2.07 0.466
## 5 cluster01 obs05 1.07 -0.0340 2.07 0.466
## 6 cluster01 obs06 1.07 -0.0340 2.07 0.466
## 7 cluster01 obs07 1.07 -0.0340 2.07 0.466
## 8 cluster01 obs08 1.07 -0.0340 2.07 0.466
## 9 cluster01 obs09 1.07 -0.0340 2.07 0.466
## 10 cluster01 obs10 1.07 -0.0340 2.07 0.466
## 11 cluster01 obs11 1.07 -0.0340 2.07 0.466
## 12 cluster01 obs12 1.07 -0.0340 2.07 0.466
## 13 cluster01 obs13 1.07 -0.0340 2.07 0.466
## 14 cluster01 obs14 1.07 -0.0340 2.07 0.466
## 15 cluster01 obs15 1.07 -0.0340 2.07 0.466
## # ℹ 585 more rows
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
)
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 1.07 -0.0340 2.07 0.466 -0.456 0.136 1.99
## 2 cluster01 obs02 1.07 -0.0340 2.07 0.466 0.0190 -0.262 1.81
## 3 cluster01 obs03 1.07 -0.0340 2.07 0.466 -0.318 0.211 2.13
## 4 cluster01 obs04 1.07 -0.0340 2.07 0.466 -0.550 0.239 2.05
## 5 cluster01 obs05 1.07 -0.0340 2.07 0.466 1.47 1.02 3.77
## 6 cluster01 obs06 1.07 -0.0340 2.07 0.466 -0.287 0.527 2.46
## 7 cluster01 obs07 1.07 -0.0340 2.07 0.466 0.422 0.145 2.41
## 8 cluster01 obs08 1.07 -0.0340 2.07 0.466 -0.0691 0.881 2.92
## 9 cluster01 obs09 1.07 -0.0340 2.07 0.466 -1.76 -0.238 1.01
## 10 cluster01 obs10 1.07 -0.0340 2.07 0.466 -2.52 0.748 1.64
## 11 cluster01 obs11 1.07 -0.0340 2.07 0.466 1.36 -1.41 1.29
## 12 cluster01 obs12 1.07 -0.0340 2.07 0.466 0.445 -0.679 1.60
## 13 cluster01 obs13 1.07 -0.0340 2.07 0.466 -0.868 -0.583 1.08
## 14 cluster01 obs14 1.07 -0.0340 2.07 0.466 0.760 -2.15 0.276
## 15 cluster01 obs15 1.07 -0.0340 2.07 0.466 -1.04 0.500 2.09
## 16 cluster01 obs16 1.07 -0.0340 2.07 0.466 -0.187 -1.91 0.0703
## 17 cluster01 obs17 1.07 -0.0340 2.07 0.466 0.404 1.54 3.80
## 18 cluster01 obs18 1.07 -0.0340 2.07 0.466 1.27 0.687 3.35
## 19 cluster01 obs19 1.07 -0.0340 2.07 0.466 -1.25 0.868 2.35
## 20 cluster01 obs20 1.07 -0.0340 2.07 0.466 0.164 -0.675 1.47
## # ℹ 580 more rows
df <- df |> select(cluster, x, y)
df |>
print(n = 20)
## # A tibble: 600 × 3
## cluster x y
## <chr> <dbl> <dbl>
## 1 cluster01 -0.456 1.99
## 2 cluster01 0.0190 1.81
## 3 cluster01 -0.318 2.13
## 4 cluster01 -0.550 2.05
## 5 cluster01 1.47 3.77
## 6 cluster01 -0.287 2.46
## 7 cluster01 0.422 2.41
## 8 cluster01 -0.0691 2.92
## 9 cluster01 -1.76 1.01
## 10 cluster01 -2.52 1.64
## 11 cluster01 1.36 1.29
## 12 cluster01 0.445 1.60
## 13 cluster01 -0.868 1.08
## 14 cluster01 0.760 0.276
## 15 cluster01 -1.04 2.09
## 16 cluster01 -0.187 0.0703
## 17 cluster01 0.404 3.80
## 18 cluster01 1.27 3.35
## 19 cluster01 -1.25 2.35
## 20 cluster01 0.164 1.47
## # ℹ 580 more rows
mdl.compl.pool <- lm(y ~ x,df)
mdl.no.pool.noint <- lm(y ~ x + cluster, df)
mdl.no.pool.int <- lm (y ~ x + cluster + x:cluster, df)
mdl.multilevel.ri <- lmer(y ~ x + (1 | cluster), df)
mdl.multilevel.ri.rs <- lmer (y ~ x + (x | cluster), df)
sim_par <- c("α_0 = 2", "", "β_0 = 1.5",
"","","", "", "", "", "", "", "",
"","","","", "", "", "", "", "", "",
"","","","", "", "", "", "", "", "",
"","","","", "", "", "", "", "", "",
"","","","", "", "", "", "", "", "",
"","","","", "", "", "", "", "", "",
"","","","", "", "", "", "", "", "",
"","","","", "", "", "", "", "", "",
"","","","", "", "", "", "", "", "",
"","","","", "", "", "", "", "", "",
"","","","", "", "", "", "", "", "",
"","","","", "", "", "", "",
"σ_u = 1.5", "σ_v = 1.8", "ρ = 0.5", "σ = 1")
cols <- data.frame(sim_par)
names(cols) <- c("SIMULATION PARAMETER")
attr(cols, "position") <- c(2,7)
modelsummary(
list(
"COMPLETE POOLING" = mdl.compl.pool,
"NO POOLING*" = mdl.no.pool.noint,
"NO POOLING + INTERACTION*" = mdl.no.pool.int,
"MULTILEVEL MODEL" = mdl.multilevel.ri,
"MULTILEVEL MODEL + RANDOM SLOPES" = mdl.multilevel.ri.rs
),
stars = TRUE,
coef_rename = c("clustercluster02" = "cluster02",
"clustercluster03" = "cluster03",
"clustercluster04" = "cluster04",
"clustercluster05" = "cluster05",
"clustercluster06" = "cluster06",
"clustercluster07" = "cluster07",
"clustercluster08" = "cluster08",
"clustercluster09" = "cluster09",
"clustercluster10" = "cluster10",
"clustercluster11" = "cluster11",
"clustercluster12" = "cluster12",
"clustercluster13" = "cluster13",
"clustercluster14" = "cluster14",
"clustercluster15" = "cluster15",
"clustercluster16" = "cluster16",
"clustercluster17" = "cluster17",
"clustercluster18" = "cluster18",
"clustercluster19" = "cluster19",
"clustercluster20" = "cluster20",
"clustercluster21" = "cluster21",
"clustercluster22" = "cluster22",
"clustercluster23" = "cluster23",
"clustercluster24" = "cluster24",
"clustercluster25" = "cluster25",
"clustercluster26" = "cluster26",
"clustercluster27" = "cluster27",
"clustercluster28" = "cluster28",
"clustercluster29" = "cluster29",
"clustercluster30" = "cluster30"
),
add_columns = cols
)
| SIMULATION PARAMETER | COMPLETE POOLING | NO POOLING* | NO POOLING + INTERACTION* | MULTILEVEL MODEL | MULTILEVEL MODEL + RANDOM SLOPES | |
|---|---|---|---|---|---|---|
| (Intercept) | α_0 = 2 | 1.062*** | 2.056*** | 2.020*** | 1.062*** | 1.060*** |
| (0.048) | (0.233) | (0.232) | (0.111) | (0.112) | ||
| x | β_0 = 1.5 | 0.546*** | 0.520*** | 0.283 | 0.524*** | 0.509*** |
| (0.051) | (0.046) | (0.231) | (0.046) | (0.057) | ||
| cluster02 | -0.095 | -0.060 | ||||
| (0.329) | (0.327) | |||||
| cluster03 | -1.590*** | -1.571*** | ||||
| (0.329) | (0.327) | |||||
| cluster04 | -1.082** | -1.054** | ||||
| (0.329) | (0.327) | |||||
| cluster05 | -0.489 | -0.429 | ||||
| (0.330) | (0.345) | |||||
| cluster06 | -0.892** | -0.962** | ||||
| (0.330) | (0.334) | |||||
| cluster07 | 0.288 | 0.301 | ||||
| (0.330) | (0.330) | |||||
| cluster08 | -1.113*** | -0.836* | ||||
| (0.330) | (0.339) | |||||
| cluster09 | -1.909*** | -1.755*** | ||||
| (0.329) | (0.333) | |||||
| cluster10 | -1.233*** | -1.146*** | ||||
| (0.330) | (0.329) | |||||
| cluster11 | -1.149*** | -1.095*** | ||||
| (0.329) | (0.328) | |||||
| cluster12 | -1.511*** | -1.478*** | ||||
| (0.329) | (0.327) | |||||
| cluster13 | -1.532*** | -1.511*** | ||||
| (0.330) | (0.335) | |||||
| cluster14 | -0.973** | -1.053** | ||||
| (0.330) | (0.331) | |||||
| cluster15 | -0.975** | -0.939** | ||||
| (0.330) | (0.327) | |||||
| cluster16 | -0.916** | -0.906** | ||||
| (0.329) | (0.333) | |||||
| cluster17 | -0.895** | -0.859** | ||||
| (0.329) | (0.327) | |||||
| cluster18 | -1.381*** | -1.344*** | ||||
| (0.329) | (0.327) | |||||
| cluster19 | -0.004 | 0.098 | ||||
| (0.329) | (0.329) | |||||
| cluster20 | -0.511 | -0.332 | ||||
| (0.330) | (0.348) | |||||
| cluster21 | -1.187*** | -1.181*** | ||||
| (0.329) | (0.333) | |||||
| cluster22 | -0.966** | -0.933** | ||||
| (0.329) | (0.327) | |||||
| cluster23 | -1.106*** | -1.067** | ||||
| (0.329) | (0.328) | |||||
| cluster24 | -1.408*** | -1.370*** | ||||
| (0.329) | (0.327) | |||||
| cluster25 | -0.691* | -0.688* | ||||
| (0.329) | (0.337) | |||||
| cluster26 | -0.672* | -0.650* | ||||
| (0.329) | (0.328) | |||||
| cluster27 | -0.600+ | -0.467 | ||||
| (0.329) | (0.342) | |||||
| cluster28 | -2.735*** | -2.778*** | ||||
| (0.329) | (0.330) | |||||
| cluster29 | -1.458*** | -1.487*** | ||||
| (0.330) | (0.331) | |||||
| cluster30 | -1.032** | -1.071** | ||||
| (0.329) | (0.335) | |||||
| x:cluster02 | 0.554+ | |||||
| (0.311) | ||||||
| x:cluster03 | -0.076 | |||||
| (0.319) | ||||||
| x:cluster04 | -0.221 | |||||
| (0.351) | ||||||
| x:cluster05 | 0.169 | |||||
| (0.378) | ||||||
| x:cluster06 | 0.548+ | |||||
| (0.303) | ||||||
| x:cluster07 | 0.374 | |||||
| (0.360) | ||||||
| x:cluster08 | -0.441 | |||||
| (0.338) | ||||||
| x:cluster09 | 0.788* | |||||
| (0.369) | ||||||
| x:cluster10 | -0.127 | |||||
| (0.344) | ||||||
| x:cluster11 | 0.426 | |||||
| (0.317) | ||||||
| x:cluster12 | -0.082 | |||||
| (0.364) | ||||||
| x:cluster13 | 0.196 | |||||
| (0.313) | ||||||
| x:cluster14 | 0.702* | |||||
| (0.315) | ||||||
| x:cluster15 | 0.234 | |||||
| (0.304) | ||||||
| x:cluster16 | 0.111 | |||||
| (0.391) | ||||||
| x:cluster17 | 0.293 | |||||
| (0.327) | ||||||
| x:cluster18 | 0.135 | |||||
| (0.385) | ||||||
| x:cluster19 | 0.603* | |||||
| (0.299) | ||||||
| x:cluster20 | -0.099 | |||||
| (0.360) | ||||||
| x:cluster21 | 0.125 | |||||
| (0.332) | ||||||
| x:cluster22 | 0.374 | |||||
| (0.354) | ||||||
| x:cluster23 | 0.275 | |||||
| (0.368) | ||||||
| x:cluster24 | 0.282 | |||||
| (0.347) | ||||||
| x:cluster25 | 0.050 | |||||
| (0.516) | ||||||
| x:cluster26 | 0.095 | |||||
| (0.335) | ||||||
| x:cluster27 | 0.589 | |||||
| (0.435) | ||||||
| x:cluster28 | -0.304 | |||||
| (0.397) | ||||||
| x:cluster29 | 0.503 | |||||
| (0.320) | ||||||
| x:cluster30 | -0.146 | |||||
| (0.428) | ||||||
| SD (Intercept cluster) | σ_u = 1.5 | 0.563 | 0.570 | |||
| SD (x cluster) | σ_v = 1.8 | 0.179 | ||||
| Cor (Intercept~x cluster) | ρ = 0.5 | 0.440 | ||||
| SD (Observations) | σ = 1 | 1.042 | 1.028 | |||
| Num.Obs. | 600 | 600 | 600 | 600 | 600 | |
| R2 | 0.160 | 0.377 | 0.424 | |||
| R2 Adj. | 0.158 | 0.344 | 0.361 | |||
| R2 Marg. | 0.148 | 0.140 | ||||
| R2 Cond. | 0.340 | 0.355 | ||||
| AIC | 1905.0 | 1783.9 | 1794.9 | 1822.2 | 1821.4 | |
| BIC | 1918.2 | 1924.6 | 2063.1 | 1839.8 | 1847.8 | |
| ICC | 0.2 | 0.3 | ||||
| Log.Lik. | -949.498 | -859.930 | -836.443 | |||
| RMSE | 1.18 | 1.01 | 0.98 | 1.02 | 1.00 |
Note: ^^ + p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001 Anser Q:Is there one single model that is superior to all the rest in terms of estimating the parameters? A:Altjough some models indeed have better fit in terms of the parameter estimation, if we’re comparing AIC and BIK, the final choice of the model will be dependant on the research question, underying assumptions and goals of the analysis, since each model will have it’s trade-offs,and comparison metrcis are not always 100% conclusive.Thus, the answer is that there’s no superior model here. Q: If not, Which model works best for what purpose? A:Complete Pooling Model can be used for simplicity and when assuming all clusters share the same relationship.No Pooling Models can be used when expecting different intercepts for each cluster. Multilevel Models are used when accounting for clustering effects, Multilevel Model & Random Slopes are used when slopes vary across clusters. Q: What does the ICC mean? How much does it deviate from what you would expect? A:ICC gives us insights into the impact of clustering on the dependent variable.For example, an ICC of 0.2 in the Multilevel Model suggests that 20% of the total variance is attributed to cluster-level differences.
clusters <- df |>
pull(cluster) |>
unique()
# Now let's fit our multilevel model
# For the graphs, fitting both models with random intercepts only, and with random intercepts and slopes
mdl.multilevel.ri <- lmer(y ~ x + (1 | cluster), data = df)
mdl.multilevel.ri.rs <- lmer(y ~ x + (x | cluster), df)
# Predictions - random intercepts
predict_y <- mdl.multilevel.ri |>
ranef() |>
augment() |>
as_tibble() |>
filter(variable == "(Intercept)") |>
arrange(estimate) |>
mutate(cluster = fct_inorder(level))
# Caterpillar - random intercept
predict_y |>
ggplot(aes(xmin = lb, xmax = ub, y = cluster, x = estimate)) +
geom_pointrange() +
labs(x = "Estimate of the intercept (Y_hat when X=0)", y = NULL) +
theme_minimal() +
theme(axis.title = element_text(face="bold"), plot.title = element_text(face = "bold")) +
labs(x = "Cluster intercepts", y = "", title = "Random effects caterpillar plot (Random intercepts)")
# Predictions - random intercepts & random slopes
predict_y1 <- mdl.multilevel.ri.rs |>
ranef() |>
augment() |>
as_tibble() |>
filter(variable == "(Intercept)") |>
arrange(estimate) |>
mutate(cluster = fct_inorder(level))
# Caterpillar - random intercept & random slopes
predict_y1 |>
ggplot(aes(xmin = lb, xmax = ub, y = cluster, x = estimate)) +
geom_pointrange() +
labs(x = "Estimate of the intercept (Y_hat when X=0)", y = NULL) +
theme_minimal() +
theme(axis.title = element_text(face="bold"), plot.title = element_text(face = "bold")) +
labs(x = "Cluster intercepts", y = "", title = "Random effects caterpillar plot (Random intercepts & random slopes)")
# Spagetti plot
predictions(mdl.multilevel.ri) |>
ggplot(aes(x, y, color = cluster)) +
geom_point() +
geom_line(aes(y = estimate)) +
theme_minimal() +
theme(axis.title = element_text(face="bold"), plot.title = element_text(face = "bold")) +
labs(x = "R", y = "", title = "Simulated data: spaghetti plot (Random intercept)", legend.position = "none")
predictions(mdl.multilevel.ri.rs) |>
ggplot(aes(x, y, color = cluster)) +
geom_point() +
geom_line(aes(y = estimate)) +
theme_minimal() +
theme(axis.title = element_text(face="bold"), plot.title = element_text(face = "bold"), legend.position = "none") +
labs(x = "R", y = "", title = "Simulated data: spaghetti plot (Random intercept and random splope)")
# Lattice plot random intercepts
predictions(mdl.multilevel.ri) |>
ggplot(aes(x, y, color = cluster)) +
geom_point() +
geom_line(aes(y = estimate)) +
facet_wrap(~cluster) +
theme_minimal() +
theme(axis.title = element_text(face="bold"), plot.title = element_text(face = "bold"), legend.position = "none") +
labs(x = "R", y = "", title = "Simulated data: lattice plot (Random intercept)")
# Lattice plot random intercepts
predictions(mdl.multilevel.ri.rs) |>
ggplot(aes(x, y, color = cluster)) +
geom_point() +
geom_line(aes(y = estimate)) +
facet_wrap(~cluster) +
theme_minimal() +
theme(axis.title = element_text(face="bold"), plot.title = element_text(face = "bold"), legend.position = "none") +
labs(x = "R", y = "", title = "Simulated data: lattice plot (Random intercept and random slope)")
Answer Q:Describe and discuss the plots: what new light
do they shed? What are the advantages or disadvantages of presenting the
models using these plots?
A: Multilevel models can be effectively visualized using caterpillar plots for intercepts and lattice or spaghetti plots for features.Caterpillar plots succinctly display the variability in intercept estimates across different levels, facilitating easy comparison and identification of group differences. However, their simplicity might limit the exploration of complex patterns in the data. Lattice or spaghetti plots offer a more detailed perspective by illustrating individual trajectories, providing insights into variability and patterns within and between groups. Yet, these plots may suffer from overplotting issues and complexity, especially with a large number of subjects.In this analysis, there is noticeable variability in the intercepts; however, upon incorporating random slopes, there is not a significant increase in overall prediction variation. This suggests that introducing random slopes may complicate the model without necessarily leading to improvement.
radon_mn <- read_rds("https://bit.ly/BMLR-Radon") |>
filter(state == "MN") |>
# drop counties without measurements
mutate(county = fct_drop(county))
radon_mn_table <- radon_mn
label(radon_mn_table$county) <- "County"
label(radon_mn_table$log.radon) <- "log Radon"
label(radon_mn_table$first_floor) <- "If first floor"
label(radon_mn_table$u) <- "Uranium levels"
table1(~ log.radon + u + county | first_floor, data=radon_mn_table, overall = F)
## Warning in table1.formula(~log.radon + u + county | first_floor, data =
## radon_mn_table, : Terms to the right of '|' in formula 'x' define table columns
## and are expected to be factors with meaningful labels.
| 0 (N=766) |
1 (N=153) |
|
|---|---|---|
| log Radon | ||
| Mean (SD) | 1.33 (0.783) | 0.713 (0.999) |
| Median [Min, Max] | 1.36 [-2.30, 3.88] | 0.742 [-2.30, 3.23] |
| Uranium levels | ||
| Mean (SD) | -0.139 (0.366) | -0.0970 (0.360) |
| Median [Min, Max] | -0.0965 [-0.882, 0.528] | -0.0900 [-0.882, 0.504] |
| County | ||
| AITKIN | 3 (0.4%) | 1 (0.7%) |
| ANOKA | 49 (6.4%) | 3 (2.0%) |
| BECKER | 1 (0.1%) | 2 (1.3%) |
| BELTRAMI | 3 (0.4%) | 4 (2.6%) |
| BENTON | 3 (0.4%) | 1 (0.7%) |
| BIG STONE | 3 (0.4%) | 0 (0%) |
| BLUE EARTH | 12 (1.6%) | 2 (1.3%) |
| BROWN | 2 (0.3%) | 2 (1.3%) |
| CARLTON | 9 (1.2%) | 1 (0.7%) |
| CARVER | 3 (0.4%) | 3 (2.0%) |
| CASS | 5 (0.7%) | 0 (0%) |
| CHIPPEWA | 4 (0.5%) | 0 (0%) |
| CHISAGO | 6 (0.8%) | 0 (0%) |
| CLAY | 10 (1.3%) | 4 (2.6%) |
| CLEARWATER | 2 (0.3%) | 2 (1.3%) |
| COOK | 2 (0.3%) | 0 (0%) |
| COTTONWOOD | 1 (0.1%) | 3 (2.0%) |
| CROW WING | 9 (1.2%) | 3 (2.0%) |
| DAKOTA | 59 (7.7%) | 4 (2.6%) |
| DODGE | 3 (0.4%) | 0 (0%) |
| DOUGLAS | 8 (1.0%) | 1 (0.7%) |
| FARIBAULT | 5 (0.7%) | 1 (0.7%) |
| FILLMORE | 1 (0.1%) | 1 (0.7%) |
| FREEBORN | 7 (0.9%) | 2 (1.3%) |
| GOODHUE | 12 (1.6%) | 2 (1.3%) |
| HENNEPIN | 94 (12.3%) | 11 (7.2%) |
| HOUSTON | 4 (0.5%) | 2 (1.3%) |
| HUBBARD | 2 (0.3%) | 3 (2.0%) |
| ISANTI | 3 (0.4%) | 0 (0%) |
| ITASCA | 11 (1.4%) | 0 (0%) |
| JACKSON | 5 (0.7%) | 0 (0%) |
| KANABEC | 4 (0.5%) | 0 (0%) |
| KANDIYOHI | 4 (0.5%) | 0 (0%) |
| KITTSON | 1 (0.1%) | 2 (1.3%) |
| KOOCHICHING | 3 (0.4%) | 4 (2.6%) |
| LAC QUI PARLE | 1 (0.1%) | 1 (0.7%) |
| LAKE | 8 (1.0%) | 1 (0.7%) |
| LAKE OF THE WOODS | 2 (0.3%) | 2 (1.3%) |
| LE SUEUR | 4 (0.5%) | 1 (0.7%) |
| LINCOLN | 3 (0.4%) | 1 (0.7%) |
| LYON | 7 (0.9%) | 1 (0.7%) |
| MAHNOMEN | 1 (0.1%) | 0 (0%) |
| MARSHALL | 4 (0.5%) | 5 (3.3%) |
| MARTIN | 6 (0.8%) | 1 (0.7%) |
| MCLEOD | 9 (1.2%) | 4 (2.6%) |
| MEEKER | 5 (0.7%) | 0 (0%) |
| MILLE LACS | 1 (0.1%) | 1 (0.7%) |
| MORRISON | 8 (1.0%) | 1 (0.7%) |
| MOWER | 11 (1.4%) | 2 (1.3%) |
| MURRAY | 1 (0.1%) | 0 (0%) |
| NICOLLET | 4 (0.5%) | 0 (0%) |
| NOBLES | 3 (0.4%) | 0 (0%) |
| NORMAN | 2 (0.3%) | 1 (0.7%) |
| OLMSTED | 20 (2.6%) | 3 (2.0%) |
| OTTER TAIL | 5 (0.7%) | 3 (2.0%) |
| PENNINGTON | 1 (0.1%) | 2 (1.3%) |
| PINE | 5 (0.7%) | 1 (0.7%) |
| PIPESTONE | 3 (0.4%) | 1 (0.7%) |
| POLK | 2 (0.3%) | 2 (1.3%) |
| POPE | 2 (0.3%) | 0 (0%) |
| RAMSEY | 29 (3.8%) | 3 (2.0%) |
| REDWOOD | 4 (0.5%) | 1 (0.7%) |
| RENVILLE | 2 (0.3%) | 1 (0.7%) |
| RICE | 10 (1.3%) | 1 (0.7%) |
| ROCK | 2 (0.3%) | 0 (0%) |
| ROSEAU | 6 (0.8%) | 8 (5.2%) |
| SCOTT | 9 (1.2%) | 4 (2.6%) |
| SHERBURNE | 8 (1.0%) | 0 (0%) |
| SIBLEY | 4 (0.5%) | 0 (0%) |
| ST LOUIS | 100 (13.1%) | 16 (10.5%) |
| STEARNS | 21 (2.7%) | 4 (2.6%) |
| STEELE | 10 (1.3%) | 0 (0%) |
| STEVENS | 2 (0.3%) | 0 (0%) |
| SWIFT | 4 (0.5%) | 0 (0%) |
| TODD | 2 (0.3%) | 1 (0.7%) |
| TRAVERSE | 3 (0.4%) | 1 (0.7%) |
| WABASHA | 6 (0.8%) | 1 (0.7%) |
| WADENA | 3 (0.4%) | 2 (1.3%) |
| WASECA | 3 (0.4%) | 1 (0.7%) |
| WASHINGTON | 41 (5.4%) | 5 (3.3%) |
| WATONWAN | 1 (0.1%) | 2 (1.3%) |
| WILKIN | 1 (0.1%) | 0 (0%) |
| WINONA | 10 (1.3%) | 3 (2.0%) |
| WRIGHT | 12 (1.6%) | 1 (0.7%) |
| YELLOW MEDICINE | 2 (0.3%) | 0 (0%) |
radon_mn_figure <- radon_mn[, c(4, 5, 8)]
radon_mn_figure$first_floor_fact <- ifelse(radon_mn_figure$first_floor == 1, "Yes", "No")
radon_mn_figure$first_floor_fact <- factor(radon_mn_figure$first_floor_fact, levels = unique(radon_mn_figure$first_floor_fact))
radon_mn_figure <- radon_mn_figure[, !names(radon_mn_figure) %in% "first_floor"]
ggpairs(radon_mn_figure, aes(color = first_floor_fact), legend = 1,
columnLabels = c("log Radon", "Uranium","If first floor"),
upper = list(continuous = wrap("smooth_lm"), combo = wrap("autopointDiag", outlier.size=0.5), discrete = wrap("colbar", fontface = "bold", size=2)),
lower = list(continuous = wrap("cor"), combo = wrap("denstrip"), discrete = wrap("crosstable", fontface = "bold", size=2, cells = "col.prop", fill = "std.resid")),
diag = list(continuous = wrap("densityDiag", alpha = 0.5, color=NA), discrete = wrap("densityDiag", alpha = 0.5))) +
theme_minimal() +
theme(legend.position = 'bottom', legend.direction = "horizontal", axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(fill='First floor')
## Warning in (function (mapping = NULL, data = NULL, stat = "identity", position
## = "auto", : Ignoring unknown parameters: `outlier.size`
## Warning in (function (mapping = NULL, data = NULL, stat = "identity", position
## = "auto", : Ignoring unknown parameters: `outlier.size`
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 13 rows containing missing values (`geom_bar()`).
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 10 rows containing missing values (`geom_bar()`).
radon_mn_count <- radon_mn |>
count(county, name = "n.obs")
m.no_pool <- radon_mn |>
lm(log.radon ~ 0 + county, data = _)
m.partial_pool <- radon_mn |>
lmer(log.radon ~ (1|county), data = _)
# Use here the two models - first the fixed effects (no pooling)
m.no_pool |>
predictions(newdata = datagrid(county = unique)) |>
as_tibble() |> inner_join(radon_mn_count, by = "county") |>
select(county, estimate, conf.low, conf.high, n.obs) |>
ggplot(
aes(
x = n.obs,
y = estimate,
ymin = conf.low,
ymax = conf.high
)
) +
geom_pointrange(
position = position_jitter(width = .5, height = 0)
) +
geom_hline(aes(yintercept = median(estimate))) +
theme_minimal() +
theme(axis.title = element_text(face="bold"), plot.title = element_text(face = "bold"), legend.position = "none") +
labs(x = "Number of observations (log 10 transformed)", y = "Estinmated log radon in the county", title = "No pooling (fixed effects)") +
scale_x_log10()
# model, and then the multilevel (partial pooling) model.
m.partial_pool |>
predictions(newdata = datagrid(county = unique)) |>
as_tibble() |> inner_join(radon_mn_count, by = "county") |>
select(county, estimate, conf.low, conf.high, n.obs) |>
ggplot(
aes(
x = n.obs,
y = estimate,
ymin = conf.low,
ymax = conf.high
)
) +
geom_pointrange(
position = position_jitter(width = .5, height = 0)
) +
geom_hline(aes(yintercept = median(estimate))) +
theme_minimal() +
theme(axis.title = element_text(face="bold"), plot.title = element_text(face = "bold"), legend.position = "none") +
labs(x = "Number of observations (log 10 transformed)", y = "Estinmated log radon in the county", title = "Partial pooling (multilevel model)") +
scale_x_log10()
Answer Q: Replicate the figures below and explain why the counties with fewer measurements have more variable estimates and larger higher standard errors. A: Counties with fewer measurements are likely to have more variable estimates because the model doesn’t pool information across counties. In other words, estimates are solely based on the data from each specific county, leading to higher variability. The higher standard errors in counties with fewer measurements are expected in the “no-pooling” analysis. This is because the estimates are less stable due to the limited amount of data for each county.
Q: What is the main advantage and disadvantage of no-pooling analysis, as illustrated in the plot on the left compared with the multilevel model? Explain. A: The “no-pooling” analysis allows for individualized estimates for each county, which might be useful if there are substantial differences between counties, and pooling data would obscure these differences. The main disadvantage however, is that estimates for counties with limited data can be highly variable and less reliable. The model doesn’t borrow strength across counties, which can lead to less accurate predictions, especially for counties with sparse data.
###Exercise 5
# Partial pooling
mdl_multilevel <- radon_mn |>
lmer(log.radon ~ first_floor + (first_floor|county), data = _)
# Collect the predictions
pred_multi <- mdl_multilevel |>
predictions(
newdata = datagrid(
first_floor = c(0, 1),
county = c("BLUE EARTH", "CLAY","DAKOTA","DOUGLAS")
)
) |>
select(county, first_floor, estimate, conf.low, conf.high) |>
as_tibble( )
# No pooling
mdl_no_pool <- radon_mn |>
lm(log.radon ~ first_floor + county, data = _)
# Collect the predictions
pred_no_pool <- mdl_no_pool |>
predictions(
newdata = datagrid(
first_floor = c(0, 1),
county = c("BLUE EARTH", "CLAY","DAKOTA","DOUGLAS")
)
) |>
select(county, first_floor, estimate, conf.low, conf.high) |>
as_tibble( )
# Complete pooling
mdl_com_pool <- radon_mn |>
lm(log.radon ~ first_floor, data = _)
# Collect the predictions
pred_com_pool <- mdl_com_pool |>
predictions(
newdata = datagrid(
first_floor = c(0, 1),
county = c("BLUE EARTH", "CLAY","DAKOTA","DOUGLAS")
)
) |>
select(county, first_floor, estimate, conf.low, conf.high) |>
as_tibble( )
## Warning: Some of the variable names are missing from the model data: county
radon_mn |>
filter(county %in% c("BLUE EARTH", "CLAY","DAKOTA","DOUGLAS")) |>
ggplot(aes(x = first_floor, y = log.radon)) +
geom_point(size=3) +
geom_line(data = pred_no_pool, aes(first_floor, estimate), linetype = "dashed", size=1) +
geom_line(data = pred_multi, aes(first_floor, estimate), color="blue", size=1) +
geom_line(data = pred_com_pool, aes(first_floor, estimate), linetype = "dashed", color="red", size=2) +
facet_wrap(~county, ncol = 4)+
theme_minimal() +
theme(axis.title = element_text(face="bold"), plot.title = element_text(face = "bold"), legend.position = "none")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
Answer Q: Please explain the graphs. What do the dashed
red and black lines show? What is the blue line? A: The red line
represents the complete pooling model, which does not vary with the
county level. This means that the model assumes the same intercept and
slope for all counties, showing a lack of sensitivity to county-specific
variations. The dashed black line represents the no pooling model, which
is highly sensitive to the data in each county. Even with a small number
of observations, the no pooling model tends to over-fit the data,
connecting the dots for each county individually. The blue line
represents the multilevel model, which strikes a balance between the
complete pooling and no pooling models. This model introduces some bias
but avoids over-fitting. It acknowledges and incorporates
county-specific variations, making it more suitable for generalizations
across counties.
Q: What determines whether the blue line would be more similar to the red line or the black line? A: The similarity of the blue line to the red or black line depends on the level of pooling. If there is a high degree of pooling, the blue line would be more similar to the red line, indicating that the model is generalizing across counties with limited consideration for county-specific variations. Conversely, if there is low pooling, the blue line would resemble the black line more closely, indicating a model that is more sensitive to county-specific data.
Q: How is this figure compare to the one shown in chapter 12 of the book? A: The figure obtained is similar to the one in Chapter 12 of the book. The key principles of complete pooling, no pooling, and partial pooling models remain consistent, illustrating the trade-offs between bias and flexibility in handling county-level variations.
# The simple linear model for the general relationship between
# Radon measurements and county level uranium
md.lm <- radon_mn |>
lm(log.radon ~ u , data = _)
# The multilevel model for the radon as a function of level-2 uranium
md.lvl12 <- radon_mn |>
lmer(log.radon ~ u + (1|county), data = _)
# Predictions for the linear model
pred.lm <- predictions(
md.lm,
)
# predictions for the multilevel model
# stratified by the level of uranium
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), linetype="dashed")+
theme_minimal() +
theme(axis.title = element_text(face="bold"), legend.position = "none") +
labs(x = "Uranium (county level)", y = "County level coefficients")
Answer Q: After replicating this graph, please explain
it briefly.
A: Each point on the resulting plot represents the point estimate of
radon levels for a specific level of uranium in the multilevel model,
while the line represents the predictions from the linear model. This
visualization can help compare the predictions from the linear and
multilevel models, especially in terms of how they account for the
variability between counties (given the multilevel structure).
modelsummary(mdl_multilevel)
| (1) | |
|---|---|
| (Intercept) | 1.463 |
| (0.054) | |
| first_floor | -0.681 |
| (0.088) | |
| SD (Intercept county) | 0.349 |
| SD (first_floor county) | 0.344 |
| Cor (Intercept~first_floor county) | -0.337 |
| SD (Observations) | 0.746 |
| Num.Obs. | 919 |
| R2 Marg. | 0.086 |
| R2 Cond. | 0.257 |
| AIC | 2180.3 |
| BIC | 2209.3 |
| ICC | 0.2 |
| RMSE | 0.72 |
obs_per_cluster <- 30
n_cluster <- 85
alpha_0 <- 1.463
beta_0 <- -0.681
sig <- 0.746
sig_u <- 0.349
sig_v <- 0.344
cor <- -0.337
set.seed(888)
df1 <- add_random(
cluster = n_cluster,
obs = obs_per_cluster
)
df1 |>
print(n = 15)
## # A tibble: 2,550 × 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
## # ℹ 2,535 more rows
df1 <- df1 |>
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
)
df1 |>
print(n = 15)
## # A tibble: 2,550 × 6
## cluster obs u_i v_i a_i b_i
## <chr> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 cluster01 obs01 0.278 -0.840 1.74 -1.52
## 2 cluster01 obs02 0.278 -0.840 1.74 -1.52
## 3 cluster01 obs03 0.278 -0.840 1.74 -1.52
## 4 cluster01 obs04 0.278 -0.840 1.74 -1.52
## 5 cluster01 obs05 0.278 -0.840 1.74 -1.52
## 6 cluster01 obs06 0.278 -0.840 1.74 -1.52
## 7 cluster01 obs07 0.278 -0.840 1.74 -1.52
## 8 cluster01 obs08 0.278 -0.840 1.74 -1.52
## 9 cluster01 obs09 0.278 -0.840 1.74 -1.52
## 10 cluster01 obs10 0.278 -0.840 1.74 -1.52
## 11 cluster01 obs11 0.278 -0.840 1.74 -1.52
## 12 cluster01 obs12 0.278 -0.840 1.74 -1.52
## 13 cluster01 obs13 0.278 -0.840 1.74 -1.52
## 14 cluster01 obs14 0.278 -0.840 1.74 -1.52
## 15 cluster01 obs15 0.278 -0.840 1.74 -1.52
## # ℹ 2,535 more rows
df1 <- df1 |>
mutate(
first_floor = rbinom(n_cluster * obs_per_cluster, size = 1, prob = 0.5),
epsilon = rnorm(n_cluster * obs_per_cluster, 0, sig),
y = a_i + b_i * first_floor + epsilon
)
df1 |>
print(n = 20)
## # A tibble: 2,550 × 9
## cluster obs u_i v_i a_i b_i first_floor epsilon y
## <chr> <chr> <dbl> <dbl> <dbl> <dbl> <int> <dbl> <dbl>
## 1 cluster01 obs01 0.278 -0.840 1.74 -1.52 1 -1.47 -1.26
## 2 cluster01 obs02 0.278 -0.840 1.74 -1.52 0 -0.0892 1.65
## 3 cluster01 obs03 0.278 -0.840 1.74 -1.52 0 1.51 3.25
## 4 cluster01 obs04 0.278 -0.840 1.74 -1.52 0 -0.270 1.47
## 5 cluster01 obs05 0.278 -0.840 1.74 -1.52 0 1.23 2.97
## 6 cluster01 obs06 0.278 -0.840 1.74 -1.52 1 -0.737 -0.518
## 7 cluster01 obs07 0.278 -0.840 1.74 -1.52 0 0.592 2.33
## 8 cluster01 obs08 0.278 -0.840 1.74 -1.52 0 0.167 1.91
## 9 cluster01 obs09 0.278 -0.840 1.74 -1.52 1 -0.606 -0.387
## 10 cluster01 obs10 0.278 -0.840 1.74 -1.52 1 -0.889 -0.670
## 11 cluster01 obs11 0.278 -0.840 1.74 -1.52 0 0.167 1.91
## 12 cluster01 obs12 0.278 -0.840 1.74 -1.52 1 1.01 1.23
## 13 cluster01 obs13 0.278 -0.840 1.74 -1.52 1 -0.348 -0.129
## 14 cluster01 obs14 0.278 -0.840 1.74 -1.52 1 -0.282 -0.0625
## 15 cluster01 obs15 0.278 -0.840 1.74 -1.52 1 0.0253 0.245
## 16 cluster01 obs16 0.278 -0.840 1.74 -1.52 0 0.0194 1.76
## 17 cluster01 obs17 0.278 -0.840 1.74 -1.52 1 0.200 0.420
## 18 cluster01 obs18 0.278 -0.840 1.74 -1.52 1 -0.859 -0.639
## 19 cluster01 obs19 0.278 -0.840 1.74 -1.52 1 0.373 0.592
## 20 cluster01 obs20 0.278 -0.840 1.74 -1.52 0 0.508 2.25
## # ℹ 2,530 more rows
df1 <- df1 |> select(cluster, first_floor, y)
df1 |>
print(n = 20)
## # A tibble: 2,550 × 3
## cluster first_floor y
## <chr> <int> <dbl>
## 1 cluster01 1 -1.26
## 2 cluster01 0 1.65
## 3 cluster01 0 3.25
## 4 cluster01 0 1.47
## 5 cluster01 0 2.97
## 6 cluster01 1 -0.518
## 7 cluster01 0 2.33
## 8 cluster01 0 1.91
## 9 cluster01 1 -0.387
## 10 cluster01 1 -0.670
## 11 cluster01 0 1.91
## 12 cluster01 1 1.23
## 13 cluster01 1 -0.129
## 14 cluster01 1 -0.0625
## 15 cluster01 1 0.245
## 16 cluster01 0 1.76
## 17 cluster01 1 0.420
## 18 cluster01 1 -0.639
## 19 cluster01 1 0.592
## 20 cluster01 0 2.25
## # ℹ 2,530 more rows
names(df1)[names(df1) == "y"] <- "log.radon"
county <- unique(as.character(radon_mn$county))
cluster <- unique(df1$cluster)
correspondence <- as.data.frame(cbind(county, cluster))
df1 <- merge(df1, correspondence, by="cluster")
df1 <- df1[c(2:4)]
sm.compl.pool <- lm(log.radon ~ first_floor,df1)
sm.no.pool <- lm(log.radon ~ first_floor + county, df1)
sm.multilevel <- lmer (log.radon ~ first_floor + (first_floor | county), df1)
mdl_com_pool <- lm(log.radon ~ first_floor, radon_mn)
mdl_no_pool <- lm(log.radon ~ first_floor + county, radon_mn)
mdl_multilevel <- lmer(log.radon ~ first_floor + (first_floor | county), radon_mn)
modelsummary(
list("COMPLETE POOLING (simulation)" = sm.compl.pool,
"COMPLETE POOLING (real data)" = mdl_com_pool,
"NO POOLING (simulation)" = sm.no.pool,
"NO POOLING (real data)" = mdl_no_pool,
"MULTILEVEL MODEL (simulation)" = sm.multilevel,
"MULTILEVEL MODEL (real data)" = mdl_multilevel))
| COMPLETE POOLING (simulation) | COMPLETE POOLING (real data) | NO POOLING (simulation) | NO POOLING (real data) | MULTILEVEL MODEL (simulation) | MULTILEVEL MODEL (real data) | |
|---|---|---|---|---|---|---|
| (Intercept) | 1.448 | 1.327 | 1.309 | 0.841 | 1.457 | 1.463 |
| (0.023) | (0.030) | (0.142) | (0.379) | (0.045) | (0.054) | |
| first_floor | -0.689 | -0.613 | -0.713 | -0.721 | -0.709 | -0.681 |
| (0.033) | (0.073) | (0.031) | (0.074) | (0.054) | (0.088) | |
| countyANOKA | 0.173 | 0.034 | ||||
| (0.199) | (0.393) | |||||
| countyBECKER | -0.201 | 0.688 | ||||
| (0.199) | (0.579) | |||||
| countyBELTRAMI | 0.259 | 0.712 | ||||
| (0.199) | (0.475) | |||||
| countyBENTON | 0.241 | 0.592 | ||||
| (0.199) | (0.535) | |||||
| countyBIG STONE | -0.214 | 0.672 | ||||
| (0.199) | (0.578) | |||||
| countyBLUE EARTH | 0.212 | 1.172 | ||||
| (0.199) | (0.429) | |||||
| countyBROWN | -0.183 | 1.149 | ||||
| (0.199) | (0.535) | |||||
| countyCARLTON | 0.398 | 0.162 | ||||
| (0.199) | (0.448) | |||||
| countyCARVER | 0.191 | 0.723 | ||||
| (0.199) | (0.489) | |||||
| countyCASS | 0.071 | 0.561 | ||||
| (0.199) | (0.508) | |||||
| countyCHIPPEWA | 0.103 | 0.890 | ||||
| (0.199) | (0.535) | |||||
| countyCHISAGO | 0.345 | 0.198 | ||||
| (0.199) | (0.489) | |||||
| countyCLAY | -0.417 | 1.148 | ||||
| (0.199) | (0.429) | |||||
| countyCLEARWATER | 0.611 | 0.497 | ||||
| (0.199) | (0.535) | |||||
| countyCOOK | 0.140 | -0.176 | ||||
| (0.199) | (0.655) | |||||
| countyCOTTONWOOD | 0.380 | 0.434 | ||||
| (0.199) | (0.536) | |||||
| countyCROW WING | 0.552 | 0.281 | ||||
| (0.199) | (0.437) | |||||
| countyDAKOTA | -0.355 | 0.498 | ||||
| (0.199) | (0.390) | |||||
| countyDODGE | 0.811 | 0.960 | ||||
| (0.199) | (0.578) | |||||
| countyDOUGLAS | 0.435 | 0.893 | ||||
| (0.199) | (0.455) | |||||
| countyFARIBAULT | 0.471 | -0.204 | ||||
| (0.199) | (0.488) | |||||
| countyFILLMORE | 0.108 | 0.559 | ||||
| (0.199) | (0.655) | |||||
| countyFREEBORN | -0.224 | 1.261 | ||||
| (0.199) | (0.455) | |||||
| countyGOODHUE | 0.361 | 1.110 | ||||
| (0.199) | (0.429) | |||||
| countyHENNEPIN | 0.150 | 0.520 | ||||
| (0.199) | (0.385) | |||||
| countyHOUSTON | -0.427 | 0.933 | ||||
| (0.199) | (0.488) | |||||
| countyHUBBARD | 0.004 | 0.401 | ||||
| (0.199) | (0.508) | |||||
| countyISANTI | 0.345 | 0.215 | ||||
| (0.199) | (0.578) | |||||
| countyITASCA | -0.377 | 0.085 | ||||
| (0.200) | (0.442) | |||||
| countyJACKSON | 0.047 | 1.180 | ||||
| (0.199) | (0.508) | |||||
| countyKANABEC | 0.846 | 0.396 | ||||
| (0.199) | (0.535) | |||||
| countyKANDIYOHI | -0.257 | 1.221 | ||||
| (0.199) | (0.535) | |||||
| countyKITTSON | 0.044 | 0.750 | ||||
| (0.199) | (0.579) | |||||
| countyKOOCHICHING | -0.062 | -0.021 | ||||
| (0.199) | (0.475) | |||||
| countyLAC QUI PARLE | 0.502 | 2.118 | ||||
| (0.199) | (0.655) | |||||
| countyLAKE | -0.299 | -0.438 | ||||
| (0.199) | (0.455) | |||||
| countyLAKE OF THE WOODS | -0.457 | 1.027 | ||||
| (0.199) | (0.535) | |||||
| countyLE SUEUR | 0.417 | 0.908 | ||||
| (0.199) | (0.507) | |||||
| countyLINCOLN | -0.153 | 1.475 | ||||
| (0.199) | (0.535) | |||||
| countyLYON | 0.002 | 1.127 | ||||
| (0.199) | (0.463) | |||||
| countyMAHNOMEN | 0.257 | 0.520 | ||||
| (0.199) | (0.846) | |||||
| countyMARSHALL | 0.137 | 0.762 | ||||
| (0.199) | (0.455) | |||||
| countyMARTIN | 1.012 | 0.200 | ||||
| (0.199) | (0.474) | |||||
| countyMCLEOD | 0.292 | 0.455 | ||||
| (0.199) | (0.433) | |||||
| countyMEEKER | 0.371 | 0.374 | ||||
| (0.199) | (0.508) | |||||
| countyMILLE LACS | 0.174 | 0.043 | ||||
| (0.199) | (0.655) | |||||
| countyMORRISON | 0.263 | 0.308 | ||||
| (0.199) | (0.455) | |||||
| countyMOWER | 0.433 | 0.862 | ||||
| (0.199) | (0.433) | |||||
| countyMURRAY | -0.352 | 1.653 | ||||
| (0.199) | (0.846) | |||||
| countyNICOLLET | -0.361 | 1.324 | ||||
| (0.199) | (0.535) | |||||
| countyNOBLES | 0.909 | 1.087 | ||||
| (0.199) | (0.578) | |||||
| countyNORMAN | 0.561 | 0.410 | ||||
| (0.199) | (0.578) | |||||
| countyOLMSTED | 0.407 | 0.466 | ||||
| (0.199) | (0.410) | |||||
| countyOTTER TAIL | 0.158 | 0.777 | ||||
| (0.199) | (0.463) | |||||
| countyPENNINGTON | 0.508 | 0.261 | ||||
| (0.199) | (0.579) | |||||
| countyPINE | 0.049 | -0.078 | ||||
| (0.199) | (0.488) | |||||
| countyPIPESTONE | -0.111 | 1.020 | ||||
| (0.199) | (0.535) | |||||
| countyPOLK | -0.372 | 0.881 | ||||
| (0.199) | (0.535) | |||||
| countyPOPE | 0.148 | 0.439 | ||||
| (0.199) | (0.655) | |||||
| countyRAMSEY | 0.375 | 0.318 | ||||
| (0.199) | (0.401) | |||||
| countyREDWOOD | 0.052 | 1.142 | ||||
| (0.199) | (0.507) | |||||
| countyRENVILLE | 0.327 | 0.830 | ||||
| (0.199) | (0.578) | |||||
| countyRICE | 0.445 | 1.007 | ||||
| (0.199) | (0.442) | |||||
| countyROCK | -0.317 | 0.459 | ||||
| (0.199) | (0.655) | |||||
| countyROSEAU | 0.117 | 0.825 | ||||
| (0.199) | (0.430) | |||||
| countySCOTT | 0.526 | 0.963 | ||||
| (0.199) | (0.433) | |||||
| countySHERBURNE | -0.130 | 0.249 | ||||
| (0.199) | (0.464) | |||||
| countySIBLEY | 0.493 | 0.402 | ||||
| (0.199) | (0.535) | |||||
| countyST LOUIS | 0.485 | 0.027 | ||||
| (0.199) | (0.385) | |||||
| countySTEARNS | -1.005 | 0.651 | ||||
| (0.199) | (0.407) | |||||
| countySTEELE | 0.237 | 0.739 | ||||
| (0.199) | (0.448) | |||||
| countySTEVENS | 0.094 | 0.951 | ||||
| (0.199) | (0.655) | |||||
| countySWIFT | 0.285 | 0.146 | ||||
| (0.199) | (0.535) | |||||
| countyTODD | 0.147 | 0.883 | ||||
| (0.199) | (0.578) | |||||
| countyTRAVERSE | -0.324 | 1.168 | ||||
| (0.199) | (0.535) | |||||
| countyWABASHA | 0.394 | 0.981 | ||||
| (0.199) | (0.474) | |||||
| countyWADENA | -0.527 | 0.445 | ||||
| (0.199) | (0.508) | |||||
| countyWASECA | 0.351 | -0.226 | ||||
| (0.199) | (0.535) | |||||
| countyWASHINGTON | 0.462 | 0.489 | ||||
| (0.199) | (0.394) | |||||
| countyWATONWAN | 0.073 | 1.869 | ||||
| (0.199) | (0.579) | |||||
| countyWILKIN | 0.669 | 1.389 | ||||
| (0.199) | (0.846) | |||||
| countyWINONA | -0.444 | 0.782 | ||||
| (0.199) | (0.433) | |||||
| countyWRIGHT | 0.469 | 0.805 | ||||
| (0.199) | (0.433) | |||||
| countyYELLOW MEDICINE | 0.463 | 0.346 | ||||
| (0.199) | (0.655) | |||||
| SD (Intercept county) | 0.372 | 0.349 | ||||
| SD (first_floor county) | 0.414 | 0.344 | ||||
| Cor (Intercept~first_floor county) | -0.449 | -0.337 | ||||
| SD (Observations) | 0.744 | 0.746 | ||||
| Num.Obs. | 2550 | 919 | 2550 | 919 | 2550 | 919 |
| R2 | 0.144 | 0.072 | 0.303 | 0.287 | ||
| R2 Adj. | 0.143 | 0.071 | 0.279 | 0.214 | ||
| R2 Marg. | 0.151 | 0.086 | ||||
| R2 Cond. | 0.336 | 0.257 | ||||
| AIC | 6362.3 | 2253.0 | 6003.8 | 2178.6 | 6014.3 | 2180.3 |
| BIC | 6379.9 | 2267.5 | 6512.2 | 2598.2 | 6049.3 | 2209.3 |
| ICC | 0.2 | 0.2 | ||||
| Log.Lik. | -3178.171 | -1123.512 | -2914.917 | -1002.310 | ||
| RMSE | 0.84 | 0.82 | 0.76 | 0.72 | 0.72 | 0.72 |
m.no_pool |>
predictions(newdata = datagrid(county = unique)) |>
as_tibble() |> inner_join(radon_mn_count, by = "county") |>
select(county, estimate, conf.low, conf.high, n.obs) |>
ggplot(
aes(
x = n.obs,
y = estimate,
ymin = conf.low,
ymax = conf.high
)
) +
geom_pointrange(
position = position_jitter(width = .5, height = 0)
) +
geom_hline(aes(yintercept = median(estimate))) +
theme_minimal() +
theme(axis.title = element_text(face="bold"), plot.title = element_text(face = "bold"), legend.position = "none") +
labs(x = "Number of observations (log 10 transformed)", y = "Estinmated log radon in the county", title = "No pooling (fixed effects) [real data]") +
scale_x_log10()
sm.no.pool |>
predictions(newdata = datagrid(county = unique)) |>
as_tibble() |> inner_join(radon_mn_count, by = "county") |>
select(county, estimate, conf.low, conf.high, n.obs) |>
ggplot(
aes(
x = n.obs,
y = estimate,
ymin = conf.low,
ymax = conf.high
)
) +
geom_pointrange(
position = position_jitter(width = .5, height = 0)
) +
geom_hline(aes(yintercept = median(estimate))) +
theme_minimal() +
theme(axis.title = element_text(face="bold"), plot.title = element_text(face = "bold"), legend.position = "none") +
labs(x = "Number of observations (log 10 transformed)", y = "Estinmated log radon in the county", title = "No pooling (fixed effects) [simulation data]") +
scale_x_log10()
m.partial_pool |>
predictions(newdata = datagrid(county = unique)) |>
as_tibble() |> inner_join(radon_mn_count, by = "county") |>
select(county, estimate, conf.low, conf.high, n.obs) |>
ggplot(
aes(
x = n.obs,
y = estimate,
ymin = conf.low,
ymax = conf.high
)
) +
geom_pointrange(
position = position_jitter(width = .5, height = 0)
) +
geom_hline(aes(yintercept = median(estimate))) +
theme_minimal() +
theme(axis.title = element_text(face="bold"), plot.title = element_text(face = "bold"), legend.position = "none") +
labs(x = "Number of observations (log 10 transformed)", y = "Estinmated log radon in the county", title = "Partial pooling (multilevel model) [real data]") +
scale_x_log10()
mdl_multilevel |>
predictions(newdata = datagrid(county = unique)) |>
as_tibble() |> inner_join(radon_mn_count, by = "county") |>
select(county, estimate, conf.low, conf.high, n.obs) |>
ggplot(
aes(
x = n.obs,
y = estimate,
ymin = conf.low,
ymax = conf.high
)
) +
geom_pointrange(
position = position_jitter(width = .5, height = 0)
) +
geom_hline(aes(yintercept = median(estimate))) +
theme_minimal() +
theme(axis.title = element_text(face="bold"), plot.title = element_text(face = "bold"), legend.position = "none") +
labs(x = "Number of observations (log 10 transformed)", y = "Estinmated log radon in the county", title = "Partial pooling (multilevel model) [simulation data]") +
scale_x_log10()
sm_pred_com_pool <- sm.compl.pool |>
predictions(
newdata = datagrid(
first_floor = c(0, 1),
county = c("BLUE EARTH", "CLAY","DAKOTA","DOUGLAS")
)
) |>
select(county, first_floor, estimate, conf.low, conf.high) |>
as_tibble( )
## Warning: Some of the variable names are missing from the model data: county
sm_pred_no_pool <- sm.no.pool |>
predictions(
newdata = datagrid(
first_floor = c(0, 1),
county = c("BLUE EARTH", "CLAY","DAKOTA","DOUGLAS")
)
) |>
select(county, first_floor, estimate, conf.low, conf.high) |>
as_tibble( )
sm_pred_multi <- sm.multilevel |>
predictions(
newdata = datagrid(
first_floor = c(0, 1),
county = c("BLUE EARTH", "CLAY","DAKOTA","DOUGLAS")
)
) |>
select(county, first_floor, estimate, conf.low, conf.high) |>
as_tibble( )
radon_mn |>
filter(county %in% c("BLUE EARTH", "CLAY","DAKOTA","DOUGLAS")) |>
ggplot(aes(x = first_floor, y = log.radon)) +
geom_point(size=3) +
geom_line(data = pred_no_pool, aes(first_floor, estimate), linetype = "dashed", size=1) +
geom_line(data = pred_multi, aes(first_floor, estimate), color="blue", size=1) +
geom_line(data = pred_com_pool, aes(first_floor, estimate), linetype = "dashed", color="red", size=2) +
facet_wrap(~county, ncol = 4)+
labs(title = "Real data") +
theme_minimal() +
theme(axis.title = element_text(face="bold"), plot.title = element_text(face = "bold"), legend.position = "none")
df1 |>
filter(county %in% c("BLUE EARTH", "CLAY","DAKOTA","DOUGLAS")) |>
ggplot(aes(x = first_floor, y = log.radon)) +
geom_point(size=3) +
geom_line(data = sm_pred_no_pool, aes(first_floor, estimate), linetype = "dashed", size=1) +
geom_line(data = sm_pred_multi, aes(first_floor, estimate), color="blue", size=1) +
geom_line(data = sm_pred_com_pool, aes(first_floor, estimate), linetype = "dashed", color="red", size=2) +
labs(title = "Simulation data") +
facet_wrap(~county, ncol = 4)+
theme_minimal() +
theme(axis.title = element_text(face="bold"), plot.title = element_text(face = "bold"), legend.position = "none")
Answer Q: Explore your synthetic data using similar
visualisations, and comment on similarities or differences between the
Radon dataset and your simulated dataset. A: The simulation data was
based on the multilevel model. In the regression analyisis, all
parameters are comparable except the intercept of pooling models between
simulation data and real data. As for fixing effect of no pooling model,
we got higher standard error in real data than simulation data. Also the
distribution is more euqal in simulation data.