#Biostatistics 360 #SIMPLE LINEAR REGRESSION #ZAR EXAMPLE 17.1 ZAR=read.table("ZarEX17.1R.txt") ZAR attach(ZAR) X=ageX Y=winglY #CREATING LINEAR MODEL LM: LM=lm(Y~X) Yhat=fitted(LM) e=residuals(LM) RESULTS=data.frame(X,Y,Yhat,e) RESULTS #ANOVA F-TEST FOR SLOPE: anova(LM) #t-TESTS FOR SLOPE & INTERCEPT: summary(LM) #CONFIDENCE INTERVAL FOR SLOPE: #THIS IS CALCULATED BY HAND! n=length(Y) Lxx=sum((X-mean(X))^2) Lxx Lxy=sum((X-mean(X))*(Y-mean(Y))) Lxy b=Lxy/Lxx b MSE=summary(LM)$sigma^2 MSE alpha=0.05 C=abs(qt(1-alpha/2,n-2)) C stderr=sqrt(MSE/Lxx) CIL=b-C*stderr CIL CIU=b+C*stderr CIU #CONFIDENCE INTERVALS FOR #REGRESSION PARAMETERS #THE EASIER WAY: confint(lm(Y~X),level=0.95) #CONFIDENCE INTERVALS FOR Yhat: CONF=predict(lm(Y~X),interval="confidence",level=0.95) CN=data.frame(CONF) CN #PREDICTION INTERVALS FOR Xn: PRED=predict(lm(Y~X),interval="prediction",level=0.95) PR=data.frame(PRED) PR #PLOTTING INTERVALS IN R: plot(X,Y) abline(lm(Y~X),col="blue") segments(X,PR$lwr,X,PR$upr,col="red") segments(X,CN$lwr,X,CN$upr,col="green") points(X,CN$lwr,col="green") points(X,CN$upr,col="green") points(X,PR$lwr,col="red") points(X,PR$upr,col="red") points(X,PR$fit,col="blue")