partial: Calculate partial effects

Description Usage Arguments Details Value See Also Examples

View source: R/partial.R

Description

partial is used to calculate partial effects of a (generalized) linear mixed-effects model fitted by a function of the lme4 package.

Usage

1
2
3
4
5
6
7
8
9
partial(
  model,
  fix = NULL,
  ran = NULL,
  grouping = FALSE,
  keep.intercept = TRUE,
  inverse = FALSE,
  keep = FALSE
)

Arguments

model

a model object.

fix

either a character vector of coefficient labels (fixed effects) or a corresponding numeric index vector.

ran

a named list of integer vectors representing random effects.

grouping

logical. Should terms associated with the ones in fix also be selected? The functionality of this parameter depends on the value of keep. The details are given under 'Details'.

keep.intercept

logical. Should the intercept be kept (default) or be removed?

inverse

logical. If TRUE, the inverse of the link function is applied (after the effects are removed).

keep

logical. Should the specified effects be removed (default) or be kept?

Details

model is an object returned by a model fitting function of the lme4 package. The function works with both linear mixed-effects models (returned by the function lmer) and generalized linear mixed-effects models (glmer).

The model formula for a (generalised) linear mixed-effects model can be written as

y = X * beta + Z * b + epsilon

where y is the vector including the response values, X is the (fixed-effects) design matrix, beta is the vector of population coefficients (fixed effecs), Z is the random-effects design matrix, b is a vector of random effects, and epsilon is a vector of random error terms (residuals).

The response vector can be represented as a sum of fixed effects, random effects, and residuals. In order to construct partial effects, the function partial either "removes" effects (if keep = FALSE, see also remef) or "keeps" effects (if keep = TRUE, see also keepef). In both cases, the function does keep the residuals. Hence, the function does not return fitted values.

The function removes a subset of fixed and random effects from the response variable. If all effects are removed, only the residuals remain.

The valid character strings for the parameter fix correspond to the names of the coeffcients in the model summary output. Note that the name of a coefficient is not necessarily identical to the name of a predictor variable (e.g., factor variables). Furthermore, a single variable in a model formula can result in multiple model coefficients (e.g., polynomial contrasts). The names of a model fit can be extracted with the function effect_labels. The character string passed to fix must not include the intercept ("(Intercept)"). Use the parameter keep.intercept instead.

If fix is a numeric index vector, the indices correspond to the order of coefficients in the model. For a fitted model object, the effect_labels returns the names of the coefficients in their order. Note that fix must not include the index of the intercept (usually 1). Use the parameter keep.intercept instead.

The parameter ran is used to specify random effects. The names of the list passed to ran correspond to the names of the random factors in the model summary. Alternatively, the string "all" can pe passed to ran which results in the selection of all random effects.

If grouping is FALSE, only the specified fixed effects in fix are used. If grouping is TRUE, associated effects of lower or higher order are used too. The actual behaviour of the function depends on the keep parameter: If keep = FALSE (effects are "removed"), grouping specifies whether effects of higher order should be selected and removed as well. Otherwise, if keep = TRUE (effects are "kept"), grouping specifies whether effects of lower order should be selected and kept as well. For example, if a model fit has the terms A, B, C, A:B, A:C, B:C, and A:B:C, terms of higher order relative to A are A:B, A:C, and A:B:C. Terms of lower order relative to A:B are A and B.

The parameter inverse is only important for generalised linear mixed-effects models (in contrast to linear mixed-effects models). In the former type of statistical models, a link function is specified to transform the response variable. If partial is used with such a type of model, the resulting values will be in the metric of the variable after after the transformation. For example, if the response is binary and a model with the binomial distribution using the logit link funtion is fit, the function partial will return logits unless inverse = TRUE in which case logits will be transformed to proportions after the removal of effects.

Value

partial returns a numeric vector. The length equals the number of observations in the model.

See Also

remef for a wrapper with keep = FALSE and keepef for a wrapper with keep = TRUE.

Examples

 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
library(lme4)

fm1 <- lmer(Reaction ~ 1 + Days + (1 + Days | Subject), sleepstudy)
summary(fm1)

# remove fixed effect of 'Days'
p1_1 <- partial(fm1, fix = "Days")

# remove fixed effect of 'Days' and the intercept
p1_2 <- partial(fm1, fix = "Days", keep.intercept = FALSE)

# remove random slope of 'Days' (random factor: 'Subject')
p1_3 <- partial(fm1, ran = list(Subject = "Days"))

# remove fixed effect of 'Days' and both random effects
p1_4 <- partial(fm1, fix = "Days", ran = list(Subject = c("(Intercept)", "Days")))
# equivalent command with numeric indices
p1_5 <- partial(fm1, fix = 2, ran = list(Subject = c(1, 2)))

# keep the fixed effect of 'Days' (and the intercept), remove all other effects
p1_6 <- partial(fm1, fix = "Days", keep = FALSE)

# remove all effects
p1_7 <- partial(fm1, fix = 2, ran = "all", keep.intercept = FALSE)
p1_8 <- partial(fm1, keep = TRUE, keep.intercept = FALSE)  # equivalent command
all.equal(unname(residuals(fm1)), p1_7)


fm2 <- glmer(r2 ~ 1 + btype + Anger + (1 + Anger || item) + (1 | id), VerbAgg, family = binomial)
summary(fm2)

# remove fixed effects of 'btype'
# (since 'btype' is a three-level factor, two coefficients are estimated, 'btypescold' and 'btypeshout')
p2_1 <- partial(fm2, fix = c("btypescold", "btypeshout"))
# extract coefficient names related to 'btype'
term2coef(fm2, "btype")

#' # since `fm2` is a binomial GLMM, the partial effects are logits,
# the parameter `inverse` can be used to return probablities instead
p2_1_prob <- partial(fm2, fix = c("btypescold", "btypeshout"), inverse = TRUE)

# remove all random effects
# (Note that the order of random effects for the random factor 'item' is:
# 'Anger', '(Intercept)'; see `summary(fm2)`)
p2_2 <- partial(fm2, ran = list(item = c(1, 2), id = 1))
p2_3 <- partial(fm2, ran = "all")  # equivalent command


fm3 <- lmer(angle ~ 1 + recipe * temperature + (1 | recipe:replicate), cake)
summary(fm3)

# remove the random intercept
p3_1 <- partial(fm3, ran = list("recipe:replicate" = 1))

# remove fixed effect of 'recipeB' and all higher-order terms (interactions)
p3_2 <- partial(fm3, fix = "recipeB", grouping = TRUE)
# these are the higher-order terms of 'recipeB'
asef(fm3, "recipeB", order = "higher")

# keep fixed effect 'recipeB:temperature^4' and the lower-order ones (and the intercept)
p3_3 <- keepef(fm3, fix = "recipeB:temperature^4", grouping = TRUE)
# these are the lower-order terms of 'recipeB:temperature^4'
asef(fm3, "recipeB:temperature^4", order = "lower")

# remove all polynomials of 'temperature'
p3_4 <- partial(fm3, fix = term2coef(fm3, "temperature"))

hohenstein/remef documentation built on Jan. 26, 2020, 12:57 a.m.