# Question 1: Maps ----

tmap_mode("plot")

map1 <- tm_shape(mobility) +
  tm_polygons("workplace_change",
              palette = "RdBu",
              title = "% Change Workplace Mobility\n(March–May 2020)") +
  tm_layout(main.title = "Workplace Mobility Change: March-May 2020",
            main.title.size = 0.9,
            legend.outside = TRUE)

map2 <- tm_shape(mobility) +
  tm_polygons("workplace_change_may_dec",
              palette = "RdBu",
              title = "% Change Workplace Mobility\n(May–Dec 2020)") +
  tm_layout(main.title = "Workplace Mobility Change: May-Dec 2020",
            main.title.size = 0.9,
            legend.outside = TRUE)

map1

map2

# ANSWER (spatial pattern observations):
# Look for whether urban/metro counties (e.g. Mecklenburg, Wake, Guilford, Durham)
# show a different pattern than rural counties, and whether the pattern from
# March-May persists, reverses, or fades by the May-Dec period.


# Question 2: Paired T-Test (Workplace Change Over Time) ----

# H0: There is no difference in the mean percent change in workplace mobility
#     between the March-May 2020 period and the May-Dec 2020 period.
# H1: The mean percent change in workplace mobility differs between the
#     two time periods.

# Check normality of the differences first
mobility <- mobility |>
  mutate(diff_mobility = workplace_change_may_dec - workplace_change)

ggplot(mobility, aes(x = diff_mobility)) +
  geom_histogram(binwidth = 5)

qqnorm(mobility$diff_mobility)
qqline(mobility$diff_mobility, col = "steelblue", lwd = 2)

# Paired t-test
t.test(mobility$workplace_change_may_dec, mobility$workplace_change,
       paired = TRUE,
       alternative = "two.sided",
       conf.level = 0.95)
## 
##  Paired t-test
## 
## data:  mobility$workplace_change_may_dec and mobility$workplace_change
## t = 17.288, df = 97, p-value < 2.2e-16
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
##  7.522023 9.473095
## sample estimates:
## mean difference 
##        8.497559
# ANSWER: Report whether p < 0.05 -> reject or fail to reject H0, and state
# the conclusion in terms of which period had higher/lower mobility change.


# Question 3: Independent Samples T-Test (Urban vs. Rural) ----

# H0: There is no difference in mean percent change in workplace mobility
#     (March-May 2020) between rural and non-rural counties.
# H1: The mean percent change in workplace mobility differs between
#     rural and non-rural counties.

# Check equal variance assumption
leveneTest(workplace_change ~ rural, data = mobility)
# If Levene's test is NOT significant (p > 0.05), variances are roughly equal
# -> use var.equal = TRUE
# If Levene's test IS significant (p < 0.05), variances differ
# -> use var.equal = FALSE (Welch's t-test)

t.test(workplace_change ~ rural, data = mobility, var.equal = TRUE)
## 
##  Two Sample t-test
## 
## data:  workplace_change by rural
## t = -6.0817, df = 96, p-value = 2.415e-08
## alternative hypothesis: true difference in means between group non-rural and group rural is not equal to 0
## 95 percent confidence interval:
##  -11.715279  -5.949667
## sample estimates:
## mean in group non-rural     mean in group rural 
##               -37.98333               -29.15086
# t.test(workplace_change ~ rural, data = mobility, var.equal = FALSE) # use this version instead if Levene's test is significant

# ANSWER: Report whether p < 0.05 -> reject or fail to reject H0, and state
# which group (rural/non-rural) had a larger drop in workplace mobility.


# Question 4: One Sample T-Test (NC vs. US Average) ----

# Known US average change March-May 2020 = -32

# H0: The mean percent change in workplace mobility for North Carolina
#     counties (March-May 2020) is equal to -32 (the US average).
# H1: The mean percent change in workplace mobility for North Carolina
#     counties is different from -32.

# Calculate the NC mean
mean(mobility$workplace_change, na.rm = TRUE)
## [1] -31.13366
# Check normality
ggplot(mobility, aes(x = workplace_change)) +
  geom_histogram(binwidth = 5)

qqnorm(mobility$workplace_change)
qqline(mobility$workplace_change, col = "steelblue", lwd = 2)

# One-sample t-test
t.test(mobility$workplace_change, mu = -32,
       alternative = "two.sided",
       conf.level = 0.95)
## 
##  One Sample t-test
## 
## data:  mobility$workplace_change
## t = 1.221, df = 97, p-value = 0.225
## alternative hypothesis: true mean is not equal to -32
## 95 percent confidence interval:
##  -32.54188 -29.72545
## sample estimates:
## mean of x 
## -31.13366
# ANSWER: Compare the NC mean to -32, report whether p < 0.05 -> reject or
# fail to reject H0, and state whether NC's mobility drop was more or less
# severe than the US average.