library(rio)
anes <- import("https://github.com/marctrussler/IIS-Data/raw/main/ANESFinalProjectData.csv")

# Recreate the ideology / vote / race variables used in the correlation
# discussion this chapter refers back to.
anes$vote.centrist.dem[anes$V201021 %in% c(1,2,3,4)] <- 1
anes$vote.centrist.dem[anes$V201021 %in% c(5,6)]   <- 0
anes$ideology <- anes$V201200
anes$ideology[anes$ideology %in% c(-9,-8,99)] <- NA
anes$white.v.black[anes$V201549x == 1] <- 1
anes$white.v.black[anes$V201549x == 2] <- 0

summary(anes$V201507x)
anes$age <- anes$V201507x
anes$age[anes$age<0] <- NA
summary(anes$age)

summary(anes$V202174)
anes$blm.therm <- anes$V202174
anes$blm.therm[anes$blm.therm<0 | anes$blm.therm>100] <- NA
summary(anes$blm.therm)

#Reduce to only where we have non NAs for those two variables
#this is not strictly necessary but helps with our "by hand" calculations
#down below
anes <- anes[!is.na(anes$age),]
anes <- anes[!is.na(anes$blm.therm),]



plot(anes$age, anes$blm.therm, xlab="Age", ylab="BLM FT")

cor(anes$age, anes$blm.therm)

ages <- sort(unique(anes$age))
mean.blm <- rep(NA, length(ages))
for(i in 1:length(ages)){
  mean.blm[i] <- mean(anes$blm.therm[anes$age==ages[i]])
}


plot(anes$age, anes$blm.therm, xlab="Age", ylab="BLM FT")
points(ages, mean.blm, type="b", col="firebrick", pch=16, lwd=2)


lm(anes$blm.therm ~ anes$age)

age <- seq(18,80,1)
regression.line <- 67.55 - .27*age
plot(anes$age, anes$blm.therm, xlab="Age", ylab="BLM FT")
points(age, regression.line, type="l", col="firebrick", pch=16, lwd=2)

anes[1,c("age","blm.therm")]

alpha <- 67.55
beta <- -.27
sum.sq <- sum((anes$blm.therm - alpha - beta*anes$age)^2)
sum.sq

alpha <- seq(0,100,1)
beta <- -.27

for(i in 1:length(alpha)){
sum.sq[i] <- sum((anes$blm.therm - alpha[i] - beta*anes$age)^2)
}

plot(alpha, sum.sq, type="l")
abline(v=67.55, lty=2, col="firebrick")

alpha <- 67.55
beta <- seq(-1,1,.01)

for(i in 1:length(beta)){
sum.sq[i] <- sum((anes$blm.therm - alpha - beta[i]*anes$age)^2)
}

plot(beta, sum.sq, type="l")
abline(v=-.27, lty=2, col="firebrick")

mean(anes$blm.therm) - (-.27 *mean(anes$age))

 sum((anes$blm.therm - mean(anes$blm.therm)) * (anes$age - mean(anes$age)))/
  sum((anes$age - mean(anes$age))^2)
#Yes

lm(anes$blm.therm ~ anes$age)
cov(anes$blm.therm, anes$age)/var(anes$age)

plot(anes$age, anes$blm.therm, xlab="Age", ylab="BLM FT")
abline(lm(anes$blm.therm ~ anes$age), col="firebrick", lwd=2)

m <- lm(anes$blm.therm ~ anes$age)
#Can also do
m <- lm(blm.therm ~ age, data=anes)
summary(m)

residuals <- anes$blm.therm - (m$coefficients["(Intercept)"] + m$coefficients["age"]*anes$age)
summary(residuals)

anes$age.months <- anes$age*12


plot(anes$age.months, anes$blm.therm, xlab="Age", ylab="BLM FT")
abline(lm(anes$blm.therm ~ anes$age.months), col="firebrick", lwd=2)

m2 <- lm(blm.therm ~ age.months, data=anes)
summary(m2)


m2$coefficients["age.months"]*12

attributes(anes$V201200)
anes$ideology <- anes$V201200
anes$ideology[anes$ideology %in% c(-9,-8,99)] <- NA
table(anes$ideology)

anes$liberal[anes$ideology<4] <- 1
anes$liberal[anes$ideology>=4] <- 0
table(anes$liberal, anes$ideology)

plot(anes$liberal, anes$blm.therm)

m3 <- lm(blm.therm ~ liberal, data=anes)
summary(m3)

t.test(anes$blm.therm ~ anes$liberal)

table(anes$ideology)
plot(anes$ideology, anes$blm.therm)
m4 <- lm(blm.therm ~ ideology, data=anes)

ideology <- 1:7
yhat <- m4$coefficients["(Intercept)"] + m4$coefficients["ideology"]*ideology
yhat
plot(anes$ideology, anes$blm.therm)
points(ideology, yhat, col="firebrick", type="b", pch=16)

ybar <- NA
for(i in 1:7){
  ybar[i] <- mean(anes$blm.therm[anes$ideology==i],na.rm=T)
}
plot(anes$ideology, anes$blm.therm)
points(ideology, yhat, col="firebrick", type="b", pch=16)
points(ideology, ybar, col="dodgerblue", type="b", pch=16)



sm.az <- import("https://github.com/marctrussler/IIS-Data/raw/main/AZFinalWeeks.csv")

plot(sm.az$age, sm.az$weight, xlab="Age", ylab="Weight")

m <- lm(weight ~ age, data=sm.az)
summary(m)

head(sm.az$race)

sm.az$white <- sm.az$race=="white"
table(sm.az$white)

m2 <- lm(weight ~ white, data=sm.az)
summary(m2)

table(sm.az$biden.approval)

sm.az$biden.approval.num <- NA
sm.az$biden.approval.num[sm.az$biden.approval=="Strongly disapprove"] <- 0
sm.az$biden.approval.num[sm.az$biden.approval=="Somewhat disapprove"] <- 1
sm.az$biden.approval.num[sm.az$biden.approval=="Somewhat approve"] <- 2
sm.az$biden.approval.num[sm.az$biden.approval=="Strongly approve"] <- 3
table(sm.az$biden.approval.num)

m3 <- lm(weight~biden.approval.num, data=sm.az)
summary(m3)

sm.az$vote.kelly <- NA
sm.az$vote.kelly[sm.az$senate.topline=="Democrat"] <- 1
sm.az$vote.kelly[sm.az$senate.topline=="Republican"] <- 0
table(sm.az$vote.kelly, sm.az$senate.topline)

plot(sm.az$age, sm.az$vote.kelly)

plot(sm.az$age, sm.az$vote.kelly)
abline(lm(vote.kelly~age, data=sm.az), col="firebrick")

m4 <- lm(vote.kelly~age, data=sm.az)
summary(m4)

m5 <- lm(vote.kelly ~ biden.approval.num, data=sm.az)
summary(m5)

plot(sm.az$biden.approval.num, sm.az$vote.kelly)
abline(m5, col="firebrick")

plot(jitter(sm.az$biden.approval.num), jitter(sm.az$vote.kelly))
abline(m5, col="firebrick")

m5$coefficients["(Intercept)"] + m5$coefficients["biden.approval.num"]*0:3

sst <- sum((anes$blm.therm - mean(anes$blm.therm,na.rm=T))^2)
anes$yhat <- coefficients(m)["(Intercept)"] + coefficients(m)["age"]*anes$age
ssr <- sum((anes$yhat - mean(anes$blm.therm,na.rm=T))^2)
r.squared <- ssr/sst
summary(m)
