1

I'm getting wildly different answers when I calculate the t-statistic manually vs the t-stat shown in the regression summary. Any ideas how to fix it?

x1 = runif(4000, min = 0, max = 1)
x2 = runif(4000, min = 0, max = 1)

y <- 1x1 - 2x2 + rnorm(4000)

X = cbind(x1,x2)

Manually find OLS estimators

estimators <- solve(t(X)%%X)%%t(X)%*%y > estimators [,1] x1 1.020039 x2 -2.012627

Manually find tstat for x2

> -2.012627 / (sd(x2)/sqrt(length(x2))) [1] -441.7463

tstat from regression

reg1 = lm(y ~ ., data = yx) summary(reg1)

Coefficients: Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.01027 0.04225 -0.243 0.808
x1 1.02882 0.05516 18.653 <2e-16 *** x2 -2.00394 0.05523 -36.284 <2e-16 ***

bandcar
  • 113
  • Why are you using (sd(x2)/sqrt(length(x2))) ? – Henry Sep 03 '22 at 23:16
  • That's the standard error – bandcar Sep 03 '22 at 23:52
  • I think it may be the standard error of the mean of x2 but that may not be what you want here – Henry Sep 04 '22 at 00:53
  • Apart from using the wrong standard error when you do it manually, you're also not computing the same estimated coefficients $\hat{\beta}$ in both places. When you compute it manually, you haven't included an intercept. But lm() will include an intercept unless you tell it not to, for instance like this: lm(y ~ 0 + x1 + x2) – civilstat Sep 06 '22 at 01:16

1 Answers1

1

The t-statistic you're using is the definition of the t-statistic for a difference in means between 2 groups, which is different. The R software is giving the correct coefficient; the t-statistic is defined as the estimate divided by the standard error. In this case, the standard error is not equal to sd(x2) / sqrt(length(x2)).

  • How would I manually compute the standard error? How would you suggest fixing this to manually get the t-statistic? – bandcar Sep 03 '22 at 23:53
  • I would suggest not calculating it manually, since there's literally 0 need to. If someone puts a gun to your head and forces you, you can find the solution here, but why would you? https://stats.stackexchange.com/questions/44838/how-are-the-standard-errors-of-coefficients-calculated-in-a-regression/44841#44841 – Closed Limelike Curves Sep 04 '22 at 00:14