Search code examples
rregressionlinear-regressionlmhypothesis-test

Get no p-value when making hypothesis test on whether slope coefficient is 1.75


I have to test the hypothesis of Ho: ß1 = 1.75. And I write this:

model1<- lm (y - x)
model2<- lm (y - x, offset = 1.75*x)
anova(model1, model2)

but I couldn't get a P-value.

enter image description here

Any ideas? Thank you.


Editor Notice

This is not a typo (so don't close this as typo); OP's ANOVA output shows that he / she manages to run lm successfully, so the - is likely to be a mistyping. The real issue is with the use of anova.


Solution

  • There is a syntax error: use tilde ~ for formula. Read ?lm and ?formula for more.

    You want to perform t-test on a coefficient. The default NULL hypothesis is 0 coefficient. As you are aware of, we can use offset or something similar to shift the coefficient.

    summary will produce a table / matrix for coefficients, and you can use coef to extract that table / matrix:

    coef(summary(lm(y ~ x, offset = 1.75 * x)))
    

    Reproducible example

    set.seed(0)
    xx <- rnorm(100)
    yy <- 1.3 * xx - 0.2 + rnorm(100, sd = 0.5)
    
    coef(summary(lm(yy ~ xx, offset = 1.75 * xx)))
    
    #              Estimate Std. Error   t value     Pr(>|t|)
    #(Intercept) -0.2243489 0.04814396 -4.659960 9.974037e-06
    #xx          -0.3806026 0.05480131 -6.945137 4.170746e-10
    

    The p-value for coefficient of xx is ~e-10, so the NULL hypothesis is rejected.


    Why not F-test?

    Your question was closed as being a typo, but in fact it is more than that. You definitely don't want anova here. The following models are equivalent:

    m1 <- lm(y ~ x)
    m2 <- lm(y ~ x, offset = 1.75 * x)
    

    They only differ in coefficient for slope, and everything else is the same (even the standard error of the slope). If you use anova,

    anova(m1, m2)
    
    #Model 1: yy ~ xx
    #Model 2: yy ~ xx  ## offset is not shown in formula
    #  Res.Df  RSS Df  Sum of Sq F Pr(>F)
    #1     98 22.7                       
    #2     98 22.7  0 3.5527e-15  
    

    we indeed see the same model degree of freedom and RSS for both models.

    Recall if you have degree of freedom d1 and d2 for m1 and m2, F-statistic is

    ( (RSS1 - RSS2) / (d2 - d1) ) / ( RSS2 / (n - d2) )
    

    which has F-distribution with degree of freedom (d2 - d1, n - d2). If d1 = d2 and RSS1 = RSS2, how can you construct F-statistic???