# August 2025

# URL of the CSV file
url <- 'https://gattonweb.uky.edu/faculty/sheather/book/docs/datasets/production.txt'

# Read the text file into a DataFrame
production <- read.table(url, header = TRUE, sep = "\t")

attach(production)

#Figure 2.1 on page 16
par(mfrow=c(1,1))
plot(production$RunSize,production$RunTime,xlab="Run Size", ylab="Run Time")

#R output on page 19
m1 <- lm(RunTime~RunSize)
summary(m1)

#Figure 2.3 on page 20
plot(production$RunSize,production$RunTime,xlab="Run Size", ylab="Run Time")
abline(lsfit(production$RunSize,production$RunTime))

#t-value on page 23
tval <- qt(1-0.05/2,18)
tval

#95% confidence intervals on page 24
round(confint(m1,level=0.95),3)

#R output on page 27
predict(m1,newdata=data.frame(RunSize=c(50,100,150,200,250,300,350)),interval="confidence",level=0.95)
predict(m1,newdata=data.frame(RunSize=c(50,100,150,200,250,300,350)),interval="prediction",level=0.95)

#R output on page 30
anova(m1)
detach(production)


# URL of the CSV file
url <- 'https://gattonweb.uky.edu/faculty/sheather/book/docs/datasets/changeover_times.txt'

# Read the CSV file into a DataFrame
changeover_times <- read.table(url, header = TRUE, sep = "\t")


attach(changeover_times)

#R output on page 31
m1 <- lm(Changeover~New)
summary(m1)

#Figure 2.5 on page 32
par(mfrow=c(2,2))
plot(New,Changeover,xlab="Dummy variable, New",ylab="Change Over Time")
abline(lsfit(New,Changeover))
boxplot(Changeover~New,xlab="Dummy variable, New",ylab="Change Over Time")
boxplot(Changeover~Method,ylab="Change Over Time",xlab="Method")

#t-value on page 33
tval <- qt(1-0.05/2,118)
tval

detach(changeover_times)

#################EXERCISES

#Exercise 2.8.1
# URL of the CSV file
url <- 'https://gattonweb.uky.edu/faculty/sheather/book/docs/datasets/playbill.csv'

# Read the CSV file into a DataFrame
playbill <- read.csv(url)
attach(playbill)

#Figure 2.6 on page 38
par(mfrow=c(1,1))
plot(LastWeek,CurrentWeek,xlab="Gross box office results previous week",
ylab="Gross box office results current week")

detach(playbill)

#Exercise 2.8.3

# URL of the CSV file
url <- 'https://gattonweb.uky.edu/faculty/sheather/book/docs/datasets/invoices.txt'

# Read the CSV file into a DataFrame
invoice <- read.csv(url, sep="\t", header=TRUE)
attach(invoice)

#Figure 2.7 on page 40
par(mfrow=c(1,1))
plot(Invoices,Time,xlab="Number of Invoices",ylab="Processing Time")
abline(lsfit(Invoices,Time))

#Output from R on page 40
m1 <- lm(Time~Invoices)
summary(m1)
mean(Time)
median(Time)
mean(Invoices)
median(Invoices)

detach(invoice)

#Exercise 2.8.5
# URL of the CSV file
url <- 'https://gattonweb.uky.edu/faculty/sheather/book/docs/datasets/Ch2Problem5.txt'

# Read the CSV file into a DataFrame
problem5 <- read.csv(url, sep="\t", header=TRUE)
attach(problem5)

#Figure 2.8 on page 42
par(mfrow=c(1,2))
plot(x1,y,main="Model 1")
abline(lsfit(x1,y))
plot(x2,y,main="Model 2")
abline(lsfit(x2,y))

detach(problem5)


