Reading data
x <- c(21,24,32,47,50,59,68,74,62,50,41,30)
y <- c(185.79,214.47,288.03,424.84,454.68,539.03,621.55,675.06,562.03,452.93,369.95,273.98)
Building model
model <- lm(y~x)
model
##
## Call:
## lm(formula = y ~ x)
##
## Coefficients:
## (Intercept) x
## -6.332 9.208
Hence our equation becomes
\(\hat{Y_i} = -6.332 + 9.208 X_i\)
Lets plot and see our graph
plot(x,y)
abline(lm(y~x))

Lets see more statistics of same
summary(model)
##
## Call:
## lm(formula = y ~ x)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.5629 -1.2581 -0.2550 0.8681 4.0581
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -6.33209 1.67005 -3.792 0.00353 **
## x 9.20847 0.03382 272.255 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.946 on 10 degrees of freedom
## Multiple R-squared: 0.9999, Adjusted R-squared: 0.9999
## F-statistic: 7.412e+04 on 1 and 10 DF, p-value: < 2.2e-16
sigma(model)
## [1] 1.945628
summary(model)$r.square
## [1] 0.9998651
This shows our r square is 99.9% that means our model explains 99.9% of error
Lets make confidence interval and prediction interval
conf <- predict(model,data.frame(x=x),interval = "confidence")
pred <- predict(model,data.frame(x=x),interval = "prediction")
plot(x,y)
abline(model)
lines(x,conf[,2], col="red")
lines(x,conf[,3], col = "red")
lines(x,pred[,2],col ="blue")
lines(x,pred[,3],col="blue")

Here we can see overlapping as our regression model has 99.9 percent explains errors , hence they got overlap , We have a good model.
Think about how changing some of the observiations might affect the fitted line, those close to the average of the predictor varaibles (ie x-bar) and those far away
If we change obs of fitted line , it would affect line in adverse way , as we know close to x-bar the regression line (for different sample of same data) stays close . also this can be seen in confidence interval, it is a curve which comes close to the regression line near x-bar but goes away as we go away from x-bar