Memuat
Memuat
VAR and Granger Causality for Education and Learning
0 views
Testing dynamic links and Granger causality among scholarship applicants, scholarship budget and open unemployment rate with a VAR (the analysis is illustrated with synthetic data). VAR (Vector Autoregression) models several stationary time series that influence one another, and Granger causality tests, impulse responses and variance decompositions reveal the direction and size of the dynamic effects. It suits theses studying two-way links among macro, financial, health or environmental variables measured monthly or quarterly.

Testing dynamic links and Granger causality among scholarship applicants, scholarship budget and open unemployment rate with a VAR (the analysis is illustrated with synthetic data).
| Role | Variable | Unit |
|---|---|---|
| Main | Scholarship applicants | count |
| Supporting 1 | Scholarship budget | IDR billion |
| Supporting 2 | Open unemployment rate | % |
The AIC-selected VAR(2) model is stable (maximum root modulus 0.618). Scholarship budget Granger-causes Scholarship applicants (p < 0.001), and the effect of Open unemployment rate is significant (p < 0.001). At horizon 10, shocks to Scholarship budget explain 39.7% of the forecast error variance of Scholarship applicants.
Computed from illustrative synthetic data, not from a real study.
VAR (Vector Autoregression) models several stationary time series that influence one another, and Granger causality tests, impulse responses and variance decompositions reveal the direction and size of the dynamic effects. It suits theses studying two-way links among macro, financial, health or environmental variables measured monthly or quarterly.
Report the selected lag length, the stability check and the Portmanteau test as diagnostics. A Granger p-value below 0.05 means the lags of that variable help forecast Y; the IRF shows the direction and duration of the response, and the FEVD gives the share of Y's forecast error variance explained by each shock.
A short starting script with this project's variable names. The delivered version is adapted to your data and fully commented.
# Contoh kode R: VAR and Granger Causality
suppressPackageStartupMessages({
library(ggplot2)
library(patchwork)
library(vars)
library(car)
})
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: tiga deret bulanan dari proses VAR(2) yang stasioner ----
nt <- 120
# matriks koefisien lag 1 dan lag 2; diulang sampai prosesnya stabil (semua akar < 0.9)
repeat {
A1 <- diag(runif(3, 0.3, 0.55))
A1[1, 2] <- runif(1, 0.25, 0.45) # X1 -> Y
A1[1, 3] <- runif(1, 0.1, 0.35) * sample(c(-1, 1), 1) # X2 -> Y
A1[2, 3] <- runif(1, -0.15, 0.15)
A1[3, 2] <- runif(1, -0.1, 0.2)
A2 <- diag(runif(3, -0.15, 0.15))
A2[1, 2] <- runif(1, 0.05, 0.2)
komp <- rbind(cbind(A1, A2), cbind(diag(3), matrix(0, 3, 3))) # matriks pendamping (companion)
if (max(Mod(eigen(komp)$values)) < 0.9) break
}
z <- matrix(0, nt + 50, 3)
for (i in 3:(nt + 50)) z[i, ] <- A1 %*% z[i - 1, ] + A2 %*% z[i - 2, ] + rnorm(3, 0, c(0.6, 1, 1))
z <- scale(z[-(1:50), ]) # buang periode pemanasan (burn-in)
d <- data.frame(periode = seq(as.Date("2015-01-01"), by = "month", length.out = nt),
scholarship_applicants = round(pmax(100 + 15 * z[, 1], 0), 3),
scholarship_budget = round(pmax(50 + 10 * z[, 2], 0), 3),
open_unemployment_rate = round(pmax(30 + 6 * z[, 3], 0), 3))
write.csv(d, "data_contoh.csv", row.names = FALSE)
# ---- Analisis: Vector Autoregression (VAR) ----
dv <- d[, c("scholarship_applicants", "scholarship_budget", "open_unemployment_rate")]
sel <- VARselect(dv, lag.max = 6, type = "const") # pemilihan panjang lag
print(sel$selection)
p_lag <- max(1, as.integer(sel$selection[["AIC(n)"]]))
fit <- do.call(VAR, list(y = dv, p = p_lag, type = "const")) # do.call: nilai lag tersimpan di objek
print(summary(fit))
# ---- Diagnostik: stabilitas dan autokorelasi residual ----
akar <- roots(fit) # modulus akar; < 1 berarti VAR stabil
st <- serial.test(fit, lags.pt = 12, type = "PT.asymptotic") # uji Portmanteau
print(akar); print(st)
# ---- Kausalitas Granger ----
# uji Wald pada persamaan scholarship_applicants: apakah semua lag X1 (atau X2) bersama-sama nol?
eq_y <- fit$varresult$scholarship_applicants
gr_x1 <- car::linearHypothesis(eq_y, paste0("scholarship_budget.l", 1:p_lag, " = 0"))
gr_x2 <- car::linearHypothesis(eq_y, paste0("open_unemployment_rate.l", 1:p_lag, " = 0"))
p_x1 <- gr_x1[2, "Pr(>F)"]; p_x2 <- gr_x2[2, "Pr(>F)"]
print(gr_x1); print(gr_x2)
print(causality(fit, cause = "scholarship_budget")$Granger) # versi blok (X1 -> semua variabel lain)
# ---- Impulse response function (IRF) dan dekomposisi varians (FEVD) ----
ir <- irf(fit, impulse = c("scholarship_budget", "open_unemployment_rate"), response = "scholarship_applicants", n.ahead = 10,
ortho = TRUE, boot = TRUE, runs = 200)
fv <- fevd(fit, n.ahead = 10)
print(round(fv$scholarship_applicants, 3))
# ---- Grafik ----
nm3 <- make.unique(c("scholarship_applicants", "scholarship_budget", "open_unemployment_rate"), sep = " ")
dl <- data.frame(periode = rep(d$periode, 3), nilai = c(d$scholarship_applicants, d$scholarship_budget, d$open_unemployment_rate),
seri = factor(rep(nm3, each = nt), levels = nm3))
p1 <- ggplot(dl, aes(periode, nilai, colour = seri)) +
geom_line(linewidth = 0.7, show.legend = FALSE) +
facet_wrap(~seri, ncol = if (tata == 1) 1 else 3, scales = "free_y") +
scale_colour_manual(values = pal[1:3]) +
labs(title = "The three series", x = NULL, y = NULL)
kej <- c("scholarship_budget", "open_unemployment_rate")
lab_s <- make.unique(c("Shock: scholarship_budget", "Shock: open_unemployment_rate"), sep = " ")
di <- do.call(rbind, lapply(1:2, function(i) data.frame(
h = 0:10, irf = ir$irf[[kej[i]]][, 1], lo = ir$Lower[[kej[i]]][, 1], hi = ir$Upper[[kej[i]]][, 1],
guncangan = lab_s[i])))
di$guncangan <- factor(di$guncangan, levels = lab_s)
p2 <- ggplot(di, aes(h, irf)) +
geom_hline(yintercept = 0, colour = warna$redup) +
geom_ribbon(aes(ymin = lo, ymax = hi, fill = guncangan), alpha = 0.22, show.legend = FALSE) +
geom_line(aes(colour = guncangan), linewidth = 1, show.legend = FALSE) +
facet_wrap(~guncangan, ncol = 2) +
scale_colour_manual(values = pal[2:3]) + scale_fill_manual(values = pal[2:3]) +
scale_x_continuous(breaks = seq(0, 10, 2)) +
labs(title = "Orthogonal IRF of scholarship_applicants (95% bootstrap)", x = "Horizon", y = "Response")
nm_s <- make.unique(c("scholarship_applicants", "scholarship_budget", "open_unemployment_rate"), sep = " ")
dfv <- data.frame(h = rep(1:10, 3), porsi = 100 * c(fv$scholarship_applicants),
sumber = factor(rep(nm_s, each = 10), levels = nm_s))
p3 <- ggplot(dfv, aes(factor(h), porsi, fill = sumber)) +
geom_col(width = 0.75, colour = warna$panel, linewidth = 0.3) +
scale_fill_manual(values = pal[1:3], name = "Shock") +
labs(title = "FEVD of scholarship_applicants", x = "Horizon", y = "Share of variance (%)")
p_hasil <- if (tata == 1) p1 | (p2 / p3) else p1 / (p2 | p3)
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$25
excl. 11% VAT
Duration: 5 working days
Output: R files + result figures
Deliverables
Package 2 · most chosen
US$69
excl. 11% VAT
Duration: 9 working days
Output: Code + analysis + interpretation notes
Includes all of Package 1, plus
Package 3
US$153
excl. 11% VAT
Duration: 14-17 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. VAR and Granger Causality 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$25, Package 2 (your data analysis and interpretation) US$69 and Package 3 (full guidance) US$153, 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