File: ci_generic.R

package info (click to toggle)
r-cran-parameters 0.24.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,852 kB
  • sloc: sh: 16; makefile: 2
file content (194 lines) | stat: -rw-r--r-- 5,819 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# generic function for CI calculation
.ci_generic <- function(model,
                        ci = 0.95,
                        method = "wald",
                        dof = NULL,
                        effects = "fixed",
                        component = "all",
                        vcov = NULL,
                        vcov_args = NULL,
                        verbose = TRUE,
                        ...) {
  # check method
  if (is.null(method)) {
    method <- "wald"
  }
  method <- tolower(method)
  method <- insight::validate_argument(
    method,
    c(
      "wald", "ml1", "betwithin", "kr", "satterthwaite", "kenward", "boot",
      "profile", "residual", "normal"
    )
  )

  effects <- insight::validate_argument(effects, c("fixed", "random", "all"))
  component <- insight::validate_argument(
    component,
    c(
      "all", "conditional", "zi", "zero_inflated", "dispersion", "precision",
      "scale", "smooth_terms", "full", "marginal"
    )
  )

  if (method == "ml1") { # nolint
    return(ci_ml1(model, ci = ci))
  } else if (method == "betwithin") {
    return(ci_betwithin(model, ci = ci))
  } else if (method == "satterthwaite") {
    return(ci_satterthwaite(model, ci = ci))
  } else if (method %in% c("kenward", "kr")) {
    return(ci_kenward(model, ci = ci))
  }

  # default CIs follow here (methods wald, boot, profile, residual, normal)
  out <- lapply(ci, function(i) {
    .ci_dof(
      model = model,
      ci = i,
      dof = dof,
      effects = effects,
      component = component,
      method = method,
      vcov = vcov,
      vcov_args = vcov_args,
      verbose = verbose,
      ...
    )
  })

  out <- do.call(rbind, out)
  row.names(out) <- NULL
  out
}


#' @keywords internal
.ci_dof <- function(model,
                    ci,
                    dof,
                    effects,
                    component,
                    method = "wald",
                    se = NULL,
                    vcov = NULL,
                    vcov_args = NULL,
                    verbose = TRUE,
                    ...) {
  # need parameters to calculate the CIs
  if (inherits(model, "emmGrid")) {
    params <- insight::get_parameters(
      model,
      effects = effects,
      component = component,
      merge_parameters = TRUE
    )
  } else {
    params <- insight::get_parameters(model,
      effects = effects,
      component = component,
      verbose = FALSE
    )
  }

  # check if all estimates are non-NA
  params <- .check_rank_deficiency(model, params, verbose = FALSE)
  # for polr, we need to fix parameter names
  params$Parameter <- gsub("Intercept: ", "", params$Parameter, fixed = TRUE)

  # validation check...
  if (is.null(method)) {
    method <- "wald"
  }
  method <- tolower(method)

  # Fist, we want standard errors for parameters
  # --------------------------------------------

  # if we have adjusted SE, e.g. from kenward-roger, don't recompute
  # standard errors to save time...
  if (is.null(se)) {
    if (!is.null(vcov) || isTRUE(list(...)[["robust"]])) {
      # robust (HC) standard errors?
      stderror <- standard_error(model,
        component = component,
        vcov = vcov,
        vcov_args = vcov_args,
        verbose = verbose,
        ...
      )
    } else {
      # normal standard errors, including small-sample approximations
      stderror <- switch(method,
        kenward = se_kenward(model),
        kr = se_kenward(model),
        satterthwaite = se_satterthwaite(model),
        standard_error(model, component = component)
      )
    }

    # if we have a non-empty stderror, use it
    if (insight::is_empty_object(stderror)) {
      return(NULL)
    }

    # filter non-matching parameters, resp. sort stderror and parameters,
    # so both have the identical order of values
    if (nrow(stderror) != nrow(params) ||
      !all(stderror$Parameter %in% params$Parameter) ||
      !all(order(stderror$Parameter) == order(params$Parameter))) {
      params <- stderror <- merge(stderror, params, sort = FALSE)
    }

    se <- stderror$SE
  }

  # Next, we need degrees of freedom
  # --------------------------------

  # check if we have a valid dof vector
  if (is.null(dof)) {
    # residual df
    dof <- insight::get_df(x = model, type = method, verbose = FALSE)
    # make sure we have a value for degrees of freedom
    if (is.null(dof) || length(dof) == 0 || .is_chi2_model(model, dof)) {
      dof <- Inf
    } else if (length(dof) > nrow(params)) {
      # filter non-matching parameters
      dof <- dof[seq_len(nrow(params))]
    }
  }

  # Now we can calculate CIs
  # ------------------------

  alpha <- (1 + ci) / 2
  fac <- suppressWarnings(stats::qt(alpha, df = dof))
  out <- cbind(
    CI_low = params$Estimate - se * fac,
    CI_high = params$Estimate + se * fac
  )

  out <- as.data.frame(out)
  out$CI <- ci
  out$Parameter <- params$Parameter

  out <- out[c("Parameter", "CI", "CI_low", "CI_high")]
  if ("Component" %in% names(params)) out$Component <- params$Component
  if ("Effects" %in% names(params) && effects != "fixed") out$Effects <- params$Effects
  if ("Response" %in% names(params)) out$Response <- params$Response

  # for cox-panel models, we have non-linear parameters with NA coefficient,
  # but test statistic and p-value - don't check for NA estimates in this case
  if (anyNA(params$Estimate) && !inherits(model, "coxph.penal")) {
    out[stats::complete.cases(out), ]
  } else {
    out
  }
}


.is_chi2_model <- function(model, dof) {
  statistic <- insight::find_statistic(model)
  (all(dof == 1) && identical(statistic, "chi-squared statistic"))
}