-
Notifications
You must be signed in to change notification settings - Fork 7
/
postestimate_infer.R
255 lines (235 loc) · 9.83 KB
/
postestimate_infer.R
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#' Inference
#'
#' \lifecycle{stable}
#'
#' Calculate common inferential quantities. For users interested in the
#' estimated standard errors, t-values, p-values and/or confidences
#' intervals of the path, weight or loading estimates, calling [summarize()]
#' directly will usually be more convenient as it has a much more
#' user-friendly print method. [infer()] is useful for comparing
#' different confidence interval estimates.
#'
#' [infer()] is a convenience wrapper around a
#' number of internal functions that compute a particular inferential
#' quantity, i.e., a value or set of values to be used in statistical inference.
#'
#' \pkg{cSEM} relies on resampling (bootstrap and jackknife) as the basis for
#' the computation of e.g., standard errors or confidence intervals.
#' Consequently, [infer()] requires resamples to work. Technically,
#' the [cSEMResults] object used in the call to [infer()] must
#' therefore also have class attribute `cSEMResults_resampled`. If
#' the object provided by the user does not contain resamples yet,
#' [infer()] will obtain bootstrap resamples first.
#' Naturally, computation will take longer in this case.
#'
#' [infer()] does as much as possible in the background. Hence, every time
#' [infer()] is called on a [cSEMResults] object the quantities chosen by
#' the user are automatically computed for every estimated parameter
#' contained in the object. By default all possible quantities are
#' computed (`.quantity = all`). The following table list the available
#' inferential quantities alongside a brief description. Implementation and
#' terminology of the confidence intervals is based on
#' \insertCite{Hesterberg2015;textual}{cSEM} and
#' \insertCite{Davison1997;textual}{cSEM}.
#' \describe{
#' \item{`"mean"`, `"sd"`}{The mean or the standard deviation
#' over all `M` resample estimates of a generic statistic or parameter.}
#' \item{`"bias"`}{The difference between the resample mean and the original
#' estimate of a generic statistic or parameter.}
#' \item{`"CI_standard_z"` and `"CI_standard_t"`}{The standard confidence interval
#' for a generic statistic or parameter with standard errors estimated by
#' the resample standard deviation. While `"CI_standard_z"` assumes a
#' standard normally distributed statistic,
#' `"CI_standard_t"` assumes a t-statistic with N - 1 degrees of freedom.}
#' \item{`"CI_percentile"`}{The percentile confidence interval. The lower and
#' upper bounds of the confidence interval are estimated as the alpha and
#' 1-alpha quantiles of the distribution of the resample estimates.}
#' \item{`"CI_basic"`}{The basic confidence interval also called the reverse
#' bootstrap percentile confidence interval. See \insertCite{Hesterberg2015;textual}{cSEM}
#' for details.}
#' \item{`"CI_bc"`}{The bias corrected (Bc) confidence interval. See
#' \insertCite{Davison1997;textual}{cSEM} for details.}
#' \item{`"CI_bca"`}{The bias-corrected and accelerated (Bca) confidence interval.
#' Requires additional jackknife resampling to compute the influence values.
#' See \insertCite{Davison1997;textual}{cSEM} for details.}
#' \item{`"CI_t_interval"`}{The "studentized" t-confidence interval. If based on bootstrap
#' resamples the interval is also called the bootstrap t-interval
#' confidence interval. See \insertCite{Hesterberg2015;textual}{cSEM} on page 381.
#' Requires resamples of resamples. See [resamplecSEMResults()].}
#' }
#'
#' By default, all but the studendized t-interval confidence interval and the
#' bias-corrected and accelerated confidence interval are calculated. The
#' reason for excluding these quantities by default are that both require
#' an additional resampling step. The former requires
#' jackknife estimates to compute influence values and the latter requires
#' double bootstrap. Both can potentially be time consuming.
#' Hence, computation is triggered only if explicitly chosen.
#'
#' @usage infer(
#' .object = NULL,
#' .quantity = c("all", "mean", "sd", "bias", "CI_standard_z",
#' "CI_standard_t", "CI_percentile", "CI_basic",
#' "CI_bc", "CI_bca", "CI_t_interval"),
#' .alpha = 0.05,
#' .bias_corrected = TRUE
#' )
#'
#' @inheritParams csem_arguments
#'
#' @return A list of class `cSEMInfer`.
#'
#' @references
#' \insertAllCited{}
#'
#' @seealso [csem()], [resamplecSEMResults()], [summarize()] [cSEMResults]
#'
#' @example inst/examples/example_infer.R
#' @export
infer <- function(
.object = NULL,
.quantity = c("all", "mean", "sd", "bias", "CI_standard_z",
"CI_standard_t", "CI_percentile", "CI_basic",
"CI_bc", "CI_bca", "CI_t_interval"),
.alpha = 0.05,
.bias_corrected = TRUE
) {
## Check arguments
match.arg(.quantity, args_default(.choices = TRUE)$.quantity, several.ok = TRUE)
## Check if "all" is part of .quantity. If yes, set .quantity = "all"
if(any(.quantity == "all")) {
.quantity <- "all"
}
if(!inherits(.object, "cSEMResults_resampled")) {
# Bootstrap if necessary
.object <- resamplecSEMResults(.object)
}
if(inherits(.object, "cSEMResults_multi")) {
## If multi object, do recursive call
out <- lapply(.object, function(x) {
infer(
.object = x,
.alpha = .alpha,
.bias_corrected = .bias_corrected,
.quantity = .quantity
)
})
## Add/ set class
class(out) <- c("cSEMInfer", "cSEMInfer_multi")
return(out)
} else if(inherits(.object, "cSEMResults_2ndorder")) {
first_resample <- .object$Second_stage$Information$Resamples$Estimates$Estimates1
second_resample <- .object$Second_stage$Information$Resamples$Estimates$Estimates2
info <- .object$Second_stage$Information$Resamples$Information_resample
} else if(inherits(.object, "cSEMResults_default")) {
first_resample <- .object$Estimates$Estimates_resample$Estimates1
second_resample <- .object$Estimates$Estimates_resample$Estimates2
info <- .object$Information$Information_resample
} else {
stop2("The following error occured in the `infer()` function:\n",
"Object must be of class `cSEMResults`")
}
## Compute quantiles/critical values -----------------------------------------
probs <- c()
.alpha <- .alpha[order(.alpha)]
for(i in seq_along(.alpha)) {
if(.alpha[i] < 0 | .alpha[i] > 1) {
stop2("The following error occured in the `infer()` function:\n",
"`.alpha` must be between 0 and 1.")
}
# Both two sided and one sided confidence intervals may be needed.
# Therefore for every alpha four values will be put in a vector
# 1. alpha
# 2. 1 - alpha
# 3. alpha/2
# 4. 1 - alpha/2
# to make sure the corresponding quantile is available later on. Values are
# round to four digits.
probs <- c(probs,
# round(.alpha[i], 4), round(1 - .alpha[i], 4),
.alpha[i]/2, 1 - .alpha[i]/2)
}
## Compute statistics and quantities
out <- list()
if(any(.quantity %in% c("all", "mean"))) {
out[["mean"]] <- MeanResample(first_resample)
}
if(any(.quantity %in% c("all", "sd"))) {
out[["sd"]] <- SdResample(
.first_resample = first_resample,
.resample_method = info$Method,
.n = info$Number_of_observations
)
}
if(any(.quantity %in% c("all", "bias"))) {
out[["bias"]] <- BiasResample(
.first_resample = first_resample,
.resample_method = info$Method,
.n = info$Number_of_observations
)
}
if(any(.quantity %in% c("all", "CI_standard_z"))) {
out[["CI_standard_z"]] <- StandardCIResample(
.first_resample = first_resample,
.bias_corrected = .bias_corrected,
.df = NULL,
.dist = "z",
.resample_method= info$Method,
.n = info$Number_of_observations,
.probs = probs
)
}
if(any(.quantity %in% c("all", "CI_standard_t"))) {
out[["CI_standard_t"]] <- StandardCIResample(
.first_resample = first_resample,
.bias_corrected = .bias_corrected,
.df = "type1",
.dist = "t",
.resample_method= info$Method,
.n = info$Number_of_observations,
.probs = probs
)
}
if(any(.quantity %in% c("all", "CI_percentile"))) {
out[["CI_percentile"]] <- PercentilCIResample(
.first_resample = first_resample,
.probs = probs
)
}
if(any(.quantity %in% c("all", "CI_basic"))) {
out[["CI_basic"]] <- BasicCIResample(
.first_resample = first_resample,
.bias_corrected = .bias_corrected,
.probs = probs
)
}
if(any(.quantity %in% c("all", "CI_bc"))) {
out[["CI_bc"]] <- BcCIResample(first_resample, probs)
}
if(any(.quantity == "CI_bca")) {
out[["CI_bca"]] <- BcaCIResample(.object = .object,
first_resample, probs)
}
if(any(.quantity %in% c("all", "CI_t_interval"))) {
if(!anyNA(second_resample)) {
out[["CI_t_interval"]] <- TStatCIResample(
.first_resample = first_resample,
.second_resample = second_resample,
.bias_corrected = .bias_corrected,
.resample_method = info$Method,
.resample_method2 = info$Method2,
.n = info$Number_of_observations,
.probs = probs
)
} else if(any(.quantity == "CI_t_interval")) {
stop2("The following error occured in the `infer()` function:\n",
"`CI_t_interval` requires (jackknife) resamples for each resample.",
" Rerun your original estimation using .resample_method2 = 'jackknife'.",
" and try again.")
}
}
out <- purrr::transpose(out)
## Add/ set class
class(out) <- c("cSEMInfer")
out
}