## ----include = FALSE---------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4.5 ) options(hcinfer.use_emoji = FALSE) ## ----residual-compression-plot, fig.alt = "Line plot showing that the fraction of local variance captured by the squared OLS residual decreases from 1 to 0 as leverage increases from 0 to 1."---- compression <- data.frame( leverage = seq(0, 0.9, length.out = 100) ) compression$fraction <- 1 - compression$leverage high_leverage_point <- data.frame( leverage = 0.65, fraction = 0.35 ) ggplot2::ggplot(compression, ggplot2::aes(x = leverage, y = fraction)) + ggplot2::geom_line(color = "#2c5f8a", linewidth = 1) + ggplot2::geom_point( data = high_leverage_point, color = "#c0392b", size = 2.6 ) + ggplot2::annotate( "text", x = 0.65, y = 0.42, label = "h[t] == 0.65", parse = TRUE, hjust = 0.5, color = "#7f1d1d" ) + ggplot2::scale_x_continuous(limits = c(0, 0.9)) + ggplot2::scale_y_continuous(limits = c(0, 1)) + ggplot2::labs( x = expression(h[t]), y = expression("Approximate fraction captured, " * 1 - h[t]) ) + ggplot2::theme_minimal(base_size = 12) ## ----available-methods-------------------------------------------------------- library(hcinfer) hc_methods() ## ----weight-comparison-plot, fig.alt = "Faceted scatterplot comparing HC adjustment factors against leverage for HC3, HC4, HC4m, and HCbeta in the public schools model."---- schools <- PublicSchools schools$income_scaled <- schools$income / 10000 schools$income_scaled_sq <- schools$income_scaled^2 fit <- lm(expenditure ~ income_scaled + income_scaled_sq, data = schools) methods <- c("hc3", "hc4", "hc4m", "hcbeta") weight_data <- purrr::map(methods, function(method) { cov <- vcov_hc(fit, type = method) data.frame( method = cov$label, leverage = unname(cov$leverage), weight = unname(cov$weights) ) }) weight_data <- do.call(rbind, weight_data) weight_data$method <- factor( weight_data$method, levels = c("HC3", "HC4", "HC4m", "HCbeta") ) ggplot2::ggplot(weight_data, ggplot2::aes(x = leverage, y = weight)) + ggplot2::geom_point(color = "#2c5f8a", alpha = 0.85, size = 1.8) + ggplot2::facet_wrap(~method, scales = "free_y", ncol = 2) + ggplot2::labs( x = expression(h[t]), y = expression(g[t]) ) + ggplot2::theme_minimal(base_size = 12) + ggplot2::theme( panel.grid.minor = ggplot2::element_blank(), strip.text = ggplot2::element_text(face = "bold") )