remef: Remove effects

Description Usage Arguments Details Value See Also Examples

View source: R/remef.R

Description

remef is used to remove fixed and random effects from repsonse variable based on a (linear) mixed-effects model object generated by the lme4 package.

Usage

1
2
3
4
5
6
7
8
remef(
  model,
  fix = NULL,
  ran = NULL,
  grouping = FALSE,
  keep.intercept = TRUE,
  inverse = 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 higher-order terms associated with the ones in fix also be removed? 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).

Details

model is an object returned by a model fitting function of the lme4 package, i.e., lmer for linear mixed-effects models or glmer for generalised linear mixed-effects models. For more on details on mixed-effects models and partial effects, see the help page of partial.

The response vector in a (generalised) linear mixed model can be represented as a sum of fixed effects, random effects, and residuals. remef "removes" a subset of these effects.

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 higher order are removed too. 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.

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 remef 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

remef returns a numeric vector. The length of the vector is equal to the number of observations of the model.

See Also

keepef for a wrapper of partial 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
library(lme4)

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

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

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

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

# remove fixed effect of 'Days' and both random effects
r1_4 <- remef(fm1, fix = "Days", ran = list(Subject = c("(Intercept)", "Days")))
# equivalent command with numeric indices
r1_5 <- remef(fm1, fix = 2, ran = list(Subject = c(1, 2)))
 
# remove all effects
r1_6 <- remef(fm1, fix = 2, ran = "all", keep.intercept = FALSE)


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')
r2_1 <- remef(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
r2_1_prob <- remef(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)`)
r2_2 <- remef(fm2, ran = list(item = c(1, 2), id = 1))
r2_3 <- remef(fm2, ran = "all")  # equivalent command


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

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

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

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

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