This function adds some internally required elements to an object of class
family (see, e.g., family()). It is called internally by
init_refmodel(), so you will rarely need to call it yourself.
Arguments
- family
An object of class
family.- latent
A single logical value indicating whether to use the latent projection (
TRUE) or not (FALSE). Note that settinglatent = TRUEcauses all arguments starting withaugdat_to be ignored.- latent_y_unqs
Only relevant for a latent projection where the original response space has finite support (i.e., the original response values may be regarded as categories), in which case this needs to be the character vector of unique response values (which will be assigned to
family$catsinternally) or may be left atNULL(so that projpred will try to infer it fromfamily$cats). See also section "Latent projection" below.- latent_ilink
Only relevant for the latent projection, in which case this needs to be the inverse-link function. If the original response family was the
binomial()or thepoisson()family, thenlatent_ilinkcan beNULL, in which case an internal default will be used. Can also beNULLin all other cases, but then an internal default based onfamily$linkinvwill be used which might not work for all families. See also section "Latent projection" below.- latent_ll_oscale
Only relevant for the latent projection, in which case this needs to be the function computing response-scale (not latent-scale) log-likelihood values. If
!is.null(family$cats)(after takinglatent_y_unqsinto account) or if the original response family was thebinomial()or thepoisson()family, thenlatent_ll_oscalecan beNULL, in which case an internal default will be used. Can also beNULLin all other cases, but then downstream functions will have limited functionality (a message thrown byextend_family()will state what exactly won't be available). See also section "Latent projection" below.- latent_ppd_oscale
Only relevant for the latent projection, in which case this needs to be the function sampling response values given latent predictors that have been transformed to response scale using
latent_ilink. If!is.null(family$cats)(after takinglatent_y_unqsinto account) or if the original response family was thebinomial()or thepoisson()family, thenlatent_ppd_oscalecan beNULL, in which case an internal default will be used. Can also beNULLin all other cases, but then downstream functions will have limited functionality (a message thrown byextend_family()will state what exactly won't be available). See also section "Latent projection" below. Note that although this function has the abbreviation "PPD" in its name (which stands for "posterior predictive distribution"), projpred currently only uses it inproj_predict(), i.e., for sampling from what would better be termed posterior-projection predictive distribution (PPPD).- augdat_y_unqs
Only relevant for augmented-data projection, in which case this needs to be the character vector of unique response values (which will be assigned to
family$catsinternally) or may be left atNULLiffamily$catsis already non-NULL. See also section "Augmented-data projection" below.- augdat_link
Only relevant for augmented-data projection, in which case this needs to be the link function. Use
NULLfor the traditional projection. See also section "Augmented-data projection" below.- augdat_ilink
Only relevant for augmented-data projection, in which case this needs to be the inverse-link function. Use
NULLfor the traditional projection. See also section "Augmented-data projection" below.- augdat_args_link
Only relevant for augmented-data projection, in which case this may be a named
listof arguments to pass to the function supplied toaugdat_link.- augdat_args_ilink
Only relevant for augmented-data projection, in which case this may be a named
listof arguments to pass to the function supplied toaugdat_ilink.- ...
Ignored (exists only to swallow up further arguments which might be passed to this function).
Details
In the following, \(N\), \(C_{\mathrm{cat}}\), \(C_{\mathrm{lat}}\), \(S_{\mathrm{ref}}\), and \(S_{\mathrm{prj}}\) from help topic refmodel-init-get are used. Note that \(N\) does not necessarily denote the number of original observations; it can also refer to new observations. Furthermore, let \(S\) denote either \(S_{\mathrm{ref}}\) or \(S_{\mathrm{prj}}\), whichever is appropriate in the context where it is used.
Augmented-data projection
As their first input, the functions supplied to arguments augdat_link and
augdat_ilink have to accept:
For
augdat_link: an \(S \times N \times C_{\mathrm{cat}}\) array containing the probabilities for the response categories. The order of the response categories is the same as infamily$cats(see argumentaugdat_y_unqs).For
augdat_ilink: an \(S \times N \times C_{\mathrm{lat}}\) array containing the linear predictors.
The return value of these functions needs to be:
For
augdat_link: an \(S \times N \times C_{\mathrm{lat}}\) array containing the linear predictors.For
augdat_ilink: an \(S \times N \times C_{\mathrm{cat}}\) array containing the probabilities for the response categories. The order of the response categories has to be the same as infamily$cats(see argumentaugdat_y_unqs).
For the augmented-data projection, the response vector resulting from
extract_model_data (see init_refmodel()) is coerced to a factor (using
as.factor()) at multiple places throughout this package. Inside of
init_refmodel(), the levels of this factor have to be identical to
family$cats (after applying extend_family() inside of
init_refmodel()). Everywhere else, these levels have to be a subset of
<refmodel>$family$cats (where <refmodel> is an object resulting from
init_refmodel()). See argument augdat_y_unqs for how to control
family$cats.
For ordinal brms families, be aware that the submodels (onto which the reference model is projected) currently have the following restrictions:
The discrimination parameter
discis not supported (i.e., it is a constant with value 1).The thresholds are
"flexible"(seebrms::brmsfamily()).The thresholds do not vary across the levels of a
factor-like variable (see argumentgrofbrms::resp_thres()).The
"probit_approx"link is replaced by"probit".
For the brms::categorical() family, be aware that:
For multilevel submodels, the group-level effects are allowed to be correlated between different response categories.
For multilevel submodels, mclogit versions < 0.9.4 may throw the error
'a' (<number> x 1) must be square. Updating mclogit to a version >= 0.9.4 should fix this.
Latent projection
The function supplied to argument latent_ilink needs to have the prototype
where:
lpredsaccepts an \(S \times N\) matrix containing the linear predictors.cl_refaccepts a numeric vector of length \(S_{\mathrm{ref}}\), containing projpred's internal cluster indices for these draws.wdraws_refaccepts a numeric vector of length \(S_{\mathrm{ref}}\), containing weights for these draws. These weights should be treated as not being normalized (i.e., they don't necessarily sum to1).
The return value of latent_ilink needs to contain the linear predictors
transformed to the original response space, with the following structure:
If
is.null(family$cats)(after takinglatent_y_unqsinto account): an \(S \times N\) matrix.If
!is.null(family$cats)(after takinglatent_y_unqsinto account): an \(S \times N \times C_{\mathrm{cat}}\) array. In that case,latent_ilinkneeds to return probabilities (for the response categories given infamily$cats, after takinglatent_y_unqsinto account).
The function supplied to argument latent_ll_oscale needs to have the
prototype
latent_ll_oscale(ilpreds, dis, y_oscale, wobs = rep(1, length(y_oscale)),
cl_ref, wdraws_ref = rep(1, length(cl_ref)))where:
ilpredsaccepts the return value fromlatent_ilink.disaccepts a vector of length \(S\) containing dispersion parameter draws.y_oscaleaccepts a vector of length \(N\) containing response values on the original response scale.wobsaccepts a numeric vector of length \(N\) containing observation weights.cl_refaccepts the same input as argumentcl_refoflatent_ilink.wdraws_refaccepts the same input as argumentwdraws_refoflatent_ilink.
The return value of latent_ll_oscale needs to be an \(S \times N\)
matrix containing the response-scale (not latent-scale) log-likelihood values
for the \(N\) observations from its inputs.
The function supplied to argument latent_ppd_oscale needs to have the
prototype
latent_ppd_oscale(ilpreds_resamp, dis_resamp, wobs, cl_ref,
wdraws_ref = rep(1, length(cl_ref)), idxs_prjdraws)where:
ilpreds_resampaccepts the return value fromlatent_ilink, but possibly with resampled (clustered) draws (see argumentnresample_clustersofproj_predict()).dis_resampaccepts a vector of lengthdim(ilpreds_resamp)[1]containing dispersion parameter draws, possibly resampled (in the same way as the draws inilpreds_resamp, see also argumentidxs_prjdraws).wobsaccepts a numeric vector of length \(N\) containing observation weights.cl_refaccepts the same input as argumentcl_refoflatent_ilink.wdraws_refaccepts the same input as argumentwdraws_refoflatent_ilink.idxs_prjdrawsaccepts a numeric vector of lengthdim(ilpreds_resamp)[1]containing the resampled indices of the projected draws (i.e., these indices are values from the set \(\{1, ..., \texttt{dim(ilpreds)[1]}\}\) whereilpredsdenotes the return value oflatent_ilink).
The return value of latent_ppd_oscale needs to be a
\(\texttt{dim(ilpreds\_resamp)[1]} \times N\)
matrix containing the response-scale (not latent-scale) draws from the
posterior(-projection) predictive distributions for the \(N\) observations
from its inputs.
If the bodies of these three functions involve parameter draws from the
reference model which have not been projected (e.g., for latent_ilink, the
thresholds in an ordinal model), cl_agg() is provided as a helper function
for aggregating these reference model draws in the same way as the draws have
been aggregated for the first argument of these functions (e.g., lpreds in
case of latent_ilink).
In fact, the weights passed to argument wdraws_ref are nonconstant only in
case of cv_varsel() with cv_method = "LOO" and validate_search = TRUE.
In that case, the weights passed to this argument are the PSIS-LOO CV weights
for one observation. Note that although argument wdraws_ref has the suffix
_ref, wdraws_ref does not necessarily obtain weights for the initial
reference model's posterior draws: In case of cv_varsel() with cv_method = "kfold", these weights may refer to one of the \(K\) reference model
refits (but in that case, they are constant anyway).
If family$cats is not NULL (after taking latent_y_unqs into account),
then the response vector resulting from extract_model_data (see
init_refmodel()) is coerced to a factor (using as.factor()) at multiple
places throughout this package. Inside of init_refmodel(), the levels of
this factor have to be identical to family$cats (after applying
extend_family() inside of init_refmodel()). Everywhere else, these levels
have to be a subset of <refmodel>$family$cats (where <refmodel> is an
object resulting from init_refmodel()).