|
2 | 2 |
|
3 | 3 | #' Predictions from a submodel (after projection) |
4 | 4 | #' |
5 | | -#' After the projection of the reference model onto a submodel, the linear |
6 | | -#' predictors (for the original or a new dataset) based on that submodel can be |
7 | | -#' calculated by [proj_linpred()]. These linear predictors can also be |
8 | | -#' transformed to response scale and averaged across the projected parameter |
9 | | -#' draws. Furthermore, [proj_linpred()] returns the corresponding log predictive |
10 | | -#' density values if the (original or new) dataset contains response values. The |
11 | | -#' [proj_predict()] function draws from the predictive distributions (there is |
12 | | -#' one such distribution for each observation from the original or new dataset) |
13 | | -#' of the submodel that the reference model has been projected onto. If the |
14 | | -#' projection has not been performed yet, both functions call [project()] |
15 | | -#' internally to perform the projection. Both functions can also handle multiple |
16 | | -#' submodels at once (for `object`s of class `vsel` or `object`s returned by a |
17 | | -#' [project()] call to an object of class `vsel`; see [project()]). |
| 5 | +#' The [proj_predict()] function draws from the projected posterior |
| 6 | +#' predictive distribution of the submodel that the reference model |
| 7 | +#' has been projected onto. By definition, these draws have higher |
| 8 | +#' variability than draws of the expected value of the posterior |
| 9 | +#' predictive distribution computed by [proj_epred()]. This is because |
| 10 | +#' the aleatoric uncertainty from the data model is incorporated in |
| 11 | +#' [proj_predict()]. |
| 12 | +#' |
| 13 | +#' The [proj_epred()] function draws from the distribution of the |
| 14 | +#' expected value of the projected posterior predictive distribution. |
| 15 | +#' By definition, these predictions have smaller variability than the |
| 16 | +#' projected posterior predictions performed by [proj_predict()]. This |
| 17 | +#' is because only the epistemic uncertainty in the expected value of |
| 18 | +#' the projected posterior predictive distribution is incorporated in |
| 19 | +#' the draws, while the aleatoric uncertainty from the data model is |
| 20 | +#' not included. However, the estimated means of both methods averaged |
| 21 | +#' across draws should be very similar. |
| 22 | +#' |
| 23 | +#' The [proj_linpred()] function draws from the projected posterior of the |
| 24 | +#' linear predictors, that is, draws before applying any link functions |
| 25 | +#' or other transformations. These linear predictors can also be |
| 26 | +#' transformed to response scale with argument `transform = TRUE`, which |
| 27 | +#' produces draws equivalent to draws produced by [proj_epred()]. |
| 28 | +#' Furthermore, [proj_linpred()] returns the corresponding log predictive |
| 29 | +#' density values if the (original or new) dataset contains response values. |
| 30 | +#' |
| 31 | +#' All these predictions can be performed for the data used to fit the |
| 32 | +#' reference model or for new data. If the projection has not been |
| 33 | +#' performed yet, all three functions call [project()] internally to |
| 34 | +#' perform the projection. All three functions can also handle |
| 35 | +#' multiple submodels at once (for `object`s of class `vsel` or |
| 36 | +#' `object`s returned by a [project()] call to an object of class |
| 37 | +#' `vsel`; see [project()]). |
18 | 38 | #' |
19 | 39 | #' @name pred-projection |
20 | 40 | #' |
|
26 | 46 | #' for only those elements (submodels) with a number of predictor terms in |
27 | 47 | #' `filter_nterms`. Therefore, needs to be a numeric vector or `NULL`. If |
28 | 48 | #' `NULL`, use all submodels. |
29 | | -#' @param transform For [proj_linpred()] only. A single logical value indicating |
| 49 | +#' @param transform For [proj_linpred()] only (not applicable for [proj_epred()] |
| 50 | +#' which always uses `transform = TRUE` internally). A single logical value indicating |
30 | 51 | #' whether the linear predictor should be transformed to response scale using |
31 | 52 | #' the inverse-link function (`TRUE`) or not (`FALSE`). In case of the latent |
32 | 53 | #' projection, argument `transform` is similar in spirit to argument |
33 | 54 | #' `resp_oscale` from other functions and affects the scale of both output |
34 | 55 | #' elements `pred` and `lpd` (see sections "Details" and "Value" below). |
35 | | -#' @param integrated For [proj_linpred()] only. A single logical value |
| 56 | +#' @param integrated For [proj_linpred()] and [proj_epred()] only. A single logical value |
36 | 57 | #' indicating whether the output should be averaged across the projected |
37 | 58 | #' posterior draws (`TRUE`) or not (`FALSE`). |
38 | 59 | #' @param nresample_clusters For [proj_predict()] with clustered projection (and |
|
42 | 63 | #' gives the number of draws (*with* replacement) from the set of clustered |
43 | 64 | #' posterior draws after projection (with this set being determined by |
44 | 65 | #' argument `nclusters` of [project()]). |
45 | | -#' @param allow_nonconst_wdraws_prj Only relevant for [proj_linpred()] and only |
46 | | -#' if `integrated` is `FALSE`. A single logical value indicating whether to |
| 66 | +#' @param allow_nonconst_wdraws_prj Only relevant for [proj_linpred()] and |
| 67 | +#' [proj_epred()] and only if `integrated` is `FALSE`. A single logical value indicating whether to |
47 | 68 | #' allow projected draws with different (i.e., nonconstant) weights (`TRUE`) |
48 | 69 | #' or not (`FALSE`). If `return_draws_matrix` is `TRUE`, |
49 | 70 | #' `allow_nonconst_wdraws_prj` is internally set to `TRUE` as well. |
|
53 | 74 | #' matrices). |
54 | 75 | #' @param return_draws_matrix A single logical value indicating whether to |
55 | 76 | #' return an object (in case of [proj_predict()]) or objects (in case of |
56 | | -#' [proj_linpred()]) of class `draws_matrix` (see |
57 | | -#' [posterior::draws_matrix()]). In case of [proj_linpred()] and projected |
| 77 | +#' [proj_linpred()] and [proj_epred()]) of class `draws_matrix` (see |
| 78 | +#' [posterior::draws_matrix()]). In case of [proj_linpred()] or |
| 79 | +#' [proj_epred()] and projected |
58 | 80 | #' draws with nonconstant weights (as well as `integrated` being `FALSE`), |
59 | 81 | #' [posterior::weight_draws()] is applied internally. |
60 | 82 | #' @param .seed Pseudorandom number generation (PRNG) seed by which the same |
61 | 83 | #' results can be obtained again if needed. Passed to argument `seed` of |
62 | 84 | #' [set.seed()], but can also be `NA` to not call [set.seed()] at all. If not |
63 | 85 | #' `NA`, then the PRNG state is reset (to the state before calling |
64 | | -#' [proj_linpred()] or [proj_predict()]) upon exiting [proj_linpred()] or |
65 | | -#' [proj_predict()]. Here, `.seed` is used for drawing new group-level effects |
| 86 | +#' [proj_linpred()], [proj_epred()], or [proj_predict()]) upon exiting |
| 87 | +#' [proj_linpred()], [proj_epred()], or [proj_predict()]. Here, `.seed` is |
| 88 | +#' used for drawing new group-level effects |
66 | 89 | #' in case of a multilevel submodel (however, not yet in case of a GAMM) and |
67 | 90 | #' for drawing from the predictive distributions of the submodel(s) in case of |
68 | 91 | #' [proj_predict()]. If a clustered projection was performed, then in |
|
140 | 163 | #' `return_draws_matrix`, `allow_nonconst_wdraws_prj`, and `integrated` |
141 | 164 | #' are all `FALSE`, then projected draws with nonconstant weights cause an |
142 | 165 | #' error.) |
| 166 | +#' * [proj_epred()] is a wrapper around [proj_linpred()] with `transform = |
| 167 | +#' TRUE` and returns only the draws of the expected value of the projected |
| 168 | +#' posterior predictive distribution on the response scale (i.e., the `pred` |
| 169 | +#' element of the `list` returned by [proj_linpred()], without the `lpd` |
| 170 | +#' element). The structure of the returned object is the same as that of the |
| 171 | +#' `pred` element described for [proj_linpred()] above. |
143 | 172 | #' * [proj_predict()] returns an \eqn{S_{\mathrm{prj}} \times N}{S_prj x N} |
144 | 173 | #' matrix of predictions where \eqn{S_{\mathrm{prj}}}{S_prj} denotes |
145 | 174 | #' `nresample_clusters` in case of clustered projection (or, more generally, |
|
179 | 208 | #' # Predictions (at the training points) from the submodel onto which the |
180 | 209 | #' # reference model was projected: |
181 | 210 | #' prjl <- proj_linpred(prj) |
| 211 | +#' prje <- proj_epred(prj) |
182 | 212 | #' prjp <- proj_predict(prj, .seed = 7364) |
183 | 213 | #' |
184 | 214 | NULL |
185 | 215 |
|
186 | 216 | # Function definitions ---------------------------------------------------- |
187 | 217 |
|
188 | | -## The 'helper' for proj_linpred and proj_predict, ie. does all the |
189 | | -## functionality that is common to them. It essentially checks all the arguments |
| 218 | +## The 'helper' for proj_linpred, proj_epred, and proj_predict, ie. does all |
| 219 | +## the functionality that is common to them. It essentially checks all the arguments |
190 | 220 | ## and sets them to their respective defaults and then loops over the |
191 | 221 | ## projections. For each projection, it evaluates the fun-function, which |
192 | 222 | ## calculates the linear predictor if called from proj_linpred and samples from |
@@ -506,6 +536,29 @@ proj_predict <- function(object, newdata = NULL, offsetnew = NULL, |
506 | 536 | ) |
507 | 537 | } |
508 | 538 |
|
| 539 | +#' @rdname pred-projection |
| 540 | +#' @export |
| 541 | +proj_epred <- function(object, newdata = NULL, offsetnew = NULL, |
| 542 | + weightsnew = NULL, filter_nterms = NULL, |
| 543 | + integrated = FALSE, |
| 544 | + allow_nonconst_wdraws_prj = return_draws_matrix, |
| 545 | + return_draws_matrix = FALSE, .seed = NA, ...) { |
| 546 | + out <- proj_linpred( |
| 547 | + object = object, newdata = newdata, |
| 548 | + offsetnew = offsetnew, weightsnew = weightsnew, |
| 549 | + filter_nterms = filter_nterms, transform = TRUE, |
| 550 | + integrated = integrated, |
| 551 | + allow_nonconst_wdraws_prj = allow_nonconst_wdraws_prj, |
| 552 | + return_draws_matrix = return_draws_matrix, |
| 553 | + .seed = .seed, ... |
| 554 | + ) |
| 555 | + if (is.list(out) && "pred" %in% names(out)) { |
| 556 | + return(out$pred) |
| 557 | + } |
| 558 | + ## Multiple submodels: each element is a list with `pred` and `lpd`. |
| 559 | + return(lapply(out, "[[", "pred")) |
| 560 | +} |
| 561 | + |
509 | 562 | ## function applied to each projected submodel in case of proj_predict() |
510 | 563 | proj_predict_aux <- function(proj, newdata, offsetnew, weightsnew, |
511 | 564 | nresample_clusters = 1000, resp_oscale = TRUE, |
|
0 commit comments