I do not know of any function capable of doing this in 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 β_0 is the reference value under which we wish to perform the test. By default, the value of \ beta_0 equals zero. The TesteCoeficiente
function, defined below, implements this hypothesis test for any value of \ beta_0. Just enter the following values for the function:
reg
: The regression model obtained with the function lm
coeficiente
: the number of the coefficient in the regression model. For y = \beta_0 + \beta_1*x
, 1 means test \beta_0
and 2 means test \beta_1
h0
: value of the null hypothesis 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 TesteCoeficiente
function is identical to that of the summary
function by testing h0 = 0
. Therefore, the function works for this particular case. It is quite reasonable to assume that it works for any value of \ beta_0. Now just change the value of the h0
parameter of the function to perform the desired hypothesis test.