| 1 |
#' Tidying methods for spatially autoregressive models |
| 2 |
#' |
| 3 |
#' These methods tidy the coefficients of spatial autoregression |
| 4 |
#' models generated by functions in the `spatialreg` package. |
| 5 |
#' |
| 6 |
#' @param x An object returned from [spatialreg::lagsarlm()] |
| 7 |
#' or [spatialreg::errorsarlm()]. |
| 8 |
#' @template param_confint |
| 9 |
#' @template param_unused_dots |
| 10 |
#' |
| 11 |
#' @evalRd return_tidy(regression = TRUE) |
| 12 |
#' |
| 13 |
# skip running examples - occasionally over CRAN check time limit |
| 14 |
#' @examplesIf rlang::is_installed(c("spdep", "spatialreg")) && identical(Sys.getenv("NOT_CRAN"), "true") |
| 15 |
#' |
| 16 |
#' |
| 17 |
#' # load libraries for models and data |
| 18 |
#' library(spatialreg) |
| 19 |
#' library(spdep) |
| 20 |
#' |
| 21 |
#' # load data |
| 22 |
#' data(oldcol, package = "spdep") |
| 23 |
#' |
| 24 |
#' listw <- nb2listw(COL.nb, style = "W") |
| 25 |
#' |
| 26 |
#' # fit model |
| 27 |
#' crime_sar <- |
| 28 |
#' lagsarlm(CRIME ~ INC + HOVAL, |
| 29 |
#' data = COL.OLD, |
| 30 |
#' listw = listw, |
| 31 |
#' method = "eigen" |
| 32 |
#' ) |
| 33 |
#' |
| 34 |
#' # summarize model fit with tidiers |
| 35 |
#' tidy(crime_sar) |
| 36 |
#' tidy(crime_sar, conf.int = TRUE) |
| 37 |
#' glance(crime_sar) |
| 38 |
#' augment(crime_sar) |
| 39 |
#' |
| 40 |
#' # fit another model |
| 41 |
#' crime_sem <- errorsarlm(CRIME ~ INC + HOVAL, data = COL.OLD, listw) |
| 42 |
#' |
| 43 |
#' # summarize model fit with tidiers |
| 44 |
#' tidy(crime_sem) |
| 45 |
#' tidy(crime_sem, conf.int = TRUE) |
| 46 |
#' glance(crime_sem) |
| 47 |
#' augment(crime_sem) |
| 48 |
#' |
| 49 |
#' # fit another model |
| 50 |
#' crime_sac <- sacsarlm(CRIME ~ INC + HOVAL, data = COL.OLD, listw) |
| 51 |
#' |
| 52 |
#' # summarize model fit with tidiers |
| 53 |
#' tidy(crime_sac) |
| 54 |
#' tidy(crime_sac, conf.int = TRUE) |
| 55 |
#' glance(crime_sac) |
| 56 |
#' augment(crime_sac) |
| 57 |
#' |
| 58 |
#' @aliases spatialreg_tidiers |
| 59 |
#' @export |
| 60 |
#' @family spatialreg tidiers |
| 61 |
#' @seealso [tidy()], [spatialreg::lagsarlm()], [spatialreg::errorsarlm()], |
| 62 |
#' [spatialreg::sacsarlm()] |
| 63 |
tidy.sarlm <- function(x, conf.int = FALSE, conf.level = .95, ...) { |
| 64 |
check_ellipses("exponentiate", "tidy", "sarlm", ...) |
| 65 |
|
| 66 |
# construct parameter table |
| 67 |
s <- summary(x) |
| 68 |
ret <- as_tidy_tibble( |
| 69 |
coef(s), |
| 70 |
new_names = c("estimate", "std.error", "statistic", "p.value") |
| 71 |
) |
| 72 |
|
| 73 |
# append spatial autoregression coefficient to parameter table if it exists |
| 74 |
if (!is.null(s$rho)) { |
| 75 |
rho <- tibble( |
| 76 |
term = "rho", |
| 77 |
estimate = as.numeric(s$rho), |
| 78 |
std.error = as.numeric(s$rho.se), |
| 79 |
statistic = as.numeric(estimate / std.error), |
| 80 |
p.value = as.numeric(2 * (1 - pnorm(abs(statistic)))) |
| 81 |
) |
| 82 |
ret <- bind_rows(rho, ret) |
| 83 |
} |
| 84 |
|
| 85 |
# append spatial error coefficient to parameter table if it exists |
| 86 |
if (!is.null(s$lambda)) { |
| 87 |
lambda <- tibble( |
| 88 |
term = "lambda", |
| 89 |
estimate = as.numeric(s$lambda), |
| 90 |
std.error = as.numeric(s$lambda.se), |
| 91 |
statistic = as.numeric(estimate / std.error), |
| 92 |
p.value = as.numeric(2 * (1 - pnorm(abs(statistic)))) |
| 93 |
) |
| 94 |
ret <- bind_rows(ret, lambda) |
| 95 |
} |
| 96 |
|
| 97 |
# Calculate confidence interval |
| 98 |
if (conf.int) { |
| 99 |
ci <- broom_confint_terms(x, level = conf.level) |
| 100 |
ret <- dplyr::left_join(ret, ci, by = "term") |
| 101 |
} |
| 102 |
|
| 103 |
ret |
| 104 |
} |
| 105 |
|
| 106 |
#' @templateVar class spatialreg |
| 107 |
#' @template title_desc_augment |
| 108 |
#' |
| 109 |
#' @evalRd return_augment(".fitted", ".resid") |
| 110 |
#' |
| 111 |
#' @inherit tidy.sarlm params examples |
| 112 |
#' @param data Ignored, but included for internal consistency. See the details |
| 113 |
#' below. |
| 114 |
#' |
| 115 |
#' @details |
| 116 |
#' The predict method for sarlm objects assumes that the response is |
| 117 |
#' known. See ?predict.sarlm for more discussion. As a result, since the |
| 118 |
#' original data can be recovered from the fit object, this method |
| 119 |
#' currently does not take in `data` or `newdata` arguments. |
| 120 |
#' |
| 121 |
#' @export |
| 122 |
#' @seealso [augment()] |
| 123 |
#' @family spatialreg tidiers |
| 124 |
augment.sarlm <- function(x, data = x$X, ...) { |
| 125 |
observed_name <- all.vars(x$call$formula)[1] |
| 126 |
|
| 127 |
reg <- x$X |> |
| 128 |
as_augment_tibble() |> |
| 129 |
dplyr::select(-.rownames) |> |
| 130 |
dplyr::mutate( |
| 131 |
!!observed_name := x$y, |
| 132 |
.fitted = x$fitted.values, |
| 133 |
.resid = x$residuals |
| 134 |
) |
| 135 |
|
| 136 |
reg |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
#' @templateVar class spatialreg |
| 141 |
#' @template title_desc_glance |
| 142 |
#' |
| 143 |
#' @inherit tidy.sarlm params examples |
| 144 |
#' |
| 145 |
#' @evalRd return_glance( |
| 146 |
#' "logLik", |
| 147 |
#' "AIC", |
| 148 |
#' "BIC", |
| 149 |
#' "deviance", |
| 150 |
#' "logLik", |
| 151 |
#' "nobs" |
| 152 |
#' ) |
| 153 |
#' @export |
| 154 |
#' @family spatialreg tidiers |
| 155 |
#' @seealso [glance()], [spatialreg::lagsarlm()], [spatialreg::errorsarlm()], |
| 156 |
#' [spatialreg::sacsarlm()] |
| 157 |
glance.sarlm <- function(x, ...) { |
| 158 |
res <- as_glance_tibble( |
| 159 |
# Using Pseudo R squared. |
| 160 |
r.squared = stats::cor(x$fitted.values, x$y)^2, |
| 161 |
AIC = stats::AIC(x), |
| 162 |
BIC = stats::BIC(x), |
| 163 |
deviance = stats::deviance(x), |
| 164 |
logLik = as.numeric(x$LL), |
| 165 |
nobs = length(x$fitted.values), |
| 166 |
na_types = "rrrrri" |
| 167 |
) |
| 168 |
|
| 169 |
res |
| 170 |
} |
| 171 |
|
| 172 |
# re-export each of them following the change in class sarlm -> Sarlm -------- |
| 173 |
|
| 174 |
#' @export |
| 175 |
#' @family spatialreg tidiers |
| 176 |
tidy.Sarlm <- tidy.sarlm |
| 177 |
|
| 178 |
#' @export |
| 179 |
#' @family spatialreg tidiers |
| 180 |
glance.Sarlm <- glance.sarlm |
| 181 |
|
| 182 |
#' @export |
| 183 |
#' @family spatialreg tidiers |
| 184 |
augment.Sarlm <- augment.sarlm |