I don’t know any function capable of doing this the R. In general, what is suggested is to do likelihood ratio tests, which are much more general and solve more sophisticated problems, even in generalized linear models.
However, nothing prevents us from writing our own function. After all, we have to test the hypotheses
H_0: beta = beta_0
H_1: beta != beta_0
where beta_0 is the reference value under which we wish to perform the test. By default, the value of beta_0 is zero. The function TesteCoeficiente
, defined below, implements this hypothesis test for any value of beta_0. Simply enter the following values for the function:
reg
: the regression model obtained with the function lm
coeficiente
: the coefficient number in the regression model. For y = \beta_0 + \beta_1*x
, 1 means testing \beta_0
and 2 means testing \beta_1
h0
: null hypothesis value to be tested. In general, this value is zero
With this set, just run the example below to see how the function is executed:
x <- c(1, 2, 3, 4, 5)
y <- c(1.2, 2.4, 3.3, 4.2, 5.1)
reg <- lm(y~x)
TesteCoeficiente <- function(reg, coeficiente, h0){
estimativas <- coef(summary(reg))
estatistica <- (estimativas[coeficiente, 1]-h0)/estimativas[coeficiente, 2]
2 * pt(abs(estatistica), reg$df.residual, lower.tail = FALSE)
}
TesteCoeficiente(reg, coeficiente=2, h0=0)
[1] 2.771281e+01 1.031328e-04
summary(reg)
Call:
lm(formula = y ~ x)
Residuals:
1 2 3 4 5
-1.200e-01 1.200e-01 6.000e-02 1.457e-16 -6.000e-02
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.36000 0.11489 3.133 0.051929 .
x 0.96000 0.03464 27.713 0.000103 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.1095 on 3 degrees of freedom
Multiple R-squared: 0.9961, Adjusted R-squared: 0.9948
F-statistic: 768 on 1 and 3 DF, p-value: 0.0001031
Note that the output of the function TesteCoeficiente
is identical to the function summary
testing h0 = 0
. Therefore, the function works for this particular case. It is quite reasonable to assume that it works for any beta_0 value. Now just change the value of the parameter h0
function to perform the desired hypothesis test.