if (requireNamespace("carData", quietly = TRUE)) withAutoprint({
data(Duncan, package = "carData")
Duncan$cluster <- sample(LETTERS[1:8], size = nrow(Duncan), replace = TRUE)
Duncan_fit <- lm(prestige ~ 0 + type + income + type:income + type:education, data=Duncan)
# Note that type:income terms are interactions because main effect of income is included
# but type:education terms are separate slopes for each unique level of type
Duncan_coefs <- coef(Duncan_fit)
# The following are all equivalent
constrain_zero(constraints = c("typeprof:income","typewc:income"),
coefs = Duncan_coefs)
constrain_zero(constraints = ":income", coefs = Duncan_coefs,
reg_ex = TRUE)
constrain_zero(constraints = 5:6, coefs = Duncan_coefs)
constrain_zero(constraints = c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE),
coefs = Duncan_coefs)
# The following are all equivalent
constrain_equal(c("typebc:education","typeprof:education","typewc:education"),
Duncan_coefs)
constrain_equal(":education", Duncan_coefs, reg_ex = TRUE)
constrain_equal(7:9, Duncan_coefs)
constrain_equal(c(FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,TRUE,TRUE,TRUE),
Duncan_coefs)
# Test pairwise equality of the education slopes
constrain_pairwise(":education", Duncan_coefs,
reg_ex = TRUE)
# Test pairwise equality of the income slopes, plus compare against zero
constrain_pairwise(":income", Duncan_coefs,
reg_ex = TRUE, with_zero = TRUE)
})
Run the code above in your browser using DataLab