library(tidyverse)
Group 4 (USArrests) Load the ‘USArrests’ data set using this code:
{data(USArrests)}
Describe and summarize your assigned data set. Which states have the highest murder and assault rates? Is there a relationship between urban population percentage and crime rates? Graph your data.
data(USArrests)
head(USArrests)
## Murder Assault UrbanPop Rape
## Alabama 13.2 236 58 21.2
## Alaska 10.0 263 48 44.5
## Arizona 8.1 294 80 31.0
## Arkansas 8.8 190 50 19.5
## California 9.0 276 91 40.6
## Colorado 7.9 204 78 38.7
str(USArrests)
## 'data.frame': 50 obs. of 4 variables:
## $ Murder : num 13.2 10 8.1 8.8 9 7.9 3.3 5.9 15.4 17.4 ...
## $ Assault : int 236 263 294 190 276 204 110 238 335 211 ...
## $ UrbanPop: int 58 48 80 50 91 78 77 72 80 60 ...
## $ Rape : num 21.2 44.5 31 19.5 40.6 38.7 11.1 15.8 31.9 25.8 ...
summary(USArrests)
## Murder Assault UrbanPop Rape
## Min. : 0.800 Min. : 45.0 Min. :32.00 Min. : 7.30
## 1st Qu.: 4.075 1st Qu.:109.0 1st Qu.:54.50 1st Qu.:15.07
## Median : 7.250 Median :159.0 Median :66.00 Median :20.10
## Mean : 7.788 Mean :170.8 Mean :65.54 Mean :21.23
## 3rd Qu.:11.250 3rd Qu.:249.0 3rd Qu.:77.75 3rd Qu.:26.18
## Max. :17.400 Max. :337.0 Max. :91.00 Max. :46.00
hist(USArrests$Murder)
hist(USArrests$Assault)
hist(USArrests$UrbanPop)
hist(USArrests$Rape)
USArrests$State <- rownames(USArrests)
As we can see from the histograms
only Urban Population seems to be normally distributed
Rape seems to be right skewed
Murder and Assault is not normally distributed
Tried logging the data but it does not make it normal
# Murder
USArrests %>%
select(State, Murder) %>%
arrange(desc(Murder)) %>%
head(10)
## State Murder
## Georgia Georgia 17.4
## Mississippi Mississippi 16.1
## Florida Florida 15.4
## Louisiana Louisiana 15.4
## South Carolina South Carolina 14.4
## Alabama Alabama 13.2
## Tennessee Tennessee 13.2
## North Carolina North Carolina 13.0
## Texas Texas 12.7
## Nevada Nevada 12.2
# Assault
USArrests %>%
select(State, Assault) %>%
arrange(desc(Assault)) %>%
head(10)
## State Assault
## North Carolina North Carolina 337
## Florida Florida 335
## Maryland Maryland 300
## Arizona Arizona 294
## New Mexico New Mexico 285
## South Carolina South Carolina 279
## California California 276
## Alaska Alaska 263
## Mississippi Mississippi 259
## Michigan Michigan 255
Urban Population affects murder/rape/assault, but murder/assault/rape does not affect urban population. There is a clear y (crime rate) and x (urban population rate). Thus, we run linear regression and not correlation test to determine if there is a relationship.
# Murder
fit <- lm(Murder ~ UrbanPop, data = USArrests)
r2_val <- summary(fit)$r.squared
p_val <- summary(fit)$coefficients[2, 4]
df_res <- df.residual(fit)
label_text <- paste0("R² = ", round(r2_val, 3),
", p = ", round(p_val, 3))
ggplot(USArrests, aes(x = UrbanPop, y = Murder)) +
geom_point(alpha = 0.6, size = 1.5) +
geom_smooth(method = "lm", color = "black", se = FALSE) +
labs(x = "Urban population percentage (%)", y = "Murder (per 100000)", subtitle = label_text) +
theme_classic()
## `geom_smooth()` using formula = 'y ~ x'
# Assault
fit <- lm(Assault ~ UrbanPop, data = USArrests)
r2_val <- summary(fit)$r.squared
p_val <- summary(fit)$coefficients[2, 4]
label_text <- paste0("R² = ", round(r2_val, 3),
", p = ", round(p_val, 3))
ggplot(USArrests, aes(x = UrbanPop, y = Assault)) +
geom_point(alpha = 0.6, size = 1.5) +
geom_smooth(method = "lm", color = "black", se = FALSE) +
labs(x = "Urban population percentage (%)", y = "Assault (per 100000)", subtitle = label_text) +
theme_classic()
## `geom_smooth()` using formula = 'y ~ x'
# Rape
fit <- lm(Rape ~ UrbanPop, data = USArrests)
r2_val <- summary(fit)$r.squared
p_val <- summary(fit)$coefficients[2, 4]
label_text <- paste0("R² = ", round(r2_val, 3),
", p = ", round(p_val, 3))
ggplot(USArrests, aes(x = UrbanPop, y = Rape)) +
geom_point(alpha = 0.6, size = 1.5) +
geom_smooth(method = "lm", color = "black", se = FALSE) +
labs(x = "Urban population percentage (%)", y = "Rape (per 100000)", subtitle = label_text) +
theme_classic()
## `geom_smooth()` using formula = 'y ~ x'
R-squared values for all three are low (<0.5). Best fit line does not explain the data well. Relationship is not statistically significant for murder and assault (p>0.05), but is statistically significant for rape (p<0.05).