#R SCRIPT FOR 2008 LINEAR MODELS 02 #READ TABLE AND ASSIGN VARIABLES FROM COLUMNS: K=read.table("c:/2008LinearModelsData/Toluca.txt") attach(K) X=V1 Y=V2 summary(lm(Y~X)) anova(lm(Y~X)) confint(lm(Y~X),level=0.90) confint(lm(Y~X),level=0.95) predict.lm(lm(Y~X),interval="confidence",level=0.90) predict.lm(lm(Y~X),interval="prediction",level=0.90) cor.test(X,Y, method="pearson",alternative="two.sided",conf.level=0.95) #COLLECTING INFORMATION FROM THE REGRESSION: Yh=predict(lm(Y~X)) RESID=resid(lm(Y~X)) CONF=data.frame(predict.lm(lm(Y~X),interval="confidence",level=0.90)) PRED= data.frame(predict.lm(lm(Y~X),interval="prediction",level=0.90)) #CALCULATING WORKING-HOTELLING INTERVAL ALSO: #FIRST ONE SPECIFIES CONFIDENCE LEVEL ALPHA: alpha=0.10 #FINDING n: n=length(X) #USING qt() FUNCTION TO CALCULATE CRITICAL VALUE C : C=qt((1-(alpha/2)),(n-2)) C #USING qf() FUNCTION TO CALCULATE WI CRITICAL VALUE W: W=sqrt(2*qf(1-alpha,2,(n-2))) W #CALCULATING WORKING-HOTELLING INTERVALS: WH.lwr=Yh-((W/C)*(Yh-CONF$lwr)) WH.upr=Yh+((W/C)*(Yh-CONF$lwr)) WORHOT=data.frame(WH.lwr,WH.upr) WORHOT # REPORTING WITHIN A DATAFRAME CALLED "RESULTS": RESULTS=data.frame(X,Y,Yh,RESID,CONF$lwr,CONF$upr,PRED$lwr,PRED$upr,WH.lwr,WH.upr) RESULTS #PLOTTING DATA, REGRESSION LINE, AND INTERVALS: plot(X,Y,pch=19,col="black") points(X,Yh,pch=19,col="blue") abline(lm(Y~X),col="blue") #segments(X,predict(lm(Y~X)),X,Y,col="red") points(X,CONF$lwr, pch=24,col="green") points(X,CONF$upr, pch=25,col="green") points(X,PRED$lwr, pch=24,col="magenta") points(X,PRED$upr, pch=25,col="magenta") points(X,WH.lwr, pch=24,col="brown") points(X,WH.upr, pch=25,col="brown")