############################################### ## Author: Joshua M. Tebbs ## Date: 22 October 2026 ## Update: 4 November 2026 ## STAT 515 course notes: R Code Chapter 11 ############################################### # Use the following command as necessary par(mar=c(4,4.5,4,3.5)) # adjust plotting region to avoid cutting off vertical axis label # Install and load "car" R package # For normal qq plots # See https://cran.r-project.org/web/packages/car/car.pdf for documentation # Figure 11.1 # Page 183 time = c(1218,1105,1191,1262,1268,1207,1070,1043,1345,1110,1227,1113,1158,1160,1060,1047, 1043,1103,983,1144,1055,1000,1048,1075) max.O2 = c(27.33,38.10,27.08,35.06,27.45,27.46,32.82,34.92,21.23,34.66,26.49, 31.17,31.18,28.21,36.81,38.28,38.29,32.18,41.91,32.80,33.65,38.67,45.62,41.76) plot(time,max.O2,xlab="Time (in seconds)",ylab="Maximum oxygen uptake (in mL/kg/min)", xlim=c(900,1400),ylim=c(20,50),pch=16) # Figure 11.2 # Page 186 fit = lm(max.O2 ~ time) fit plot(time,max.O2,xlab="Time (in seconds)",ylab="Maximum oxygen uptake (in mL/kg/min)", xlim=c(900,1400),ylim=c(20,50),pch=16) # Superimpose least-squares line abline(fit,lwd=2,col="red") # Figure 11.3 # Page 188 fit = lm(max.O2 ~ time) plot(time,max.O2,xlab="Time (in seconds)",ylab="Maximum oxygen uptake (in mL/kg/min)", xlim=c(1200,1300),ylim=c(24,36),pch=16) abline(fit,col="red",lwd=2) # Add vertical distance lines lines(c(1218,1218),c(27.33,fit$coefficients[1] + fit$coefficients[2]*1218),col="blue",lwd=2) lines(c(1262,1262),c(35.06,fit$coefficients[1] + fit$coefficients[2]*1262),col="blue",lwd=2) lines(c(1268,1268),c(27.45,fit$coefficients[1] + fit$coefficients[2]*1268),col="blue",lwd=2) lines(c(1207,1207),c(27.46,fit$coefficients[1] + fit$coefficients[2]*1207),col="blue",lwd=2) lines(c(1227,1227),c(26.49,fit$coefficients[1] + fit$coefficients[2]*1227),col="blue",lwd=2) # Calculate fitted values and residuals # Page 189 residuals(fit) predict(fit) # Figure 11.4 # Page 191 x = seq(-5,5,0.001) pdf = dt(x,10) plot(x,pdf,type="l",lty=1,xlab="",xaxt="n",yaxt="n",bty="n",ylab="",ylim=c(0,0.4)) abline(h=0) x = seq(-5,qt(0.025,10),0.001) y = dt(x,10) polygon(c(-5,x,qt(0.025,10)),c(0,y,0),col="lightblue") points(x=qt(0.025,10),y=0,pch=19,cex=1) x = seq(qt(0.975,10),5,0.001) y = dt(x,10) polygon(c(qt(0.975,10),x,5),c(0,y,0),col="lightblue") points(x=qt(0.975,10),y=0,pch=19,cex=1) text(-0.025,0.1,expression(1-alpha),cex=1.25) text(-3.5,0.05,expression(alpha/2),cex=1.25) text(3.5,0.05,expression(alpha/2),cex=1.25) text(1.5,0.3,"t(n-2)",cex=1.25) # Figure 11.5 # Page 193 # Top--this is the same as Figure 11.4. # Bottom left x = seq(-5,5,0.001) pdf = dt(x,10) plot(x,pdf,type="l",lty=1,xlab="",xaxt="n",yaxt="n",bty="n",ylab="",ylim=c(0,0.4)) abline(h=0) x = seq(-5,qt(0.05,10),0.001) y = dt(x,10) polygon(c(-5,x,qt(0.05,10)),c(0,y,0),col="lightblue") points(x=qt(0.05,10),y=0,pch=19,cex=1) text(-0.025,0.1,expression(1-alpha),cex=1.25) text(-3,0.05,expression(alpha),cex=1.25) text(1.5,0.3,"t(n-2)",cex=1.25) # Bottom right x = seq(-5,5,0.001) pdf = dt(x,10) plot(x,pdf,type="l",lty=1,xlab="",xaxt="n",yaxt="n",bty="n",ylab="",ylim=c(0,0.4)) abline(h=0) x = seq(qt(0.95,10),5,0.001) y = dt(x,10) polygon(c(qt(0.95,10),x,5),c(0,y,0),col="lightblue") points(x=qt(0.95,10),y=0,pch=19,cex=1) text(-0.025,0.1,expression(1-alpha),cex=1.25) text(3,0.05,expression(alpha),cex=1.25) text(1.5,0.3,"t(n-2)",cex=1.25) # Figure 11.6 # Page 195 x = seq(-7,7,0.001) pdf = dt(x,22) plot(x,pdf,type="l",lty=1,xlab="",xaxt="n",yaxt="n",bty="n",ylab="",ylim=c(0,0.4)) axis(side = 1, at = c(-4, -2, 0, 2, 4),pos=0) abline(h=0) text(1.75,0.3,expression(t(22)),cex=1.25) points(x=-6.52,y=0,pch=19,cex=1) # Figure 11.7 # Page 196 fit = lm(max.O2 ~ time) plot(time,max.O2,xlab="Time (in seconds)",ylab="Maximum oxygen uptake (in mL/kg/min)", xlim=c(900,1400),ylim=c(20,50),pch=16) abline(fit,lwd=2,col="red") abline(v=1200,lty=2) # Figure 11.8 # Page 198 fit = lm(max.O2 ~ time) x.0 = seq(900,1400,1) conf = predict(fit,data.frame(time=x.0),level=0.95,interval="confidence") pred = predict(fit,data.frame(time=x.0),level=0.95,interval="prediction") plot(time,max.O2,xlab="Time (in seconds)",ylab="Maximum oxygen uptake (in mL/kg/min)", xlim=c(900,1400),ylim=c(min(pred[,2]),max(pred[,3])),pch=16) abline(fit,lwd=2,col="red") # Add bands for confidence intervals and prediction intervals lines(x.0,conf[,2],type="l",lty=2) lines(x.0,conf[,3],type="l",lty=2) lines(x.0,pred[,2],type="l",lty=4) lines(x.0,pred[,3],type="l",lty=4) # Add legend legend(1200,50,lty = c(2,4), c( expression(paste("95% confidence")), expression(paste("95% prediction")) )) # Figure 11.9 # Page 200 fit = lm(max.O2 ~ time) residuals = residuals(fit) library(car) qqPlot(residuals,distribution="norm",mean=mean(residuals),sd=sd(residuals), xlab="Normal quantiles",ylab="Residuals",pch=16, envelope=list(border=TRUE,style="lines"),id=FALSE) # Figure 11.10 # Page 202 fit = lm(max.O2 ~ time) plot(fitted(fit),residuals(fit),pch=16,xlab="Fitted values",ylab="Residuals") abline(h=0,lty=2) # Example 11.2 # Page 202-206 # Electricity demand data # data file housed on my desktop # read.table imports the data into R electricity = read.table( "C:\\Users\\tebbs\\OneDrive - University of South Carolina\\Desktop\\STAT515_data\\electricity_demand.txt",header=TRUE) # Define variables monthly.usage = electricity[,1] peak.demand = electricity[,2] # Figure 11.11 # Page 203 fit = lm(peak.demand ~ monthly.usage) fit # Left (Scatterplot with least-squares fit) plot(monthly.usage,peak.demand,xlab="Monthly usage (in kWh)",ylab ="Peak demand (in kWh)", xlim=c(0,max(monthly.usage)),pch=16) abline(fit,lwd=2,col="red") # Right (residual plot) plot(fitted(fit),residuals(fit),pch=16,xlab="Fitted values",ylab="Residuals",ylim=c(-4,4)) abline(h=0,lty=2) # Transformed analysis sqrt.peak.demand = sqrt(peak.demand) # transform the response variable fit.2 = lm(sqrt.peak.demand ~ monthly.usage) fit.2 # Figure 11.12 # Page 205 # Left (Scatterplot with least-squares fit) plot(monthly.usage,sqrt.peak.demand,xlab="Monthly usage (in kWh)",ylab=expression("Peak demand (in"~sqrt(kWh)~")"), xlim=c(0,max(monthly.usage)),pch=16) abline(fit.2,lwd=2,col="red") # Right (residual plot) plot(fitted(fit.2),residuals(fit.2),pch=16,xlab="Fitted values",ylab="Residuals",ylim=c(-1.5,1)) abline(h=0,lty=2) summary(fit.2) # hypothesis tests confint(fit.2) # confidence intervals # Figure 11.13 # Page 206 fit.2 = lm(sqrt.peak.demand ~ monthly.usage) resid = resid(fit.2) library(car) qqPlot(resid,distribution="norm",mean=mean(resid),sd=sd(resid), xlab="Normal quantiles",ylab="Residuals",pch=16, envelope=list(border=TRUE,style="lines"),id=FALSE) # Example 11.3 # Page 207-209 # Grasshopper data no.eggs = c(27,32,39,48,59,67,71,65,73,67,78,72,81,74,83,75,84,77,83,76,82,75,78,77,75,73,71,70,68,65) weight = c(2.1,2.3,2.4,2.5,2.9,3.1,3.2,3.3,3.4,3.4,3.5,3.5,3.5,3.6,3.6,3.6,3.6,3.7,3.7,3.7,3.8,3.9,4.0,4.3,4.4,4.7,4.8,4.9,5.0,5.1) fit = lm(no.eggs ~ weight) fit # Figure 11.14 # Left (Scatterplot with least-squares fit) plot(weight,no.eggs,xlab="Weight (in grams)",ylab="Number of eggs",pch=16) abline(fit,lwd=2,col="red") # Right (residual plot) plot(fitted(fit),residuals(fit),pch=16,xlab="Fitted values",ylab="Residuals") abline(h=0,lty=2) # Quadratic regression weight.sq = weight^2 fit.2 = lm(no.eggs ~ weight + weight.sq) fit.2 # Figure 11.15 # Page 209 # Left (Scatterplot with least-squares fit) plot(weight,no.eggs,xlab="Weight (in grams)",ylab="Number of eggs",pch=16) curve(expr = fit.2$coefficients[1] + fit.2$coefficients[2]*x + fit.2$coefficients[3]*x^2, col="red",lwd=2,add=TRUE) # Right (residual plot) plot(fitted(fit.2),residuals(fit.2),pch=16,xlab="Fitted values",ylab="Residuals") abline(h=0,lty=2) # Figure 11.16 # Page 211 # Each subfigure is created separately using the code below rho = 0.99 # correlation (change this to produce different figures) library(MASS) # must type this first Sigma = matrix(c(1,rho,rho,1),2,2) data = mvrnorm(n = 200, c(30,50), Sigma) plot(data[,1],data[,2],xlab="x",ylab="y",pch=16,cex.lab=1.25,xaxt="n",yaxt="n") # Example 11.4 # Page 212-214 factors = c(231,323,113,208,162,117,159,169,55,77,103,147,230,78,525,121,248,233,260,224,472,220,383,301,262, 354,142,286,341,201,158,243,156,184,115,202,206,360,84,331,302,60,110,131,364,180,134,401,155,338) length = c(9,7,8,5,4,4,6,9,6,3,4,6,6,3,9,7,5,8,4,7,12,8,6,9,7,11,7,9,10,5,11,6,6,7,4,6,5,6,3,9,7,2,2,5,4,7,6,15,4,8) # Figure 11.17 # Page 213 # Left (Scatterplot) plot(factors,length,xlab="Number of factors",ylab="Length of stay (in days)",pch=16,xlim=c(0,max(factors)),ylim=c(0,15)) # Right (rejection region) x = seq(-4,4,0.001) pdf = dt(x,48) plot(x,pdf,type="l",lty=1,xlab="",xaxt="n",yaxt="n",bty="n",ylab="",ylim=c(0,0.4)) abline(h=0) x = seq(qt(0.95,48),4,0.001) y = dt(x,48) polygon(c(qt(0.95,48),x,4),c(0,y,0),col="lightblue") points(x=qt(0.95,48),y=0,pch=19,cex=1) text(-0.025,0.1,"0.95",cex=1.25) text(3.2,0.04,"0.05",cex=1.25) text(1.5,0.3,"t(48)",cex=1.25) text(qt(0.95,48),-0.011,1.677,cex=1.1) # Internal R function cor.test(factors,length,alternative="greater",conf.level=0.95) # Example 11.5 # Page 215-216 # STAT 110 exam data # data file housed on my desktop # read.table imports the data into R scores = read.table( "C:\\Users\\tebbs\\OneDrive - University of South Carolina\\Desktop\\STAT515_data\\examscores.txt",header=TRUE) # Create variables exam.1 = scores[,1] exam.2 = scores[,2] exam.3 = scores[,3] final = scores[,4] # Create average midterm score midterm = (exam.1+exam.2+exam.3)/3 # Figure 11.18 # Page 216 plot(midterm,final,xlab="Average of three midterm scores",ylab="Final exam score",pch=16) # Population correlation confidence interval function # Page 216 rho.ci = function(r,n,conf.level=0.95){ W.r = 0.5*log((1+r)/(1-r)) W.L = W.r - qnorm((1+conf.level)/2,0,1)/sqrt(n-3) W.U = W.r + qnorm((1+conf.level)/2,0,1)/sqrt(n-3) c(tanh(W.L),tanh(W.U)) } rho.ci(0.841,48,conf.level=0.95)