Entering the data as vectors
x<-c(10, 35, 19, 16, 40, 32, 14)
y<-c(10, 35, 14, 22, 41, 25, 17)
n<-length(y)
Creating a data frame from the two vectors and view top records:
d<-data.frame(x,y)
head(d)
Calculating the Average of Y
meany<-mean(y)
print(meany)
[1] 23.42857
Producing the Plot
plot(d,pch=20,cex=3,cex.axis=1.5,cex.lab=1.5)
Adding the Mean of Y:
plot(d,pch=20,cex=3,cex.axis=1.5,cex.lab=1.5)
lines(c(0,50),c(meany,meany),col="black",lwd=3)
Adding the Residuals:
plot(d,pch=20,cex=3,cex.axis=1.5,cex.lab=1.5)
mlines<-function(){ for(i in 1:n)
lines(c(d$x[i],d$x[i]),c(d$y[i],meany),col="red",lwd=3)
}
mlines()
lines(c(0,50),c(meany,meany),col="black",lwd=3)
Produce the Linear equation:
model_y<-lm(y~x)
Show results:
summary(model_y)
Call:
lm(formula = y ~ x)
Residuals:
1 2 3 4 5 6 7
-1.269 1.565 -5.249 5.411 3.132 -5.775 2.184
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.4026 4.2352 0.567 0.5950
x 0.8866 0.1624 5.460 0.0028 **
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 4.665 on 5 degrees of freedom
Multiple R-squared: 0.8564, Adjusted R-squared: 0.8277
F-statistic: 29.81 on 1 and 5 DF, p-value: 0.002803
Use model to make prediction
yhat<-predict(model_y)
Redraw Plot and add Linear Regression Line
plot(d,pch=20,cex=3,cex.axis=1.5,cex.lab=1.5)
abline(model_y,col="blue",lwd=3)
Add Residual lines to plot
plot(d,pch=20,cex=3,cex.axis=1.5,cex.lab=1.5)
abline(model_y,col="blue",lwd=3)
plines<-function(){ for(i in 1:length(yhat))
lines(c(d$x[i],d$x[i]),c(d$y[i],yhat[i]),col="red",lwd=3)
}
plines()