È possibile utilizzare un semplice test t come proposto da Glen_b o un test Wald più generale.
Il test Wald consente di testare più ipotesi su più parametri. È formulato come: dove R seleziona (una combinazione di) coefficienti e q indica il valore da testare, essendo i coefficienti di regressione standard.βR β= qβ
Nel tuo esempio, dove hai solo un'ipotesi su un parametro, R è un vettore di riga, con un valore di uno per il parametro in questione e zero altrove, e q è uno scalare con la restrizione da testare.
In R, è possibile eseguire un test di Wald con la funzione linearHypothesis () dal pacchetto di auto . Diciamo che si desidera verificare se il secondo coefficiente (indicato dall'argomento hypothesis.matrix ) è diverso da 0.1 (argomento 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
Per il test t, questa funzione implementa il test t mostrato da 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
Assicuriamoci di aver ottenuto la procedura corretta confrontando Wald, il nostro test t e il test t predefinito R, per l'ipotesi standard che il secondo coefficiente sia 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
Dovresti ottenere lo stesso risultato con le tre procedure.