test_terms: Testing Model Terms via Nested Models

Description Usage Arguments Examples

View source: R/test_terms.R

Description

Generic interfaces that allows Type III tests of model terms (i.e., main effects and interactions) via nested model comparisons using a user-supplied estimation/fitting function and user-supplied function for comparing two models. The default function for testing model terms is anova which returns likelihood-ratio tests.

Usage

1
2
3
test_terms(formula, data, extra_formula, fit_fun, fit_arg = list(),
  test_fun = anova_df, test_arg = list(), type = 3,
  test_intercept = FALSE, na.action)

Arguments

formula

Two-sided formula. Separate formulas (i.e., nested models) will be created for each model term on right side of this formula. For example, for mixed-effects models this should be the response (i.e., dependent) variable plus the fixed-effects.

data

data.frame passed to model.frame holding the variables in formula and extra_formula.

extra_formula

Optional one-sided formula that will be added to the resulting formulas. No separate formulas will be built based on this formula. For example, for mixed-effects model this should contain the random-effects.

fit_fun

Fitting/estimation function. For example, lm, lmer, ...

fit_arg

list of additional argments passed to fit_fun.

test_fun

Function comparing two models. Needs to return a data.frame with one row and the last two columns need to be the test statistics (e.g., F, Chisq) and the corresponding p-value (e.g., Pr(>F), Pr(>Chisq)). Default is anova_df which is a wrapper for the generic anova function that autodetects relevant columns.

test_arg

additional argument passed to test_fun. See examples for how to use it with the default test_fun.

type

Type of sums of squares. Currently only Type III (i.e., 3 or "III") are supported.

test_intercept

Logical. Should test for intercept be included in output? Default is FALSE

na.action

how NAs are treated. Passed to model.frame.

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
67
68
69
70
71
72
73
74
75
76
77
set_sum_contrasts() ## quite important, currently coding is not checked

data("Machines", package = "MEMSS")

# ignoring repeated-measures
m1 <- test_terms(score ~ Machine, data=Machines,
                 fit_fun = lm)
m1
nice(m1)
anova(m1)

## fixed-effects model
m2 <- test_terms(score ~ Machine*Worker, data=Machines,
                 fit_fun = lm)
m2


# simple model with random-slopes for repeated-measures factor
m3 <- test_terms(score ~ Machine, data=Machines,
                 extra_formula = ~ (Machine|Worker),
                 fit_fun = lme4::lmer, fit_arg = list(REML = FALSE),
                 test_arg = list(model.names=c("f", "r")))
m3
anova(m3)

## specify colnames in anova() output by hand instead of automatically:
m3b <- test_terms(score ~ Machine, data=Machines,
                 extra_formula = ~ (Machine|Worker),
                 fit_fun = lme4::lmer, fit_arg = list(REML = FALSE),
                 test_arg = list(model.names=c("f", "r"),
                                 colnames = list("Chi Df", "Chisq", "Pr(>Chisq)")))
m3b


## Not run: 
# using an example from afex
data("sk2011.2", package = "afex")
# use only affirmation problems (S&K also splitted the data like this)
sk2_aff <- droplevels(sk2011.2[sk2011.2$what == "affirmation",])

library("lme4")
# not a particularly reasonable non-maximal model
sk_m1 <- test_terms(response ~ instruction*inference*type,
                             sk2_aff,
                             extra_formula = ~(inference|id),
                    fit_fun = lmer, fit_arg = list(REML = FALSE),
                    test_arg = list(model.names=c("f", "r")))

nice(sk_m1)
anova(sk_m1)

### matches:
afex::mixed(response ~ instruction*inference*type + (inference|id),
                             sk2_aff, method = "LRT")

## if corresponding method exist, emmeans support is provided automatically:
emmeans::emmeans(sk_m1, c("instruction", "type"))


## End(Not run)

## Not run: 
## It works also with glmmTMB
## see: https://cran.r-project.org/web/packages/glmmTMB/vignettes/glmmTMB.pdf
Owls <- transform(glmmTMB::Owls,
                  Nest=reorder(Nest,NegPerChick),
                  NCalls=SiblingNegotiation,
                  FT=FoodTreatment)
zipp_test <- test_terms(formula = NCalls~(FT+ArrivalTime)*SexParent,
                        data = Owls,
                        extra_formula = ~ offset(log(BroodSize)) + (1|Nest),
                        fit_fun = glmmTMB::glmmTMB,
                        fit_arg = list(ziformula=~1, family=poisson)
)
zipp_test

## End(Not run)

singmann/monet documentation built on April 23, 2021, 3:02 a.m.