Returns the model matrix used for the fit (data = NULL) or rebuilt for
new data with the stored terms, factor levels, contrasts and spline
knots (splines::ns(), splines::bs() and poly() keep the basis of
the fit). Its columns match coef(object). With a regression spline,
model.matrix(fit, grid) %*% coef(fit) traces the fitted curve, and the
covariance vcov(fit) gives pointwise confidence bands; for
fixed-effects fits the curve is identified up to a constant, so center
it at a reference value.
Usage
# S3 method for class 'panglm'
model.matrix(object, data = NULL, cre_means = NULL, ...)See also
Other panglm methods:
coefplot(),
confint.panglm(),
effectcurve(),
fitted.panglm(),
fixef(),
glance.panglm(),
logLik.panglm(),
partialeffects.panordered(),
plot.panglm(),
plot.panglm_ape(),
plot.panglm_effectcurve(),
predict.panglm(),
predict.panordered(),
residuals.panglm(),
summary.panglm(),
tidy.panglm(),
vcov.panglm()
Examples
data(copd)
fit <- panglm(fev1 ~ splines::ns(crp, 3), data = copd,
index = c("id", "visit"), model = "within")
grid <- data.frame(crp = seq(min(copd$crp), max(copd$crp), length.out = 5))
X <- model.matrix(fit, grid)
X0 <- model.matrix(fit, data.frame(crp = median(copd$crp)))
Xc <- sweep(X, 2, X0) # centered at the median CRP
cbind(grid, effect = drop(Xc %*% coef(fit)),
se = sqrt(rowSums((Xc %*% vcov(fit)) * Xc)))
#> crp effect se
#> 1 0.1 0.17651379 0.06824028
#> 2 3.4 0.04928458 0.01512769
#> 3 6.7 -0.04884615 0.01859332
#> 4 10.0 -0.21194570 0.05430171
#> 5 13.3 -0.42355337 0.14824802