If my model looks like this, Y=β0 β1X1 β2X2 β3X3 β4X4, and I want to perform an F test (5%) in R for β1=β2, how do I do it? The only tutorials I can find online deal with β1=β2=0, but that's not what I'm looking for here.
CodePudding user response:
Here's an example in R testing whether the coefficient for vs
is the same as the coefficient for am
:
data(mtcars)
mod <- lm(mpg ~ hp disp vs am, data=mtcars)
library(car)
linearHypothesis(mod, "vs=am")
# Linear hypothesis test
#
# Hypothesis:
# vs - am = 0
#
# Model 1: restricted model
# Model 2: mpg ~ hp disp vs am
#
# Res.Df RSS Df Sum of Sq F Pr(>F)
# 1 28 227.07
# 2 27 213.52 1 13.547 1.7131 0.2016
CodePudding user response:
The glht
function from multcomp
package can do this (among others). For example, if your model is
mod1 <-lm( y ~ x1 x2 x3 x4)
then you can use:
summary(multcomp::glht(mod1, "x1-x2=0"))
CodePudding user response:
Run the model with and without the constraint and them use anova to compare them. No packages are used.
mod1 <- lm(mpg ~ cyl disp hp drat, mtcars)
mod2 <- lm(mpg ~ I(cyl disp) hp drat, mtcars) # constraint imposed
anova(mod2, mod1)
giving:
Analysis of Variance Table
Model 1: mpg ~ I(cyl disp) hp drat
Model 2: mpg ~ cyl disp hp drat
Res.Df RSS Df Sum of Sq F Pr(>F)
1 28 252.95
2 27 244.90 1 8.0513 0.8876 0.3545
The underlying calculation is the following. It gives the same result as above.
L <- matrix(c(0, 1, -1, 0, 0), 1) # hypothesis is L %*% beta == 0
q <- nrow(L) # 1
co <- coef(mod1)
resdf <- df.residual(mod1) # = nobs(mod1) - length(co) = 32 - 5 = 27
SSH <- t(L %*% co) %*% solve(L %*% vcov(mod1) %*% t(L)) %*% L %*% co
SSH/q # F value
## [,1]
## [1,] 0.8876363
pf(SSH/q, q, resdf, lower.tail = FALSE) # p value
## [,1]
## [1,] 0.3544728