20
$\begingroup$

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.

$\endgroup$
17
$\begingroup$

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$.

$\endgroup$
  • 1
    $\begingroup$ Thanks Glen, I know this from [this great answer]. But how will I get p-value from the t-value? $\endgroup$ – Curious Aug 12 '14 at 15:39
  • 2
    $\begingroup$ @Curious pt() $\endgroup$ – Affine Aug 12 '14 at 15:54
  • $\begingroup$ @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. $\endgroup$ – Glen_b Aug 12 '14 at 15:59
  • $\begingroup$ It would be nice if lm, lmer and the others accepted a test parameter different from zero directly. $\endgroup$ – skan Mar 10 '17 at 16:49
  • $\begingroup$ @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. $\endgroup$ – Glen_b May 13 '17 at 22:28
10
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ looks good! Can you please explain the hypothesis.matrix parameter? $\endgroup$ – Curious Aug 12 '14 at 14:42
  • $\begingroup$ 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. $\endgroup$ – Curious Aug 14 '14 at 16:00
  • $\begingroup$ @Curious Hope it is clearer now? $\endgroup$ – Matifou Aug 26 '14 at 14:41
3
$\begingroup$

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

gls(I(y - T*x) ~ x, ...)
$\endgroup$
  • $\begingroup$ Will that produce the same results? $\endgroup$ – skan Dec 16 '16 at 17:27
  • $\begingroup$ 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))? $\endgroup$ – skan Mar 10 '17 at 16:52
  • 1
    $\begingroup$ @skan the regression is conditional on x, there's no dependence there; it should be the same as using offset. $\endgroup$ – Glen_b May 21 '17 at 4:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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