# August 2025

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

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

# Compute correlation on page 2
cor_val <- round(cor(FGtM1, FGt, use = "complete.obs"), 3)
cor_val 

#Figure 1.1 on page 2
# Combine the title text and correlation value properly into one string
title_text <- paste("Unadjusted Correlation =", cor_val)

# Plot
plot(FGtM1, FGt,
     main = title_text,
     xlab = "Field Goal Percentage in Year t-1",
     ylab = "Field Goal Percentage in Year t")

#p-value on page 2
fit.0 <- lm(FGt~FGtM1,data=kicker)
anova(fit.0)

#p-values on page 3
fit.1 <- lm(FGt~FGtM1 +Name +FGtM1:Name,data=kicker)
anova(fit.1)

#slope and interecepts of lines in Figure 1.2 on page 3
fit.2 <- lm(FGt ~ Name + FGtM1,data=kicker)
fit.2

#Figure 1.2 on page 3
plot(kicker$FGtM1,kicker$FGt,
main="Slope of each line = -0.504",
xlab="Field Goal Percentage in Year t-1",
ylab="Field Goal Percentage in Year t")
tt <- seq(60,100,length=1001)
slope.piece <- summary(fit.2)$coef[20]*tt
lines(tt,summary(fit.2)$coef[1]+slope.piece,lty=2)
for (i in 2:19)
{lines(tt,summary(fit.2)$coef[1]+summary(fit.2)$coef[i]+slope.piece,lty=2)}

detach(kicker)

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

# Read the file into a DataFrame using tab as separator
circulation <- read.table(url, header = TRUE, sep = "\t")

attach(circulation)

#Figure 1.3 on page 5
plot(Weekday, Sunday,
     xlab = "Weekday Circulation",
     ylab = "Sunday Circulation",
     pch = Tabloid.with.a.Serious.Competitor + 1,
     col = Tabloid.with.a.Serious.Competitor + 1)

legend(110000, 1600000,
       legend = c("0", "1"),
       pch = 1:2,
       col = 1:2,
       title = "Tabloid dummy variable")

#Figure 1.4 on page 5
plot(log(circulation$Weekday), log(circulation$Sunday),
     xlab = "log(Weekday Circulation)",
     ylab = "log(Sunday Circulation)",
     pch = circulation$Tabloid.with.a.Serious.Competitor + 1,
     col = circulation$Tabloid.with.a.Serious.Competitor + 1)

legend(11.6, 14.1,
       legend = c("0", "1"),
       pch = 1:2,
       col = 1:2,
       title = "Tabloid dummy variable")

detach(circulation)

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

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

attach(nyc)

#Figure 1.5 on page 7Price
pairs(Price~Food+Decor+Service,data=nyc,gap=0.4,
cex.labels=1.5)

#Figure 1.6 on page 7
boxplot(Price~East,ylab="Price",
xlab="East (1 = East of Fifth Avenue)")

detach(nyc)

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

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

#Figure 1.7 on page 10
pairs(Price~ParkerPoints+CoatesPoints,data=Bordeaux,gap=0.4,cex.labels=1.5)

#Figure 1.8 on page 11
par(mfrow=c(2,3))
boxplot(Price~P95andAbove,ylab="Price",xlab="P95andAbove")
boxplot(Price~FirstGrowth,ylab="Price",xlab="First Growth")
boxplot(Price~CultWine,ylab="Price",xlab="Cult Wine")
boxplot(Price~Pomerol,ylab="Price",xlab="Pomerol")
boxplot(Price~VintageSuperstar,ylab="Price",xlab="Vintage Superstar")

#Figure 1.9 on page 12
par(mfrow=c(1,1))
pairs(log(Price)~log(ParkerPoints)+log(CoatesPoints),data=Bordeaux,gap=0.4,cex.labels=1.5)

#Figure 1.10 on page 13
par(mfrow=c(2,3))
boxplot(log(Price)~P95andAbove,ylab="log(Price)",
xlab="P95andAbove")
boxplot(log(Price)~FirstGrowth,ylab="log(Price)",
xlab="First Growth")
boxplot(log(Price)~CultWine,ylab="log(Price)",
xlab="Cult Wine")
boxplot(log(Price)~Pomerol,ylab="log(Price)",
xlab="Pomerol")
boxplot(log(Price)~VintageSuperstar,ylab="log(Price)",
xlab="Vintage Superstar")

detach(Bordeaux)
