Debian Codegraph @

man/constraint_matrices.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/Wald_test.R
3 \name{constraint_matrices}
4 \alias{constraint_matrices}
5 \alias{constrain_zero}
6 \alias{constrain_equal}
7 \alias{constrain_pairwise}
8 \title{Create constraint matrices}
9 \usage{
10 constrain_zero(constraints, coefs, reg_ex = FALSE)
11
12 constrain_equal(constraints, coefs, reg_ex = FALSE)
13
14 constrain_pairwise(constraints, coefs, reg_ex = FALSE, with_zero = FALSE)
15 }
16 \arguments{
17 \item{constraints}{Set of constraints to test. Can be logical (using
18 \code{TRUE} to specify which coefficients to constrain), integer (specify
19 the index of coefficients to constrain), character (specify the names of
20 the coefficients to constrain), or a regular expression.}
21
22 \item{coefs}{Vector of coefficient estimates, used to determine the column
23 dimension of the constraint matrix. Can be omitted if the function is
24 called inside \code{Wald_test()}.}
25
26 \item{reg_ex}{Logical indicating whether \code{constraints} should be
27 interpreted as a regular expression. Defaults to \code{FALSE}.}
28
29 \item{with_zero}{Logical indicating whether coefficients should also be
30 compared to zero. Defaults to \code{FALSE}.}
31 }
32 \value{
33 A matrix or list of matrices encoding the specified set of
34 constraints.
35 }
36 \description{
37 Helper functions to create common types of constraint matrices,
38 for use with \code{\link{Wald_test}} to conduct Wald-type tests of linear
39 contrasts from a fitted regression model.
40 }
41 \details{
42 Constraints can be specified as character vectors, regular
43 expressions (with \code{reg_ex = TRUE}), integer vectors, or logical
44 vectors.
45
46 \code{constrain_zero()} Creates a matrix that constrains a specified set of
47 coefficients to all be equal to zero.
48
49 \code{constrain_equal()} Creates a matrix that constrains a specified set
50 of coefficients to all be equal.
51
52 \code{constrain_pairwise()} Creates a list of constraint matrices
53 consisting of all pairwise comparisons between a specified set of
54 coefficients. If \code{with_zero = TRUE}, then the list will also include a
55 set of constraint matrices comparing each coefficient to zero.
56 }
57 \examples{
58
59 if (requireNamespace("carData", quietly = TRUE)) withAutoprint({
60
61 data(Duncan, package = "carData")
62 Duncan$cluster <- sample(LETTERS[1:8], size = nrow(Duncan), replace = TRUE)
63
64 Duncan_fit <- lm(prestige ~ 0 + type + income + type:income + type:education, data=Duncan)
65 # Note that type:income terms are interactions because main effect of income is included
66 # but type:education terms are separate slopes for each unique level of type
67
68 Duncan_coefs <- coef(Duncan_fit)
69
70 # The following are all equivalent
71 constrain_zero(constraints = c("typeprof:income","typewc:income"),
72 coefs = Duncan_coefs)
73 constrain_zero(constraints = ":income", coefs = Duncan_coefs,
74 reg_ex = TRUE)
75 constrain_zero(constraints = 5:6, coefs = Duncan_coefs)
76 constrain_zero(constraints = c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE),
77 coefs = Duncan_coefs)
78
79 # The following are all equivalent
80 constrain_equal(c("typebc:education","typeprof:education","typewc:education"),
81 Duncan_coefs)
82 constrain_equal(":education", Duncan_coefs, reg_ex = TRUE)
83 constrain_equal(7:9, Duncan_coefs)
84 constrain_equal(c(FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,TRUE,TRUE,TRUE),
85 Duncan_coefs)
86
87 # Test pairwise equality of the education slopes
88 constrain_pairwise(":education", Duncan_coefs,
89 reg_ex = TRUE)
90
91 # Test pairwise equality of the income slopes, plus compare against zero
92 constrain_pairwise(":income", Duncan_coefs,
93 reg_ex = TRUE, with_zero = TRUE)
94
95 })
96
97 }
98 \seealso{
99 \code{\link{Wald_test}}
100 }