22

In R, when I have a (generalized) linear model (lm, glm, gls, glmm, ...), how can I test the coefficient (regression slope) against any other value than 0? In the summary of the model, t-test results of the coefficient are automatically reported, but only for comparison with 0. I want to compare it with another value.

I know I can use a trick with reparametrizing y ~ x as y - T*x ~ x, where T is the tested value, and run this reparametrized model, but I seek simpler solution, that would possibly work on the original model.

Tomas
  • 5,735
  • 11
  • 52
  • 93
  • related: https://stats.stackexchange.com/questions/29981/should-confidence-intervals-for-linear-regression-coefficients-be-based-on-the-n – Tomas Oct 24 '19 at 11:20

3 Answers3

21

Here's a broader solution that will work with any package, or even if you only have the regression output (such as from a paper).

Take the coefficient and its standard error.

Compute $t=\frac{\hat{\beta}-\beta_{H_0}}{\text{s.e.}(\hat{\beta})}$. The d.f. for the $t$ are the same as they would be for a test with $H_0: \beta=0$.

Glen_b
  • 257,508
  • 32
  • 553
  • 939
  • 1
    Thanks Glen, I know this from [this great answer]. But how will I get p-value from the t-value? – Tomas Aug 12 '14 at 15:39
  • 2
    @Curious `pt()` – Affine Aug 12 '14 at 15:54
  • @Curious: As Affine saysm the R function `pt` - or anything else that gives you the value of t cdfs. Many packages have these, and there are widely available t-tables. – Glen_b Aug 12 '14 at 15:59
  • It would be nice if lm, lmer and the others accepted a test parameter different from zero directly. – skan Mar 10 '17 at 16:49
  • @skan it's literally a single line of R code to get a p-value; it would be a simple matter to write a little function to take the output of summary.lm and produce a new table to your exact specifications. – Glen_b May 13 '17 at 22:28
  • Could you also reply my question in Glen_b answer, please? – skan May 14 '17 at 10:10
  • What about lm(I(y - T*x) ~ x, ...) ? That way you would be substracting something not independent. Wouldn't it be a problem with the assumptions for least squares or with collinearity? How is it different from lm(y ~ x +offset(T*x))? – skan May 19 '17 at 15:38
12

You can use either a simple t-test as proposed by Glen_b, or a more general Wald test.

The Wald test allows to test multiple hypotheses on multiple parameters. It is formulated as: $R\beta=q$ where R selects (a combination of) coefficients, and q indicates the value to be tested against, $\beta$ being the standard regresison coefficients.

In your example, where you have just one hypothesis on one parameter, R is a row vector, with a value of one for the parameter in question and zero elsewhere, and q is a scalar with the restriction to test.

In R, you can run a Wald test with the function linearHypothesis() from package car. Let us say you want to check if the second coefficient (indicated by argument hypothesis.matrix) is different than 0.1 (argument rhs):

reg <- lm(freeny)
coef(reg)

# wald test for lag.quarterly.revenue =0.1
>library(car)
>linearHypothesis(reg, hypothesis.matrix = c(0, 1, rep(0,3)), rhs=0.1)
#skip some result, look at last value on last row, of Pr(>F) 
  Res.Df       RSS Df  Sum of Sq      F Pr(>F)
1     35 0.0073811                            
2     34 0.0073750  1 6.0936e-06 0.0281 0.8679

For the t-test, this function implements the t-test shown by Glen_b:

ttest <- function(reg, coefnum, val){
  co <- coef(summary(reg))
  tstat <- (co[coefnum,1]-val)/co[coefnum,2]
  2 * pt(abs(tstat), reg$df.residual, lower.tail = FALSE)
}

> ttest(reg, 2,0.1)
[1] 0.8678848

Let us make sure we got the right procedure by comparing the Wald, our t-test, and R default t-test, for the standard hypothesis that the second coefficient is zero:

> linearHypothesis(reg, hypothesis.matrix = c(0, 1, rep(0,3)), rhs=0)[["Pr(>F)"]][2]
[1] 0.3904361
> ttest(reg, 2,0)
[1] 0.3904361
## The 'right' answer from R:
> coef(summary(reg))[2,4]
[1] 0.3904361

You should get the same result with the three procedures.

Matifou
  • 2,699
  • 14
  • 25
  • looks good! Can you please explain the `hypothesis.matrix` parameter? – Tomas Aug 12 '14 at 14:42
  • I am not sure if the Wald test does it. I meant to use the normal t-test which is standardly reported along with the parameters, but not with 0 but with some other value. – Tomas Aug 14 '14 at 16:00
  • @Curious Hope it is clearer now? – Matifou Aug 26 '14 at 14:41
3

In the end, farly the easiest solution was to do the reparametrization:

gls(I(y - T*x) ~ x, ...)
Tomas
  • 5,735
  • 11
  • 52
  • 93
  • Will that produce the same results? – skan Dec 16 '16 at 17:27
  • But you are substracting something not independent. Wouldn't it be a problem with the assumptions for least squares or with collinearity? How is it different from lm(y ~ x + +offset(T*x))? – skan Mar 10 '17 at 16:52
  • 1
    @skan the regression is conditional on x, there's no dependence there; it should be the same as using offset. – Glen_b May 21 '17 at 04:44