Use the purity-hydrocarbon data from Homework #2 and R to answer the following questions. The data are hosted on my github and the linke is provided on the WISE page.

Use R to report the least squares regression line equation. Please submit your R code for both parts.

a) First program it from scratch using the least squares formulas that we derived.

#First Attempt

beta_1 <- sum((x-(mean(x))*(y-mean(y))))/(sum(x-mean(x))^2)
beta_0 <- mean(y)-beta_1*mean(x)

Verifying:

beta_1
## [1] 5.315003e+30
beta_0
## [1] -6.284991e+30
xy <- lm(y~x)
xy
## 
## Call:
## lm(formula = y ~ x)
## 
## Coefficients:
## (Intercept)            x  
##       77.86        11.80

These two arenโ€™t the same at all.

This became too complicated, so I decided to break it up into separate parts.

# Second Attempt

x2 <- sum((x-mean(x))*(y-mean(y)))
x3 <- sum((x-mean(x))^2)

beta_1 <- x2/x3
beta_0 <- mean(y) - beta_1*mean(x)

beta_0
## [1] 77.86328
beta_1
## [1] 11.80103

b) Verify your solutions using the lm() function built into R.

xy <- lm(y~x)
xy
## 
## Call:
## lm(formula = y ~ x)
## 
## Coefficients:
## (Intercept)            x  
##       77.86        11.80

That looks better!