1)
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag(): dplyr, stats
KimData <- read.csv("C:/Users/Tristan/Downloads/Clean-KimData.csv")
Functions)
panel.hist <- function(x, ...)
{
usr <- par("usr"); on.exit(par(usr))
par(usr = c(usr[1:2], 0, 1.5) )
h <- hist(x, plot = FALSE)
breaks <- h$breaks; nB <- length(breaks)
y <- h$counts; y <- y/max(y)
rect(breaks[-nB], 0, breaks[-1], y, col = "cyan", ...)
}
panel.cor <- function(x, y, digits = 2, prefix = "", cex.cor, ...)
{
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
r <- abs(cor(x, y, use="pairwise.complete.obs"))
txt <- format(c(r, 0.123456789), digits = digits)[1]
txt <- paste0(prefix, txt)
if(missing(cex.cor)) cex.cor <- 1/strwidth(txt)
text(0.5, 0.5, txt, cex = max(cex.cor * r,1.5))
}
2)
KimNumRaw <- KimData[c(1:3,5:7, 12, 13:15, 20:22)]
KimNum <- KimNumRaw %>%
replace_na(list(Cups.of.Water=0,Cups.of.Coffee=0)) %>%
mutate(Shoe.Size=as.numeric(sub(113, 11, Shoe.Size, fixed =TRUE))) %>%
mutate(Gender=as.factor(Gender))
KimNumRaw <- KimData[c(3:7, 11:13)]
pairs(KimNum[c(11:13, 3:7)], lower.panel = panel.smooth, upper.panel = panel.cor,
bg = "light blue", diag.panel = panel.hist, cex.labels = 1, font.labels = 2)

#2a. Yes they do.
#2b. It seems that 11:13 most closely correlate with Shoe size and Height.
#2c. Weight has the largest negative correlation, meaning weight doesn't have anything to do with politics.
3)
GenderColor <- c("darkseagreen3", "mediumpurple2", "firebrick")
KimNum %>%
gather(-Politically.Liberal, -Gender, key = "var", value = "value", na.rm=TRUE) %>%
ggplot(aes(y = value, x = Politically.Liberal, color = Gender)) +
geom_jitter() +
facet_wrap(~ var, scales = "free")+
stat_smooth(method="lm")+
scale_x_continuous(breaks= c(1,2,3,4,5,6,7,8,9,10,11,12,13))
## Warning: Removed 56 rows containing non-finite values (stat_smooth).
## Warning: Removed 56 rows containing missing values (geom_point).

GenderColor <- c("darkseagreen3", "mediumpurple2", "firebrick")
#3b. No, it does not seem fair to say that. It seems as though liberal women drank similar amounts of coffee and water as conservative women.
#3c. However, the plots do seem to show that politically liberal men are smaller than politically conservative men, using this data.
4)
R2 <- lm(Height ~ Gender, data = KimNum)
R2
##
## Call:
## lm(formula = Height ~ Gender, data = KimNum)
##
## Coefficients:
## (Intercept) GenderM Genderother
## 64.376 5.565 -2.376
summary(R2)
##
## Call:
## lm(formula = Height ~ Gender, data = KimNum)
##
## Residuals:
## Min 1Q Median 3Q Max
## -19.3759 -1.3759 0.4241 2.6241 10.6241
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 64.3759 0.2973 216.527 <2e-16 ***
## GenderM 5.5652 0.4496 12.379 <2e-16 ***
## Genderother -2.3759 4.2360 -0.561 0.575
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.226 on 357 degrees of freedom
## (17 observations deleted due to missingness)
## Multiple R-squared: 0.3021, Adjusted R-squared: 0.2982
## F-statistic: 77.26 on 2 and 357 DF, p-value: < 2.2e-16
R3 <- lm(Politically.Liberal ~ Semester, data = KimNum)
R3
##
## Call:
## lm(formula = Politically.Liberal ~ Semester, data = KimNum)
##
## Coefficients:
## (Intercept) Semester
## 0.34312 -0.06675
R4 <- lm(Politically.Liberal ~ Shoe.Size, data = KimNum)
R4
##
## Call:
## lm(formula = Politically.Liberal ~ Shoe.Size, data = KimNum)
##
## Coefficients:
## (Intercept) Shoe.Size
## 1.4358 -0.1379
R5 <- lm(Politically.Liberal ~ Socially.C.or.L, data = KimNum)
R5
##
## Call:
## lm(formula = Politically.Liberal ~ Socially.C.or.L, data = KimNum)
##
## Coefficients:
## (Intercept) Socially.C.or.L
## -0.1690 0.7208
R6 <- lm(Politically.Liberal ~ Gender, data = KimNum)
R6
##
## Call:
## lm(formula = Politically.Liberal ~ Gender, data = KimNum)
##
## Coefficients:
## (Intercept) GenderM Genderother
## 0.3095 -0.3473 0.6905
R3G <- lm(Politically.Liberal ~ Shoe.Size + Gender, data = KimNum)
R3G
##
## Call:
## lm(formula = Politically.Liberal ~ Shoe.Size + Gender, data = KimNum)
##
## Coefficients:
## (Intercept) Shoe.Size GenderM Genderother
## 1.54618 -0.15467 0.09999 0.53654
5)
#5a. It adds different variables which changes the numbers a bit.