Productivity <- scan(what=list(Lot.Size=0,Hours=0)) 80 399 30 121 50 221 90 376 70 361 60 224 120 546 80 352 100 353 50 157 40 160 70 252 90 389 20 113 110 435 100 420 30 212 50 268 90 377 110 421 30 273 90 468 40 244 80 342 70 323 Toluca <- data.frame(Productivity$Lot.Size,Productivity$Hours) names(Toluca)=c("Lot.Size","Hours") #Plot the data with(Toluca,plot(Lot.Size,Hours,xlab="Lot Size (parts/lot)",ylab="Work Hours")) #Fit a simple linear regression model Toluca.lm <- lm(Hours ~ Lot.Size,data=Toluca) #summaries of linear models object attributes(Toluca.lm) str(Toluca.lm) summary(Toluca.lm) #Add regression line using linear models object abline(Toluca.lm,col="red") #Page through diagnostic plots plot(Toluca.lm) #Prediction and confidence intervals for new data newdata = data.frame(Lot.Size=c(65,100)) predict(Toluca.lm, newdata, interval="predict",level=0.95) predict(Toluca.lm, newdata, interval="confidence") # .95 is the default newdata=data.frame(Lot.Size=seq(min(Toluca$Lot.Size),max(Toluca$Lot.Size))) predict(Toluca.lm,newdata) Toluca.conf <- predict(Toluca.lm, newdata, interval="confidence",level=0.90,type="response") Toluca.predict <- predict(Toluca.lm, newdata, interval="predict",level=0.90,type="response") with(Toluca,plot(Lot.Size,Hours,xlab="Lot Size (parts/lot)",ylab="Work Hours")) abline(Toluca.lm,col="black") lines(newdata[,1],Toluca.conf[,2],col="red",lty=2) lines(newdata[,1],Toluca.conf[,3],col="red",lty=2) lines(newdata[,1],Toluca.predict[,2],col="blue",lty=2) lines(newdata[,1],Toluca.predict[,3],col="blue",lty=2) #Confidence intervals for intercept and slope confint(Toluca.lm,level=0.90) #Pearson and Spearman correlations and confidence intervals #cor() lacks functionality cor(Toluca) with(Toluca,cor(Lot.Size,Hours)) cor(Toluca,method="spearman") with(Toluca,cor.test(Lot.Size,Hours)) #No CI for Spearman--use alternate package with(Toluca,cor.test(Lot.Size,Hours,method="spearman"))