# STATLAB Academy: first analysis of the Canadian macroeconomic teaching data # Version 1.0 - August 2026 # This script uses base R only. No package installation is required. candidate_paths <- c( file.path("data", "canada_macro_monthly.csv"), "canada_macro_monthly.csv" ) available_paths <- candidate_paths[file.exists(candidate_paths)] if (length(available_paths) == 0L) { stop( paste( "R cannot find canada_macro_monthly.csv.", "Open the extracted starter-pack folder as your project,", "or place the CSV in the current folder.", "Use getwd() and list.files(recursive = TRUE) to inspect your files." ) ) } data_path <- available_paths[[1L]] macro <- read.csv(data_path, stringsAsFactors = FALSE) required_columns <- c( "date", "cpi_all_items_2002_100", "inflation_yoy_percent", "policy_rate_percent", "usd_cad_monthly_average" ) missing_columns <- setdiff(required_columns, names(macro)) if (length(missing_columns) > 0L) { stop("Missing required columns: ", paste(missing_columns, collapse = ", ")) } macro$date <- as.Date(macro$date) if (anyNA(macro[required_columns])) { stop("The required data contain missing or invalid values.") } if (any(diff(macro$date) <= 0)) { stop("Dates must be unique and sorted from oldest to newest.") } if (nrow(macro) != 114L) { warning("Version 1.0 is expected to contain 114 observations.") } cat("\nSTATLAB Canadian macroeconomic teaching data\n") cat("File:", data_path, "\n") cat("Rows:", nrow(macro), "| Columns:", ncol(macro), "\n") cat("Coverage:", format(min(macro$date)), "to", format(max(macro$date)), "\n\n") cat("First six observations:\n") print(head(macro)) cat("\nVariable structure:\n") str(macro) cat("\nSummary statistics:\n") print(summary(macro)) latest <- tail(macro, 1L) cat("\nLatest observation:\n") print(latest) numeric_names <- required_columns[-1L] descriptive_table <- data.frame( variable = numeric_names, mean = vapply(macro[numeric_names], mean, numeric(1)), median = vapply(macro[numeric_names], median, numeric(1)), minimum = vapply(macro[numeric_names], min, numeric(1)), maximum = vapply(macro[numeric_names], max, numeric(1)), row.names = NULL ) cat("\nSelected descriptive statistics:\n") print(transform( descriptive_table, mean = round(mean, 3), median = round(median, 3), minimum = round(minimum, 3), maximum = round(maximum, 3) )) cat("\nCorrelation matrix (association, not causation):\n") print(round(cor(macro[numeric_names]), 3)) teaching_model <- lm( inflation_yoy_percent ~ policy_rate_percent + usd_cad_monthly_average, data = macro ) cat("\nTeaching-only linear regression:\n") print(summary(teaching_model)) cat( "\nInterpret carefully: these contemporaneous coefficients describe", "sample associations and are not causal effects.\n" ) plot_file <- "canadian_inflation_plot.png" png(plot_file, width = 1400, height = 800, res = 160) plot( macro$date, macro$inflation_yoy_percent, type = "l", lwd = 2.5, col = "#08747D", xlab = "Month", ylab = "Year-over-year inflation (%)", main = "Canadian inflation in the STATLAB teaching dataset" ) abline(h = 2, lty = 2, col = "#D96C4B") legend( "topleft", legend = c("Inflation", "2% reference"), col = c("#08747D", "#D96C4B"), lty = c(1, 2), lwd = c(2.5, 1), bty = "n" ) dev.off() recent_file <- "canada_macro_last_24_months.csv" write.csv(tail(macro, 24L), recent_file, row.names = FALSE) cat("\nFiles created:\n") cat("-", plot_file, "\n") cat("-", recent_file, "\n") cat("\nAnalysis complete.\n")