#Simple regression test > setwd("c:/Rdata") > regression=read.table("regression.txt") > attach(regression) > regression > X=V3 > Y=V7 > LM=lm(Y~X) > LM > Yhat=fitted(LM) > e=residuals(LM) > RESULTS=data.frame(X,Y,Yhat,e) > plot(X,Y) > abline(LM,col="green") > segments(X,Yhat,X,Y,col="blue") > anova(LM) > summary(LM) > cor.test(X,Y,method="pearson",alternative="two.sided",conf.level=.95) #Plotting regression > plot(X,Y) > abline(LM,col="blue") > segments(X,Yhat,X,Y,col="red") #Multiple Regression > setwd("c:/Rdata") > regression=read.table("regression.txt") > attach(regression) > regression > Y=V7 > X1=V3 > X2=V4 > X3=V5 > X4=V6 > LM=lm(Y~X1+X2+X3+X4) > LM > summary(LM) > anova(LM) #Choosing optimal model > Y=V7 > X1=V3 > X2=V4 > X3=V5 > X4=V6 > FM=lm(Y~X1+X2+X3+X4) > n=length(Y) > I=diag(n) > J=matrix(nrow=n,ncol=n,1) > X=model.matrix(FM) > p=ncol(X) > H=X%*%solve(t(X)%*%X)%*%t(X) > SSR=t(Y)%*%(H-((1/n)*J))%*%Y > SSE=t(Y)%*%(I-H)%*%Y > SSTO=t(Y)%*%(I-(1/n)*J)%*%Y > SSTO > rbind(SSR,SSE,SSTO) > AIC=n*log(SSE[1])-n*log(n)+2*p > AIC > extractAIC(FM) > drop1(FM) > require(car) > Anova(FM) #Automated “backwards” Stepwise Regression > step(FM,direction="backward")