Debian Codegraph @

R/geepack.R

raw
1 #' @templateVar class geeglm
2 #' @template title_desc_tidy
3 #'
4 #' @param x A `geeglm` object returned from a call to [geepack::geeglm()].
5 #' @template param_confint
6 #' @template param_exponentiate
7 #' @template param_unused_dots
8 #'
9 #' @details If `conf.int = TRUE`, the confidence interval is computed with
10 #' the an internal `confint.geeglm()` function.
11 #'
12 #' If you have missing values in your model data, you may need to
13 #' refit the model with `na.action = na.exclude` or deal with the
14 #' missingness in the data beforehand.
15 #'
16 #' @examplesIf rlang::is_installed("geepack")
17 #'
18 #' # load modeling library
19 #' library(geepack)
20 #'
21 #' # load data
22 #' data(state)
23 #'
24 #'
25 #' ds <- data.frame(state.region, state.x77)
26 #'
27 #' # fit model
28 #' geefit <- geeglm(Income ~ Frost + Murder,
29 #' id = state.region,
30 #' data = ds,
31 #' corstr = "exchangeable"
32 #' )
33 #'
34 #' # summarize model fit with tidiers
35 #' tidy(geefit)
36 #' tidy(geefit, conf.int = TRUE)
37 #'
38 #' @evalRd return_tidy(regression = TRUE)
39 #'
40 #' @export
41 #' @aliases geeglm_tidiers geepack_tidiers
42 #' @seealso [tidy()], [geepack::geeglm()]
43 #'
44 tidy.geeglm <- function(
45 x,
46 conf.int = FALSE,
47 conf.level = .95,
48 exponentiate = FALSE,
49 ...
50 ) {
51 co <- stats::coef(summary(x))
52
53 ret <- as_tidy_tibble(
54 co,
55 c("estimate", "std.error", "statistic", "p.value")[1:ncol(co)]
56 )
57
58 if (conf.int) {
59 ci <- broom_confint_terms(x, level = conf.level)
60 ret <- dplyr::left_join(ret, ci, by = "term")
61 }
62
63 if (exponentiate) {
64 if (
65 is.null(x$family) ||
66 (x$family$link != "logit" && x$family$link != "log")
67 ) {
68 cli::cli_warn(
69 "Coefficients will be exponentiated, but the model didn't
70 use a {.code log} or {.code logit} link."
71 )
72 }
73
74 ret <- exponentiate(ret)
75 }
76
77 ret
78 }
79
80 #' Generate confidence intervals for geeglm objects
81 #'
82 #' @param object A `geeglm` object.
83 #' @param parm The parameter to calculate the confidence interval
84 #' for. If not specified, the default is to calculate a confidence
85 #' interval on all parameters (all variables in the model).
86 #' @param level Confidence level of the interval.
87 #' @param ... Additional parameters (ignored).
88 #'
89 #' @details From https://stackoverflow.com/a/21221995/2632184.
90 #'
91 #' @return Lower and upper confidence bounds in a data.frame(?).
92 #'
93 #' @noRd
94 confint.geeglm <- function(object, parm, level = 0.95, ...) {
95 cc <- stats::coef(summary(object))
96 mult <- stats::qnorm((1 + level) / 2)
97 citab <- with(
98 as.data.frame(cc),
99 cbind(
100 lwr = Estimate - mult * Std.err,
101 upr = Estimate + mult * Std.err
102 )
103 )
104 rownames(citab) <- rownames(cc)
105 citab[parm, ]
106 }
107
108 #' @templateVar class geeglm
109 #' @template title_desc_glance
110 #'
111 #' @inherit tidy.geeglm params examples
112 #'
113 #' @evalRd return_glance("df.residual", "n.clusters", "max.cluster.size", "alpha", "gamma")
114 #'
115 #' @export
116 #' @seealso [glance()], [geepack::geeglm()]
117 #' @family geepack tidiers
118 glance.geeglm <- function(x, ...) {
119 s <- summary(x)
120 as_glance_tibble(
121 df.residual = x$df.residual,
122 n.clusters = length(s$clusz),
123 max.cluster.size = max(s$clusz),
124 alpha = x$geese$alpha,
125 gamma = x$geese$gamma,
126 na_types = "iiirr"
127 )
128 }