问题
I want to test if the slope in a simple linear regression is equal to a given constant other than zero.
> x <- c(1,2,3,4)
> y <- c(2,5,8,13)
> fit <- lm(y ~ x)
> summary(fit)
Call:
lm(formula = y ~ x)
Residuals:
1 2 3 4
0.4 -0.2 -0.8 0.6
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -2.0000 0.9487 -2.108 0.16955
x 3.6000 0.3464 10.392 0.00913 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.7746 on 2 degrees of freedom
Multiple R-squared: 0.9818, Adjusted R-squared: 0.9727
F-statistic: 108 on 1 and 2 DF, p-value: 0.009133
> confint(fit)
2.5 % 97.5 %
(Intercept) -6.081855 2.081855
x 2.109517 5.090483
In this example, I want to test if the slope is equal to 5. I know I won't reject it since 5 is in the 95% CI. But is there a function which can give me the p-value directly?
回答1:
One approach to testing whether a fit is significantly different than a particular coefficient is to construct an "offset" in which that coefficient is used as a factor applied to the x-value. You should think of this as re-setting the "zero" at least the zero-for-the-slope. The Intercept is still "free" to "move around", er, to be estimated.
fit2 <- lm( y~x +offset(5*x) )
#----------------
summary(fit2)
#--------
Call:
lm(formula = y ~ x + offset(5 * x))
Residuals:
1 2 3 4
0.4 -0.2 -0.8 0.6
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -2.0000 0.9487 -2.108 0.1695
x -1.4000 0.3464 -4.041 0.0561 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.7746 on 2 degrees of freedom
Multiple R-squared: 0.9818, Adjusted R-squared: 0.9727
F-statistic: 108 on 1 and 2 DF, p-value: 0.009133
Now compare to the results from your fit
-object. The coefficients for x differ by exactly 5. The model fit statistics are the same, but as you suspected the p-value for the x
-variable is much lower ... er, higher, i.e. less significant.
> summary(fit)
Call:
lm(formula = y ~ x)
Residuals:
1 2 3 4
0.4 -0.2 -0.8 0.6
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -2.0000 0.9487 -2.108 0.16955
x 3.6000 0.3464 10.392 0.00913 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.7746 on 2 degrees of freedom
Multiple R-squared: 0.9818, Adjusted R-squared: 0.9727
F-statistic: 108 on 1 and 2 DF, p-value: 0.009133
回答2:
You just have to construct the t-statistic for the null hypothesis slope=5:
# Compute Summary with statistics
sfit<- summary(fit)
# Compute t-student H0: intercept=5. The estimation of coefficients and their s.d. are in sfit$coefficients
tstats <- (5-sfit$coefficients[2,1])/sfit$coefficients[2,2]
# Calculates two tailed probability
pval<- 2 * pt(abs(tstats), df = df.residual(fit), lower.tail = FALSE)
print(pval)
回答3:
My impression is that the linearHypothesis
function from the car
package provides a standard way to do this.
For example
library(car)
x <- 1:4
y <- c(2, 5, 8, 13)
model <- lm(y ~ x)
linearHypothesis(model, "x = 1")
#> Linear hypothesis test
#>
#> Hypothesis:
#> x = 1
#>
#> Model 1: restricted model
#> Model 2: y ~ x
#>
#> Res.Df RSS Df Sum of Sq F Pr(>F)
#> 1 3 35.0
#> 2 2 1.2 1 33.8 56.333 0.01729 *
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
And here the hypothesis tests shows that the restricted model (i.e. the case when the coefficient of x
equals 1) is explaining less variance than the full model in a statistically significant way as evaluated by an F statistic.
This is more useful than using offset
in a formula as you can test multiple restrictions at once:
model <- lm(y ~ x + I(x^2))
linearHypothesis(model, c("I(x^2) = 1", "x = 1"))
#> Linear hypothesis test
#>
#> Hypothesis:
#> I(x^2) = 1
#> x = 1
#>
#> Model 1: restricted model
#> Model 2: y ~ x + I(x^2)
#>
#> Res.Df RSS Df Sum of Sq F Pr(>F)
#> 1 3 30.0
#> 2 1 0.2 2 29.8 74.5 0.08165 .
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
来源:https://stackoverflow.com/questions/33060601/test-if-the-slope-in-simple-linear-regression-equals-to-a-given-constant-in-r