Memuat
Memuat
Bayesian Regression (rstanarm) for Education and Learning
0 views
Estimating the posterior distribution of the effect of programme duration and baseline literacy score on literacy score gain with Bayesian regression (the analysis is illustrated with synthetic data). Bayesian regression estimates the regression coefficients as posterior distributions that combine a prior with the data, reported as posterior medians and credible intervals. It suits theses and dissertations with small to moderate samples, or when researchers want to state the probability of an effect directly or use prior information.

Estimating the posterior distribution of the effect of programme duration and baseline literacy score on literacy score gain with Bayesian regression (the analysis is illustrated with synthetic data).
| Role | Variable | Unit |
|---|---|---|
| Main | Literacy score gain | score |
| Supporting 1 | Programme duration | weeks |
| Supporting 2 | Baseline literacy score | score |
The posterior median effect of Programme duration on Literacy score gain is 1.890 (95% CrI 1.628 to 2.182) and the probability of a positive effect is 1.000, so its direction is very clear. The Baseline literacy score coefficient has a median of -0.182. Bayesian R2 is 0.378; max Rhat 1.004 and min ESS 718 show the chains have converged well.
Computed from illustrative synthetic data, not from a real study.
Bayesian regression estimates the regression coefficients as posterior distributions that combine a prior with the data, reported as posterior medians and credible intervals. It suits theses and dissertations with small to moderate samples, or when researchers want to state the probability of an effect directly or use prior information.
Report each coefficient's posterior median with its 95% credible interval and interpret the interval as the range of most plausible effect values. Add the posterior probability of a positive or negative effect, Bayesian R2, convergence evidence (Rhat, ESS) and the posterior predictive check.
A short starting script with this project's variable names. The delivered version is adapted to your data and fully commented.
# Contoh kode R: Bayesian Regression (rstanarm)
suppressPackageStartupMessages({
library(ggplot2)
library(patchwork)
library(rstanarm)
})
set.seed(42)
# ---- Gaya grafik ----
pal <- c("#1f6feb", "#e8590c", "#2f9e44", "#ae3ec9", "#f59f00")
warna <- list(bg = "#f4f6fa", panel = "#ffffff", teks = "#1f2933", redup = "#5f6b78", grid = "#e3e8ee", bar = "#1d3557")
tata <- 1
theme_set(theme_minimal(base_size = 11) + theme(legend.position = "bottom"))
# ---- Data sintetis ----
n <- 200
d <- data.frame(programme_duration = rnorm(n, 50, 10),
baseline_literacy_score = rnorm(n, 30, 6))
z1 <- as.numeric(scale(d$programme_duration))
z2 <- as.numeric(scale(d$baseline_literacy_score))
b1 <- runif(1, 0.4, 0.8) * sample(c(-1, 1), 1, prob = c(0.25, 0.75))
b2 <- runif(1, 0.12, 0.45) * sample(c(-1, 1), 1)
d$literacy_score_gain <- 100 + 15 * (b1 * z1 + b2 * z2 + rnorm(n, 0, runif(1, 0.5, 0.85)))
write.csv(d, "data_contoh.csv", row.names = FALSE)
# ---- Analisis: regresi linear Bayesian (rstanarm, MCMC) ----
# Prior normal lemah (autoscale menyesuaikan skala data), 2 rantai x 1500 iterasi (750 warmup)
fit <- stan_glm(literacy_score_gain ~ programme_duration + baseline_literacy_score, data = d, family = gaussian(),
prior = normal(0, 2.5, autoscale = TRUE),
prior_intercept = normal(0, 5, autoscale = TRUE),
prior_aux = exponential(1, autoscale = TRUE),
chains = 2, iter = 1500, refresh = 0, cores = 1, seed = sample.int(1e6, 1))
print(fit, digits = 3)
sm <- summary(fit, probs = c(0.025, 0.5, 0.975), digits = 3)
print(sm)
ci <- posterior_interval(fit, prob = 0.95) # interval kredibel 95%
print(ci)
post <- as.matrix(fit) # draw posterior (baris = draw)
med <- apply(post, 2, median)
rhat_maks <- max(sm[, "Rhat"], na.rm = TRUE) # konvergensi: idealnya < 1.01
ess_min <- min(sm[, "n_eff"], na.rm = TRUE) # ukuran sampel efektif
r2_draw <- bayes_R2(fit) # Bayesian R2 per draw
p_pos1 <- mean(post[, "programme_duration"] > 0) # peluang posterior efek positif
p_pos2 <- mean(post[, "baseline_literacy_score"] > 0)
cat("Median Bayesian R2:", round(median(r2_draw), 3), "\n")
cat("P(koefisien programme_duration > 0):", round(p_pos1, 3), " P(koefisien baseline_literacy_score > 0):", round(p_pos2, 3), "\n")
# ---- Grafik ----
# 1) densitas posterior koefisien dengan interval kredibel 95%
koef <- c("programme_duration", "baseline_literacy_score")
lab_koef <- make.unique(c("programme_duration", "baseline_literacy_score"), sep = " ")
dp <- do.call(rbind, lapply(1:2, function(j) {
dz <- density(post[, koef[j]], n = 256)
data.frame(koef = lab_koef[j], x = dz$x, y = dz$y,
dalam = dz$x >= quantile(post[, koef[j]], 0.025) & dz$x <= quantile(post[, koef[j]], 0.975))
}))
dp$koef <- factor(dp$koef, levels = lab_koef)
dm <- data.frame(koef = factor(lab_koef, levels = lab_koef), m = apply(post[, koef], 2, median))
p1 <- ggplot(dp, aes(x, y)) +
geom_area(data = subset(dp, dalam), fill = pal[1], alpha = 0.35) +
geom_line(colour = pal[1], linewidth = 0.9) +
geom_vline(data = dm, aes(xintercept = m), colour = pal[2], linewidth = 0.9) +
geom_vline(xintercept = 0, linetype = "dashed", colour = warna$redup) +
facet_wrap(~koef, scales = "free", ncol = 1) +
labs(title = "Posterior of coefficients (95% CrI shaded)", x = "Coefficient value", y = "Posterior density")
# 2) posterior predictive check: densitas y amatan vs 40 replikasi
yrep <- posterior_predict(fit, draws = 40)
dr <- do.call(rbind, lapply(seq_len(nrow(yrep)), function(i) {
dz <- density(yrep[i, ], n = 128)
data.frame(rep = i, x = dz$x, y = dz$y)
}))
dy <- density(d$literacy_score_gain, n = 256)
p2 <- ggplot() +
geom_line(data = dr, aes(x, y, group = rep, colour = "Replicated"), alpha = 0.3, linewidth = 0.4) +
geom_line(data = data.frame(x = dy$x, y = dy$y), aes(x, y, colour = "Observed"), linewidth = 1.1) +
scale_colour_manual(values = c(Observed = warna$teks, Replicated = pal[3]), name = NULL) +
labs(title = "Posterior predictive check", x = "literacy_score_gain", y = "Density")
# 3) trace plot koefisien programme_duration per rantai
arr <- as.array(fit)
dt <- data.frame(iter = rep(seq_len(dim(arr)[1]), 2),
rantai = factor(rep(1:2, each = dim(arr)[1])),
nilai = c(arr[, 1, "programme_duration"], arr[, 2, "programme_duration"]))
p3 <- ggplot(dt, aes(iter, nilai, colour = rantai)) +
geom_line(alpha = 0.7, linewidth = 0.35) +
scale_colour_manual(values = pal[c(1, 4)], name = "Chain") +
labs(title = "Trace of programme_duration coefficient", x = "Post-warmup iteration", y = "Coefficient")
p_hasil <- if (tata == 1) p1 | (p2 / p3) else (p1 | p2) / p3 + plot_layout(heights = c(1.4, 1))
print(p_hasil)
The original project file is available. It is not just the picture above: you get the working R project. It can be downloaded after payment, and we send it by WhatsApp or email.
ZIP: analisis.R + data_contoh.csv + BACA-DULU.txt
Choose how much help you need. Each higher package includes everything in the lower ones. Prices exclude 11% VAT.
Package 1
US$28
excl. 11% VAT
Duration: 7 working days
Output: R files + result figures
Deliverables
Package 2 · most chosen
US$81
excl. 11% VAT
Duration: 13 working days
Output: Code + analysis + interpretation notes
Includes all of Package 1, plus
Package 3
US$178
excl. 11% VAT
Duration: 18-21 working days
Output: Code + analysis + guidance until ready to present
Includes all of Packages 1 & 2, plus
Yes. The original project file is a ZIP containing the complete analisis.R script, a data_contoh.csv sample dataset and a BACA-DULU.txt guide. It can be downloaded after payment, and we send it by WhatsApp or email. For your own data (Package 2 and 3) the script is adapted to your dataset.
Yes. Bayesian Regression (rstanarm) is commonly used at this level in Education and Learning. We adapt the analysis to your research question, your own data and your supervisor's requirements.
No. The sheet is computed from illustrative synthetic data so you can see the kind of output R Studio gives. For your order we run the same method on your own data.
That is the point of the packages. Every script is commented, and Package 3 includes two coaching sessions plus practice questions so you can explain the method and the results at your seminar and defence.
Each project has three packages: Package 1 (code and explanation) US$28, Package 2 (your data analysis and interpretation) US$81 and Package 3 (full guidance) US$178, excluding 11% VAT. Larger datasets, extra methods or tight deadlines adjust the quote after a short consultation.
The result sheet uses illustrative synthetic data. R is a programming language of the R Foundation project; RStudio and Posit are trademarks of Posit PBC.
Back to R Studio portfolio