#read in the data from a csv file dat1=read.table("Data Sets/Chapter 2/Examples/ex-2-1.csv", header=T, sep=",") dat1 #print out the dat y=dat1[,2] #assign the second column in dat1 to be variable y x=dat1[,3] #assign the third column in dat1 to be variable x #plot the data plot(y~x) fm1=lm(y~x) summary(fm1) anova(fm1) resid(fm1) df.residual(fm1) coef(fm1) #assign some notations ybar=mean(y) xbar=mean(x) ei=resid(fm1) yhat=fitted(fm1) df=df.residual(fm1) print(cbind(x,y,yhat, ei)) sum(ei^2) sum(ei^2)/df s2.hat=sum(ei^2)/df sum((y-ybar)^2) sum((yhat-ybar)^2) #plot the data and the regression line plot(y~x) lines(x, yhat, col=2) Sxy=sum( (y-mean(y)) * (x-mean(x)) ); Sxy Sxx=sum( (x-mean(x))^2 ); Sxx sqrt(s2.hat * (1/20+mean(x)^2/Sxx)) sqrt(s2.hat/Sxx) confint(fm1) #install.packages("DAAG") #library(DAAG)