Creating your own OLS function should be completed without using base R statistical functions like sd() and var(). Basic math functions like mean, sqrt and length are fine.

Calculating \(\beta_0\) and \(\beta_1\)

Recall the formulas for calculating the least squares estimators \(\hat{\beta_0}\) and \(\hat{\beta_1}\) in the equation \(\hat{y_i} = \hat{\beta_0} + \hat{\beta_1}x_i\):

Once we have the coefficients and , we can use those estimates to create an equation that, with our observed values for \(x\), estimates predicted or fitted (i.e., fitting the regression line) values \(\hat{y}\):

Now we have all the terms we need to calculate the standard errors. Display 7.7 of the Statistical Sleuth presents the formulas for calculating standard errors for the intercept and slope estimates with simple linear regression. As we do not know the true population standard deviation \(\sigma\), we estimate a sample standard deviation \(\hat{\sigma}\) and substitute for \(\sigma\) as in the formulas below.