1
$\begingroup$

I'm trying to run the most basic GLS regression but I'm not getting the expected results. My GLS uses the identity matrix as the error covariance matrix.

In R, the GLS regression is:

set.seed(0)
n = 50
x = as.matrix(seq(1, n))
X = as.matrix(cbind(rep(1, n), x))
y = 2 + 3 * x + rnorm(n)

V = diag(n)

m = glm(y~1+x, weights=diag(V))
s = summary(m)

So far so good. Now, I do the above by hand instead of using the glm package. From wikipedia:

$$ \hat{\beta} = (X'V^{-1}X)^{-1} X' V^{-1} y \\ Cov(\hat{\beta}) = (X'V^{-1}X)^{-1} $$

Then define $\beta$'s covariance matrix as $C = Cov(\hat{\beta}) = (X'V^{-1}X)^{-1}$. Then the diagonal elements $Var(\hat{\beta}_i) = c_{ii}$ are the variances. This allows me to calculate the standard errors:

$$ se(\hat{\beta}_i) = \sqrt{Var(\hat{\beta}_i)} = \sqrt{c_{ii}} \\ \text{tvalue} = \frac{\hat{\beta}_i}{se(\hat{\beta}_i)} $$

b = solve(t(X) %*% solve(V) %*% X) %*% t(X) %*% solve(V) %*% y

var_b = solve(t(X) %*% solve(V) %*% X)
st_err = sqrt(diag(var_b))
tvalues = b/st_err

I get the coefficients correctly, but my standard errors and tvalues are different. I can also successfully calculate $\hat{\sigma}^2$ so I'm clearly overlooking something about $Cov(\hat{\beta})$.

$\endgroup$

1 Answer 1

3
$\begingroup$

By default, glm for a gaussian family will use $$\widehat{\mathrm{cov}}[\hat\beta]=\hat\sigma^2\left(X^TV^{-1}X\right)^{-1}$$

To use a known dispersion parameter instead of estimating, specify it as an option to summary

> summary(m, dispersion=1)

Call:
glm(formula = y ~ 1 + x, weights = diag(V))

Coefficients:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept)   2.1409     0.2871   7.456 8.91e-14 ***
x             2.9954     0.0098 305.657  < 2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for gaussian family taken to be 1)

which agrees with your calculations

$\endgroup$
3
  • $\begingroup$ I'm a bit confused. How is this different from my equations in which $\sigma^2 = 1$? $\endgroup$
    – s5s
    Commented Nov 18 at 15:37
  • $\begingroup$ Let, $cov(\hat{\beta}) = \sigma^2 (X'V^{-1}X)^{-1} = (X'W^{-1}X)^{-1}$. I am passing $W$ - are you saying the library expects me to pass $V$ and it will multiply by the estimated $\hat{\sigma}^2$ $\endgroup$
    – s5s
    Commented Nov 18 at 15:43
  • $\begingroup$ glm isn't designed for GLS specifically, it's for generalised linear models, but basically yes. You pass it a vector that is $W$ or proportional to $W$ and it will compute $\hat\sigma^2$. If you want GLS you might try nlme::gls $\endgroup$ Commented Nov 18 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.