# How is the F-Stat in a regression in R calculated [duplicate]

I am running a regression and I'd like to be able to do the calculation to get to the F stat .3062. How is this .3062 calculated? Can you help?

x<-rnorm(20000)
y<-rnorm(20000)
data<- data.frame(x =x, y=y)

fit<-lm(x ~ y)
summary(fit)

####### calculation for r squared
meanX<-mean(x)
tss<- sum( (x - meanX)^2)
r.squared

Call:
lm(formula = x ~ y)

Residuals:
Min      1Q  Median      3Q     Max
-3.6405 -0.6794  0.0033  0.6674  4.3485

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.003090   0.007064   0.437    0.662
y           0.003897   0.007043   0.553    0.580

Residual standard error: 0.999 on 19998 degrees of freedom
Multiple R-squared:  1.531e-05, Adjusted R-squared:  -3.469e-05
F-statistic: 0.3062 on 1 and 19998 DF,  p-value: 0.58


Model that you are estimating: $x_i = \alpha + \beta_yy_i$

$H_0: \beta_y = 0$

$F = \frac{(RSS_0-RSS)/p}{RSS/(n-p-1)}$

It's good idea to set seed when using random numbers so others can reproduce.

Anyway try to see if this makes sense to you:

> set.seed(133)
> x<-rnorm(20000)
> y<-rnorm(20000)
> data<- data.frame(x, y)
>
> fit<-lm(x ~ y, data = data)
> summary(fit)

Call:
lm(formula = x ~ y, data = data)

Residuals:
Min      1Q  Median      3Q     Max
-3.9285 -0.6780 -0.0026  0.6747  3.9789

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)  0.012254   0.007103   1.725   0.0845 .
y           -0.004023   0.007120  -0.565   0.5721
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.005 on 19998 degrees of freedom
Multiple R-squared:  1.596e-05, Adjusted R-squared:  -3.404e-05
F-statistic: 0.3192 on 1 and 19998 DF,  p-value: 0.5721

>
> RSS0 <- sum((x - mean(x))^2) #20181.97, this is same as TSS really
> RSS <- sum(fit\$residuals^2) #20181.64
> p <-  1 #predictors whos coefficient we are testing.
> n <- length(y) #number of observations
>