| 1 |
#' @templateVar class ergm |
| 2 |
#' @template title_desc_tidy |
| 3 |
#' |
| 4 |
#' @description The methods should work with any model that conforms to |
| 5 |
#' the \pkg{ergm} class, such as those produced from weighted networks by the |
| 6 |
#' \pkg{ergm.count} package. |
| 7 |
#' |
| 8 |
#' @param x An `ergm` object returned from a call to [ergm::ergm()]. |
| 9 |
#' @template param_confint |
| 10 |
#' @template param_exponentiate |
| 11 |
#' @param ... Additional arguments to pass to [ergm::summary()]. |
| 12 |
#' **Cautionary note**: Misspecified arguments may be silently ignored. |
| 13 |
#' |
| 14 |
#' |
| 15 |
#' @return A [tibble::tibble] with one row for each coefficient in the |
| 16 |
#' exponential random graph model, with columns: |
| 17 |
#' \item{term}{The term in the model being estimated and tested} |
| 18 |
#' \item{estimate}{The estimated coefficient} |
| 19 |
#' \item{std.error}{The standard error} |
| 20 |
#' \item{mcmc.error}{The MCMC error} |
| 21 |
#' \item{p.value}{The two-sided p-value} |
| 22 |
#' |
| 23 |
#' @examplesIf rlang::is_installed("ergm") |
| 24 |
#' |
| 25 |
#' # load libraries for models and data |
| 26 |
#' library(ergm) |
| 27 |
#' |
| 28 |
#' # load the Florentine marriage network data |
| 29 |
#' data(florentine) |
| 30 |
#' |
| 31 |
#' # fit a model where the propensity to form ties between |
| 32 |
#' # families depends on the absolute difference in wealth |
| 33 |
#' gest <- ergm(flomarriage ~ edges + absdiff("wealth")) |
| 34 |
#' |
| 35 |
#' # show terms, coefficient estimates and errors |
| 36 |
#' tidy(gest) |
| 37 |
#' |
| 38 |
#' # show coefficients as odds ratios with a 99% CI |
| 39 |
#' tidy(gest, exponentiate = TRUE, conf.int = TRUE, conf.level = 0.99) |
| 40 |
#' |
| 41 |
#' # take a look at likelihood measures and other |
| 42 |
#' # control parameters used during MCMC estimation |
| 43 |
#' glance(gest) |
| 44 |
#' glance(gest, deviance = TRUE) |
| 45 |
#' glance(gest, mcmc = TRUE) |
| 46 |
#' |
| 47 |
#' @references Hunter DR, Handcock MS, Butts CT, Goodreau SM, Morris M (2008b). |
| 48 |
#' \pkg{ergm}: A Package to Fit, Simulate and Diagnose Exponential-Family |
| 49 |
#' Models for Networks. *Journal of Statistical Software*, 24(3). |
| 50 |
#' <https://www.jstatsoft.org/v24/i03/>. |
| 51 |
#' |
| 52 |
#' @export |
| 53 |
#' @aliases ergm_tidiers |
| 54 |
#' @seealso [tidy()], [ergm::ergm()], [ergm::control.ergm()], |
| 55 |
#' [ergm::summary()] |
| 56 |
#' @family ergm tidiers |
| 57 |
tidy.ergm <- function( |
| 58 |
x, |
| 59 |
conf.int = FALSE, |
| 60 |
conf.level = 0.95, |
| 61 |
exponentiate = FALSE, |
| 62 |
... |
| 63 |
) { |
| 64 |
# in ergm 3.9 summary(x, ...)$coefs has columns: |
| 65 |
# Estimate, Std. Error, MCMC %, Pr(>|Z|) |
| 66 |
|
| 67 |
# in ergm 3.10 summary(x, ...)$coefs has columns: |
| 68 |
# Estimate, Std. Error, MCMC %, z value, Pr(>|Z|) |
| 69 |
|
| 70 |
ret <- summary(x, ...)$coefficients |> |
| 71 |
tibble::as_tibble(rownames = "term") |> |
| 72 |
rename2( |
| 73 |
term = "term", |
| 74 |
estimate = "Estimate", |
| 75 |
std.error = "Std. Error", |
| 76 |
mcmc.error = "MCMC %", |
| 77 |
statistic = "z value", |
| 78 |
p.value = "Pr(>|z|)" |
| 79 |
) |
| 80 |
|
| 81 |
if (conf.int) { |
| 82 |
z <- stats::qnorm(1 - (1 - conf.level) / 2) |
| 83 |
ret$conf.low <- ret$estimate - z * ret$std.error |
| 84 |
ret$conf.high <- ret$estimate + z * ret$std.error |
| 85 |
} |
| 86 |
|
| 87 |
if (exponentiate) { |
| 88 |
if ( |
| 89 |
is.null(x$glm) || |
| 90 |
(x$glm$family$link != "logit" && x$glm$family$link != "log") |
| 91 |
) { |
| 92 |
cli::cli_warn( |
| 93 |
"Coefficients will be exponentiated, but the model didn't |
| 94 |
use a {.code log} or {.code logit} link." |
| 95 |
) |
| 96 |
} |
| 97 |
|
| 98 |
ret <- exponentiate(ret) |
| 99 |
} |
| 100 |
|
| 101 |
as_tibble(ret) |
| 102 |
} |
| 103 |
|
| 104 |
#' @templateVar class ergm |
| 105 |
#' @template title_desc_glance |
| 106 |
#' |
| 107 |
#' @inheritParams tidy.ergm |
| 108 |
#' @param deviance Logical indicating whether or not to report null and |
| 109 |
#' residual deviance for the model, as well as degrees of freedom. Defaults |
| 110 |
#' to `FALSE`. |
| 111 |
#' @param mcmc Logical indicating whether or not to report MCMC interval, |
| 112 |
#' burn-in and sample size used to estimate the model. Defaults to `FALSE`. |
| 113 |
#' |
| 114 |
#' @return `glance.ergm` returns a one-row tibble with the columns |
| 115 |
#' \item{independence}{Whether the model assumed dyadic independence} |
| 116 |
#' \item{iterations}{The number of MCMLE iterations performed before convergence} |
| 117 |
#' \item{logLik}{If applicable, the log-likelihood associated with the model} |
| 118 |
#' \item{AIC}{The Akaike Information Criterion} |
| 119 |
#' \item{BIC}{The Bayesian Information Criterion} |
| 120 |
#' |
| 121 |
#' If `deviance = TRUE`, and if the model supports it, the |
| 122 |
#' tibble will also contain the columns |
| 123 |
#' \item{null.deviance}{The null deviance of the model} |
| 124 |
#' \item{df.null}{The degrees of freedom of the null deviance} |
| 125 |
#' \item{residual.deviance}{The residual deviance of the model} |
| 126 |
#' \item{df.residual}{The degrees of freedom of the residual deviance} |
| 127 |
#' |
| 128 |
#' @export |
| 129 |
#' @seealso [glance()], [ergm::ergm()], [ergm::summary.ergm()] |
| 130 |
#' @family ergm tidiers |
| 131 |
glance.ergm <- function(x, deviance = FALSE, mcmc = FALSE, ...) { |
| 132 |
s <- summary(x, ...) # produces lots of messages |
| 133 |
|
| 134 |
ret <- as_glance_tibble( |
| 135 |
independence = s$independence, |
| 136 |
iterations = x$iterations, |
| 137 |
logLik = as.numeric(logLik(x)), |
| 138 |
na_types = "lir" |
| 139 |
) |
| 140 |
|
| 141 |
if (deviance & !is.null(ret$logLik)) { |
| 142 |
# see #567 for details on the following |
| 143 |
|
| 144 |
if (utils::packageVersion("ergm") < "3.10") { |
| 145 |
dyads <- sum( |
| 146 |
ergm::as.rlebdm(x$constrained, x$constrained.obs, which = "informative") |
| 147 |
) |
| 148 |
} else { |
| 149 |
dyads <- stats::nobs(x) |
| 150 |
} |
| 151 |
|
| 152 |
lln <- ergm::logLikNull(x) |
| 153 |
ret$null.deviance <- if (is.na(lln)) 0 else -2 * lln |
| 154 |
ret$df.null <- dyads |
| 155 |
|
| 156 |
ret$residual.deviance <- -2 * ret$logLik |
| 157 |
ret$df.residual <- dyads - length(x$coefs) |
| 158 |
} |
| 159 |
|
| 160 |
ret$AIC <- stats::AIC(x) |
| 161 |
ret$BIC <- stats::BIC(x) |
| 162 |
|
| 163 |
if (mcmc) { |
| 164 |
if (isTRUE(x$MPLE_is_MLE)) { |
| 165 |
cli::cli_inform( |
| 166 |
c( |
| 167 |
"Though {.fn glance} was supplied {.code mcmc = TRUE}, the model was not |
| 168 |
fitted using MCMC,", |
| 169 |
"i" = "The corresponding columns will be omitted." |
| 170 |
) |
| 171 |
) |
| 172 |
} |
| 173 |
|
| 174 |
ret$MCMC.interval <- x$control$MCMC.interval |
| 175 |
ret$MCMC.burnin <- x$control$MCMC.burnin |
| 176 |
ret$MCMC.samplesize <- x$control$MCMC.samplesize |
| 177 |
} |
| 178 |
|
| 179 |
ret |
| 180 |
} |