Kmatrix, predictmeans, contrastmeans and covariatemeans get
everything they need from a model through these seven generics. Their default methods work for any model class with
the usual methods (coef, vcov, terms, df.residual, family), so many classes need
no extra code. For a class that lacks one of these, or needs something special, write a method for the
corresponding generic only; nothing inside predictmeans needs to change.
Usage
pm_coef(model, ...)
pm_vcov(model, ...)
pm_basis(model, newdata, ...)
pm_df(model, L, ...)
pm_linkinv(model, ...)
pm_contrasts(model, ...)Details
pm_coef(model)Named fixed-effect estimates, with
NAfor aliased coefficients. Default:coef(model); methods forlme,merModandglmmTMBreturn the fixed effects.pm_vcov(model)Covariance matrix of the non-aliased estimates. Default:
vcov(model).pm_basis(model, newdata, ...)Design matrix of the fixed effects for
newdata, with columns named as inpm_coef(model): the columns are matched to the coefficients by name, and a coefficient without a column stops with a message (a column for an aliased coefficient may be there or not). Default:model.matrix()of the model's terms (with their storedpredvars, sopoly(),ns(),scale()etc. use the fitted basis), contrasts and factor levels. Thegammethod uses the linear predictor matrix of package mgcv.pm_data(model)The data used to fit the model. The default method (see
pm_data) finds them from thedataargument of the call,getData()or the model frame;Kmatrix()uses apm_data()method only when the class has its own.pm_df(model, L)Denominator degrees of freedom for each row of
L, orNULL(the default) to use the model's single residual df:df.residual(model), orInf(normal-based) when there is none. Kenward-Roger df forlmerand the rules of nlme forlmeare built in.pm_linkinv(model)Inverse link function used to back-transform the means, or
NULLfor none. Default: the inverse link offamily(model)unless it is the identity.pm_contrasts(model)The contrasts of the factors used in the fit, as for the
contrasts.argargument ofmodel.matrix(). Default: those ofmodel.matrix(model), elsemodel$contrasts; a method is only needed for a class that has neither.
The model also needs a terms() method for its fixed effects (used to find the variables in the model), and
its call (getCall()) or a pm_data() method so that the data can be found.
What a new class gets
- Means, pairs and contrasts
predictmeans(),contrastmeans(),pm_joint(),pm_slopes(),Kmatrix()needcoef()(orpm_coef()),vcov()(orpm_vcov()) andterms(), plus the data (found from the call, or apm_data()method). The columns ofpm_basis()are matched to the coefficients by name.- Residual plots
residplot()andpm_check()also needfitted()andresiduals()methods (residuals divided by their SD); without them they stop and say so.- The rest
anova(),CookD(),R2_model(),varcomp(),se_ranef(),permmodels()andbootmodels()have their own code for the classes they list, and stop with that list for any other class.
Examples
library(predictmeans)
# A made-up model class that wraps lm() and has only four methods. (Methods are
# found where predictmeans looks for them: in a package's namespace or, as
# here, in the global environment; registerS3method() also works.)
fit_mymodel <- function(formula, data) {
structure(list(fit = lm(formula, data), call = match.call()), class = "mymodel")
}
registerS3method("coef", "mymodel", function(object, ...) coef(object$fit))
registerS3method("vcov", "mymodel", function(object, ...) vcov(object$fit))
registerS3method("terms", "mymodel", function(x, ...) terms(x$fit))
registerS3method("df.residual", "mymodel", function(object, ...) object$fit$df.residual)
Oats <- as.data.frame(nlme::Oats)
Oats$nitro <- factor(Oats$nitro)
m <- fit_mymodel(yield ~ Block + nitro*Variety, data = Oats)
predictmeans(m, "nitro")$means
#> nitro estimate std.error df conf.low conf.high comparison.low
#> 1 0 79.38889 3.758096 55 71.8575 86.92028 74.06339
#> 2 0.2 98.88889 3.758096 55 91.3575 106.42028 93.56339
#> 3 0.4 114.22222 3.758096 55 106.6908 121.75362 108.89672
#> 4 0.6 123.38889 3.758096 55 115.8575 130.92028 118.06339
#> comparison.high n estimable
#> 1 84.71439 18 TRUE
#> 2 104.21439 18 TRUE
#> 3 119.54772 18 TRUE
#> 4 128.71439 18 TRUE
# Degrees of freedom of its own, e.g. normal-based:
registerS3method("pm_df", "mymodel", function(model, L, ...) rep(Inf, nrow(L)))
predictmeans(m, "nitro")$means
#> nitro estimate std.error df conf.low conf.high comparison.low
#> 1 0 79.38889 3.758096 Inf 72.02316 86.75462 74.18053
#> 2 0.2 98.88889 3.758096 Inf 91.52316 106.25462 93.68053
#> 3 0.4 114.22222 3.758096 Inf 106.85649 121.58796 109.01386
#> 4 0.6 123.38889 3.758096 Inf 116.02316 130.75462 118.18053
#> comparison.high n estimable
#> 1 84.59725 18 TRUE
#> 2 104.09725 18 TRUE
#> 3 119.43058 18 TRUE
#> 4 128.59725 18 TRUE
