Resume where you left off
Online at https://diogoribeiro7.github.io/analytics-blog-jekyll/2024/04/03/r-statistical-analysis-visualizations/
Topics
R remains a powerhouse for statistical modeling. This walkthrough fits a varying-intercept model with lme4, then layers diagnostic charts created with ggplot2 to verify assumptions.
Load packages and data
1
2
3
4
5
library(tidyverse)
library(lme4)
metrics <- read_csv("data/store-conversion.csv")
metrics <- metrics |> mutate(period = as.Date(period))
Fit a multilevel model
1
2
model <- lmer(conversion_rate ~ campaign_spend + (1 | region), data = metrics)
summary(model)
The random intercept term captures regional heterogeneity while partial pooling shrinks noisy estimates toward the grand mean.
Visualize partial pooling
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
library(broom.mixed)
coefs <- broom.mixed::ranef(model, condVar = TRUE)$region |> tibble::rownames_to_column("region")
posterior <- coefs |> mutate(
estimate = `(Intercept)`[, "condval"],
se = sqrt(`(Intercept)`[, "condvar"])
)
posterior_plot <- posterior |>
ggplot(aes(x = estimate, y = fct_reorder(region, estimate))) +
geom_point(color = "#1b9e77", size = 2.6) +
geom_errorbarh(aes(xmin = estimate - 2 * se, xmax = estimate + 2 * se), height = 0.15) +
labs(
title = "Regional baseline conversion rates",
x = "Log-odds",
y = NULL
) +
theme_minimal(base_size = 14)
ggplot2::ggsave("assets/images/posts/conversion-region-effects.png", posterior_plot, width = 8, height = 6, dpi = 144)

Exporting figures directly from the R session ensures the rendered asset is tracked in version control. Pair the static PNG with an Observable embed for interactive exploration:
Share reproducibility artifacts
- Commit the R script or Quarto document to
_notebooks/. - Publish the model summary as a downloadable CSV in
_datasets/. - Capture session info with
sessionInfo()for audit trails.
The article mixes syntax highlighting, figure embeds, and Observable visualizations so readers can scrutinize both the statistical rigor and presentation quality.
Your private highlights
Kept in this browser only and never sent anywhere. These are your notes, not comments. With text selected, Alt+Shift+H highlights it and Alt+Shift+N adds a note.
This browser is not letting the site keep data, so saving, progress and highlights are off.
Select a passage of the article to highlight it.
Your reading data
Embed interactive plots, widgets, and demos using <figure>, <iframe>, or <div class="interactive-embed"> containers. Ensure each embed includes descriptive captions for accessibility.
© 2024 Diogo Ribeiro. Text and figures under CC BY 4.0.
How to cite
Use the quick export buttons to save citations for reference managers or copy the formatted text directly.
Diogo Ribeiro (2024). Multilevel Modeling in R with ggplot2 Diagnostics. DataLog | Data Science & Research Theme. https://diogoribeiro7.github.io/analytics-blog-jekyll/2024/04/03/r-statistical-analysis-visualizations/.
BibTeX
RIS
EndNote
Open science & reproducibility badges
These badges highlight the transparency practices applied to this work. Hover or focus on each badge to learn more about the criteria.
- Open Data Dataset and code repository published with permissive license. Public repository, DOI issued, README with reproduction steps.
- Reproducible Workflow Containerized environment and automated tests provided. Continuous integration pipeline with reproducibility checks.
- Transparent Peer Review Peer review reports archived with DOI and linked to article. Open peer review statement and archived reports on Zenodo.