5
$\begingroup$

For example, the following R code performs a linear regression:

set.seed(123) 
x <- 1:40
y <- rnorm(40)+((x-20)/10)^2
fit.linear <- lm(y~x)
summary(fit.linear)

and outputs:

Call:
lm(formula = y ~ x)

Residuals:
    Min      1Q  Median      3Q     Max 
-3.2849 -1.2145 -0.2076  1.4374  3.2221 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)  
(Intercept) 1.200322   0.533666   2.249   0.0304 *
x           0.008774   0.022684   0.387   0.7011  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.656 on 38 degrees of freedom
Multiple R-squared:  0.003922,  Adjusted R-squared:  -0.02229 
F-statistic: 0.1496 on 1 and 38 DF,  p-value: 0.7011

How are the t-values for the intercept as well as the coefficient of x computed?

I see that each t-value is equal to the his estimate of the coefficient divided by its standard error. But how is the standard error computed?

$\endgroup$
0

1 Answer 1

2
$\begingroup$

First calculate the residual standard error

summary(fit.linear)$sigma

[1] 1.656053

sigma<-sqrt(sum((fitted(fit.linear)-y)^2)/(length(x)-2))

sigma

[1] 1.656053

Than calculate the standard error of the slope

sqrt(vcov(summary(fit.linear))[2,2])

[1] 0.02268353

se_slope<-sqrt(length(x)*sigma^2/(length(x)*sum(x^2)-sum(x)^2))

se_slope

[1] 0.02268353

and, finally, of the intercept

sqrt(vcov(summary(fit.linear))[1,1])

[1] 0.5336658

sqrt(se_slope^2*(1/length(x))*sum(x^2))

0.5336658

t-values are then, for example for the slope

summary(fit.linear)$coef[2,1]/se_slope

0.3867889

See https://en.wikipedia.org/wiki/Simple_linear_regression#Numerical_example

$\endgroup$

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