I am estimating the following model using the lm
package in R
:
$y_t = \alpha + \beta_1x_t + \beta_2 z_t$.
I am interest in the null hypothesis $H_0: \beta_1 + \beta_2=0$.
How can I test this?
If $\beta_1 + \beta_2 = 0$, then $\beta_1 = -\beta_2$, so $\beta_1 x_t + \beta_2 z_t = \beta_1 x_t - \beta_1 z_t = \beta_1 (x_t - z_t)$. So, in R, you can run
f1 <- lm(y ~ I(x - z), data = data)
f2 <- lm(y ~ x + z, data = data)
anova(f1, f2)
which will give you a test if the model where $\beta_1 + \beta_2 = 0$ (i.e., f1
) fits worse than a model whether $\beta_1$ and $\beta_2$ can freely vary (i.e., f2
). If the comparison is significant, then f2
is the better model and you can reject the null hypothesis that $\beta_1 + \beta_2 = 0$.
More generally, you can use the multcomp
package to test general linear hypotheses:
multcomp::glht(f2, linfct = "x + z = 0")
Great thread which generated some great answers - though I have a feeling it will be moved to Stack Exchange because it is software related (the software being R).
To supplement Noah's answer, I will show an alternative way one can test the hypotheses of interest using the multcomp package. [Note that we can't test just a null hypothesis - we need to specify an alternative hypothesis as well.]
Recall that the linear regression model is:
$y_t = \alpha + \beta_1x_t + \beta_2 z_t$,
while the two competing hypotheses being tested are:
$H_0: \beta_1 + \beta_2 = 0$ and
$H_a: \beta_1 + \beta_2 \neq 0$.
Here, $H_0$ refers to the null hypothesis and $H_a$ refers to the alternative hypothesis.
Step 1:
The first thing we have to note is that the left hand side of the two stated hypotheses is nothing but a linear combination of the regression coefficients $\alpha$, $\beta_1$ and $\beta_2$in the linear regression model. Specifically:
$\beta_1 + \beta_2 = 0*\alpha + 1*\beta_1 + 1*\beta_2$,
where the weights given to the regression coefficients $\alpha$, $\beta_1$ and $\beta_2$ in this linear combination are 0, 1 and 1, respectively.
Step 2:
We will define a matrix of weights W with a single row, which lists the weights of the regression coefficients:
W <- rbind(c(0, 1, 1))
This is what W looks like currently:
> W
[,1] [,2] [,3]
[1,] 0 1 1
Step 3:
We assign proper names to the row and column of weights W. The row name will be beta1 + beta2. The column names will be alpha, beta1 and beta2. This is just so that we can keep track of what linear combination of the coefficients $\alpha$, $\beta_1$ and $\beta_2$ we are interested in testing.
rownames(W) <- c("beta1 + beta2")
colnames(W) <- c("alpha","beta1", "beta2")
W
This is what the beautified version of W looks like:
> W
alpha beta1 beta2
beta1 + beta2 0 1 1
Step 4:
We fit the model and perform the test of the null hypothesis against the alternative hypothesis:
library(multcomp)
model <- lm(y ~ x + z, data = data)
model.test <- glht(model, linfct = W)
summary(model.test)
If we generate the data with the commands below:
set.seed(1)
x = rnorm(100)
z = rnorm(100)
X = model.matrix(~x+z)
y = X%*%c(1,-2,3) + rnorm(100)
data <- data.frame(y=y, x=x)
here is what the R output would look like:
> summary(model.test)
Simultaneous Tests for General Linear Hypotheses
Fit: lm(formula = y ~ x + z, data = data)
Linear Hypotheses:
Estimate Std. Error t value Pr(>|t|)
beta1 + beta2 == 0 0.9676 0.1601 6.043 2.81e-08 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Adjusted p values reported -- single-step method)
From this output, we can see that the p-value for the t-test used to test the null hypothesis against the alternative hypothesis is 2.81e-08 (which is scientific notation for 0.0000000281).
Step 5:
To estimate the value of $\beta1 + \beta2$ and compute an associated confidence interval, we can use the command:
confint(model.test)
whose output will look like this:
> confint(model.test)
Simultaneous Confidence Intervals
Fit: lm(formula = y ~ x + z, data = data)
Quantile = 1.9847
95% family-wise confidence level
Linear Hypotheses:
Estimate lwr upr
beta1 + beta2 == 0 0.9676 0.6498 1.2855
From this output, we can see that the estimated value of $\beta1 + \beta2$ is 0.9676 and the corresponding 95% confidence interval is (0.6498, 1.2855).
We can plot the confidence interval we computed via these commands:
par(mar=c(4,8,4,4))
plot(model.test)
\neq
.
$\endgroup$
\not
which will add a negating slash over the following symbol: \not =
$\not =$, \not <
$\not <$, \not \approx
$\not\approx$, \not\mapsto
$\not\mapsto$
$\endgroup$
Aug 13, 2019 at 7:46
The variance of $\beta_1 + \beta_2$ is $\operatorname{Var}(\beta_1) + \operatorname{Var}(\beta_2) + 2\operatorname{Cov}(\beta_1,\beta_2)$. Obtain the variance and covaraince from the covariance matrix and construct an appropriate confidence interval.
Here is some R code. I'm sure there is a package to do this, but until someone posts that, this should do fine.
x = rnorm(100)
z = rnorm(100)
X = model.matrix(~x+z)
y = X%*%c(1,-2,3) + rnorm(100)
model = lm(y~x+z)
sigma = vcov(model)
var_est = as.numeric(c(0,1,1)%*%sigma%*%c(0,1,1))
betas = coef(model)
(betas[2]+betas[3]) + c(-1,1)*1.96*sqrt(var_est)
Maybe you can try a Chi-square goodness of fit ( observed -expected) for your data points, for two models, one with $\beta_1= -\beta_2$ and another one where you use $\beta_2 \in (\beta_1 - \epsilon, \beta_1 + \epsilon )$ with your choice of $\epsilon >0 $ a Real number, and check the two Chi-squared statistics using the needed degrees of freedom.