8
$\begingroup$

In R, is there a way to use the lm function to test for the hypothesis that the coefficients are different from a value other than zero? For instance, if the model is:

Y = a + b1x1 + b2x2 + b3x3 + e

It is easy to test whether a single b is different from an arbitrary number. If you wanna test for b1 = 10, then you can estimate:

h0 <- lm(Y ~ offset(10*x1) + x2 + x3)
h1 <- lm(Y ~ x1 + x2 + x3)
anova(h0,h1)

However, what if you want to test at the same time that a=3, b1=10, b2=9 and b3=4 but get the p-values for each? Then an anova would only test whether those restrictions are all true at the same time or not. Is there a way to test such restrictions at once but get the p-values of each coefficient?

$\endgroup$
3
  • $\begingroup$ This is primarily about software. So it is off topic here. $\endgroup$ Apr 23, 2017 at 19:03
  • 1
    $\begingroup$ You could simply fit more models and compare them to h1, e.g.: h2 <- lm(Y ~ x1 + offset(9*x2) + x3) If you want to code it in fewer steps, perhaps, then that question would probably best answered at Stack Overflow. $\endgroup$
    – Mark White
    Apr 23, 2017 at 20:31
  • $\begingroup$ The question is originally about software, but I would be more concerned with the multiple comparison problem that arises here - depending on what software approach is used. en.wikipedia.org/wiki/Multiple_comparisons_problem $\endgroup$
    – Pere
    Apr 24, 2017 at 10:21

2 Answers 2

15
$\begingroup$

Rather than using anova() to carry out the F test, you can also use summary() to get the marginal Wald tests for each coefficient. So it is easy to simply include the entire offset of all terms and then use summary(). Here, I use some artificial data with coefficients as suggested in your post:

set.seed(1)
d <- data.frame(
  x1 = runif(100, -1, 1),
  x2 = runif(100, -1, 1),
  x3 = runif(100, -1, 1)
)
d$y <- 3 + 10 * d$x1 + 9 * d$x2 + 4 * d$x3 + rnorm(100)
m <- lm(y ~ x1 + x2 + x3, data = d,
  offset = 3 + 10 * x1 + 9 * x2 + 4 * x3)
summary(m)
## Call:
## lm(formula = y ~ x1 + x2 + x3, data = d, offset = 3 + 10 * x1 + 
##     9 * x2 + 4 * x3)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -3.00507 -0.67884 -0.08825  0.68643  2.55013 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  0.03520    0.10947   0.322    0.748
## x1          -0.09123    0.20091  -0.454    0.651
## x2          -0.19222    0.19572  -0.982    0.329
## x3           0.01885    0.19285   0.098    0.922
## 
## Residual standard error: 1.058 on 96 degrees of freedom
## Multiple R-squared:  0.9824, Adjusted R-squared:  0.9818 
## F-statistic:  1784 on 3 and 96 DF,  p-value: < 2.2e-16

Another convenient option to conduct these tests of linear hypotheses is the linearHypothesis() function from the car package. This can replicate both the marginal Wald tests:

library("car")
m2 <- lm(y ~ x1 + x2 + x3, data = d)
linearHypothesis(m2, "(Intercept) = 3")
## Linear hypothesis test
## 
## Hypothesis:
## (Intercept) = 3
## 
## Model 1: restricted model
## Model 2: y ~ x1 + x2 + x3
## 
##   Res.Df    RSS Df Sum of Sq      F Pr(>F)
## 1     97 107.67                           
## 2     96 107.55  1   0.11586 0.1034 0.7485

...

linearHypothesis(m2, "x3 = 4")
## Linear hypothesis test
## 
## Hypothesis:
## x3 = 4
## 
## Model 1: restricted model
## Model 2: y ~ x1 + x2 + x3
## 
##   Res.Df    RSS Df Sum of Sq      F Pr(>F)
## 1     97 107.56                           
## 2     96 107.55  1    0.0107 0.0096 0.9224

And it can also conduct the F test of all coefficients:

linearHypothesis(m2,
  c("(Intercept) = 3", "x1 = 10", "x2 = 9", "x3 = 4"))
## Linear hypothesis test
## 
## Hypothesis:
## (Intercept) = 3
## x1 = 10
## x2 = 9
## x3 = 4
## 
## Model 1: restricted model
## Model 2: y ~ x1 + x2 + x3
## 
##   Res.Df    RSS Df Sum of Sq     F Pr(>F)
## 1    100 108.93                          
## 2     96 107.55  4    1.3802 0.308  0.872
$\endgroup$
7
$\begingroup$

It's not clear what your confusion is... but the model

$$Y = X \beta + Z \gamma + \varepsilon$$

Can be equivalently expressed as

$$\left( Y - X\beta^0 \right) = X \left( \beta - \beta^0 \right) + Z\gamma + \varepsilon$$

If we're trying to directly test that $\beta = \beta^0$.

$\endgroup$
1
  • 3
    $\begingroup$ This approach can be easily accomplished in R via offset, setting the offset to $X\beta_0$ $\endgroup$
    – Glen_b
    Apr 24, 2017 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.