library(tidyverse)
library(haven)
library(readr)

Question 1: Survey Question

I think the issue with the survey question is the combination of wording, specifically “gridlock” and “dysfunction,” because to me, it feels like it places a harsh connotation on the question that may sway survey respondents from thinking critically. There is also a lack of information to help respondents recall the event.

Additionally, the answer options do not make much sense.

Revised Survey Question

Who do you believe caused the 43-day legislative gridlock in Washington in 2025?

  • Donald Trump
  • Joe Biden
  • Republican Congressional Leadership
  • Democratic Congressional Leadership

I considered separating Congress into the House and Senate, along with removing Biden since he is not really a factor. However, I feel like these choices are straightforward and easy to answer.

Question 2.1: September 1 Spending

sept_data <- read_csv("meta/FacebookAdLibraryReport_2024-09-01_US_yesterday_locations.csv")
## New names:
## Rows: 56 Columns: 3
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (2): Location name, Amount spent (USD) dbl (1): ...1
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
## Specify the column types or set `show_col_types = FALSE` to quiet this message.
## • `` -> `...1`
sept_data$spent <- as.numeric(
  gsub(",", "", gsub("≤", "", sept_data[["Amount spent (USD)"]])))

sept_data[which.max(sept_data$spent), ]
## # A tibble: 1 × 4
##    ...1 `Location name` `Amount spent (USD)`  spent
##   <dbl> <chr>           <chr>                 <dbl>
## 1    40 Pennsylvania    234238               234238
sum(sept_data$spent, na.rm = TRUE)
## [1] 2455487

Question 2.2: Create a Date Variable

sept_data$date <- as.Date("2024-09-01")

Question 3.3: Create a Vector of Dates

dates <- seq(
  as.Date("2024-09-02"),
  as.Date("2024-11-30"),
  by = "day")

length(dates)
## [1] 90

Question 3.3.1: Identify CSV Files

all_data <- list.files("meta",
  pattern = "\\.csv$",
  full.names = TRUE)

all_data <- all_data[
  !grepl("2024-09-01", basename(all_data))]

all_data <- sort(all_data)

length(all_data)
## [1] 91
all_data <- paste0(
  "meta/FacebookAdLibraryReport_",
  dates,
  "_US_yesterday_locations.csv")

Question 4: Combine Daily Spending Data

names(sept_data) <- make.names(names(sept_data))

combined_data <- data.frame(
  state = sept_data[["Location.name"]],
  spent = sept_data$spent,
  date = sept_data$date)

for (i in 1:length(all_data)) {
  
  total_data <- read_csv(all_data[i])
  
  names(total_data) <- make.names(names(total_data))
  
  total_data$spent <- as.numeric(
    gsub(",", "", gsub("≤", "", total_data[["Amount.spent..USD."]])))
  
  right_data <- data.frame(
    state = total_data[["Location.name"]],
    spent = total_data$spent,
    date = dates[i])
  
  combined_data <- rbind(combined_data, right_data)
}

head(combined_data)
##            state  spent       date
## 1        Alabama  18727 2024-09-01
## 2         Alaska   8125 2024-09-01
## 3 American Samoa    100 2024-09-01
## 4        Arizona 101491 2024-09-01
## 5       Arkansas  12502 2024-09-01
## 6     California 218232 2024-09-01

Question 5: Nationwide Daily Spending

daily <- aggregate(spent ~ date,
                  data = combined_data,
                  FUN = sum,
                  na.rm = TRUE)

head(daily)
##         date   spent
## 1 2024-09-01 2455487
## 2 2024-09-02 2385876
## 3 2024-09-03 2722227
## 4 2024-09-04 2728140
## 5 2024-09-05 2993057
## 6 2024-09-06 3102120

Question 6: Graph Nationwide Daily Spending

plot(daily$date, daily$spent,
     type = "l",
     col = "brown",
     lwd = 2,
     main = "Daily Spending on Meta Political Ads",
     xlab = "Date",
     ylab = "",
     yaxt = "n")

axis(2, at = axTicks(2),
     labels = paste0("$", format(axTicks(2),
      big.mark = ",", scientific = FALSE)),
     las = 1)

mtext("Money Spent", side = 2, line = 8)

Interpretation

The spike in political advertisement spending in September may be associated with the presidential debate between Kamala Harris and Donald Trump

it increased in October as Election Day came close then declined after the election ended

Question 7: Identify Swing States

swing_states <- c("Arizona", "Georgia", "Michigan",
                  "North Carolina", "Nevada", "Pennsylvania",
                  "Wisconsin")

combined_data$swings <- combined_data$state %in% swing_states

head(combined_data)
##            state  spent       date swings
## 1        Alabama  18727 2024-09-01  FALSE
## 2         Alaska   8125 2024-09-01  FALSE
## 3 American Samoa    100 2024-09-01  FALSE
## 4        Arizona 101491 2024-09-01   TRUE
## 5       Arkansas  12502 2024-09-01  FALSE
## 6     California 218232 2024-09-01  FALSE

Question 8: Aggregate Swing and Non-Swing State Spending

state_data <- combined_data[
  combined_data$state %in% state.name,]

daily_swing <- aggregate(
  spent ~ date + swings,
  data = state_data,
  FUN = sum,
  na.rm = TRUE)

head(daily_swing)
##         date swings   spent
## 1 2024-09-01  FALSE 1474662
## 2 2024-09-02  FALSE 1429259
## 3 2024-09-03  FALSE 1420490
## 4 2024-09-04  FALSE 1462263
## 5 2024-09-05  FALSE 1538479
## 6 2024-09-06  FALSE 1607871

Question 9: Graph Swing and Non-Swing State Spending

swing <- daily_swing[daily_swing$swings == TRUE, ]
non_swing <- daily_swing[daily_swing$swings == FALSE, ]


plot(swing$date, swing$spent,
     type = "l", col = "green",
     ylim = range(daily_swing$spent),
     main = "Swing vs. Non-Swing State Spending",
     xlab = "Date",
     ylab = "",
     yaxt = "n")

axis(2,
     at = axTicks(2),
     labels = paste0("$", format(axTicks(2),
                          big.mark = ",", scientific = FALSE)),
     las = 1, cex.axis = 0.85)

mtext("Spending (USD)", side = 2, line = 8)

lines(non_swing$date, non_swing$spent,
      col = "brown")

legend("topleft",
       legend = c("Swing States", "Non-Swing States"),
       col = c("green", "brown"), lty = 1)

Interpretation

The spending increases in both swing and non-swing states in September spike

#Problem 2

genforward <- read_sav("genforward sept 2017.sav")

Data cleaning

q1_num <- as.numeric(genforward[["Q1"]])
genforward[["genforwardclean"]] <- ifelse(q1_num == 1, "Strongly approve",
                            ifelse(q1_num == 2, "Somewhat approve",
                            ifelse(q1_num == 3, "Neither approve nor disapprove",
                            ifelse(q1_num == 4, "Somewhat disapprove",
                            ifelse(q1_num == 5, "Strongly disapprove", NA)))))

genforward[["approve"]] <- ifelse(is.na(genforward[["genforwardclean"]]), NA,
                           ifelse(genforward[["genforwardclean"]] %in% c("Strongly approve", "Somewhat approve"),
                                  "Yes", "No"))

party_num <- as.numeric(genforward[["PartyID7"]])
genforward[["party"]] <- ifelse(party_num == 1, "Strong Democrat",
                               ifelse(party_num == 2, "Moderate Democrat",
                               ifelse(party_num == 3, "Lean Democrat",
                               ifelse(party_num == 4, "Independent/None",
                               ifelse(party_num == 5, "Lean Republican",
                               ifelse(party_num == 6, "Moderate Republican",
                               ifelse(party_num == 7, "Strong Republican", NA)))))))

genforward[["republican"]] <- genforward[["party"]] %in%
  c("Lean Republican", "Moderate Republican", "Strong Republican")

# gender: 1 = Male, 2 = Female (0 = Unknown, 98 = skipped become NA)
gender_num <- as.numeric(genforward[["gender"]])
genforward[["gender"]] <- ifelse(gender_num == 1, "Male",
                                ifelse(gender_num == 2, "Female", NA))

vote_num <- as.numeric(genforward[["Q0"]])
genforward[["vote2016"]] <- ifelse(vote_num == 1, "Clinton",
                            ifelse(vote_num == 2, "Trump",
                            ifelse(vote_num == 3, "Someone else",
                            ifelse(vote_num == 4, "Did not vote", NA))))

Question 1 Approves of Trump

q1 <- mean(genforward[["approve"]] == "Yes", na.rm = TRUE) * 100
cat("1) % approve of Trump (all valid respondents):", round(q1, 2), "\n")
## 1) % approve of Trump (all valid respondents): 14.72

Question 2 Republican men who approve

republicanmen <- genforward[which(genforward[["republican"]] & genforward[["gender"]] == "Male"), "approve"][[1]]
cat("2) Republican men approving:", round(mean(republicanmen == "Yes", na.rm = TRUE) * 100, 2),
    "% (n =", sum(!is.na(republicanmen)), ")\n")
## 2) Republican men approving: 52.44 % (n = 225 )

Question 3 Republican women who approve

republicanwomen <- genforward[which(genforward[["republican"]] & genforward[["gender"]] == "Female"), "approve"][[1]]
cat("3) Republican women approving:", round(mean(republicanwomen == "Yes", na.rm = TRUE) * 100, 2),
    "% (n =", sum(!is.na(republicanwomen)), ")\n")
## 3) Republican women approving: 45.45 % (n = 121 )

Question 4 Most important issue

issue_names <- c(
  "Social Security","Foreign policy", "National debt", "Environment and climate change", "Gay rights",
  "Gun control", "Health care", "Poverty", "Income inequality",
  "Unemployment", "Economic growth", "Military strength",
  "Morality and religion in society", "Racism", "Immigration","Taxes",
  "Women's rights", "Crime","Abortion", "Education",
  "Terrorism and homeland security", "Police brutality"
)
q13_cols <- paste0("Q13_", 1:22)

q13_mat <- sapply(genforward[, q13_cols], as.numeric)
genforward[["most_important_issue"]] <- apply(q13_mat, 1, function(r) {
  w <- which(r == 1)
  if (length(w) == 1) issue_names[w] else NA_character_})

cat("\n4) Most important issue (all respondents):\n")
## 
## 4) Most important issue (all respondents):
print(sort(table(genforward[["most_important_issue"]]), decreasing = TRUE))
## 
##                           Racism                      Gun control 
##                              311                              209 
##                    National debt                        Education 
##                              203                              153 
##  Terrorism and homeland security                      Health care 
##                              131                               96 
##                  Economic growth                Income inequality 
##                               89                               81 
##                          Poverty                         Abortion 
##                               72                               52 
##                   Foreign policy                     Unemployment 
##                               52                               48 
##                            Crime                            Taxes 
##                               41                               40 
## Morality and religion in society                       Gay rights 
##                               37                               32 
##                      Immigration                  Social Security 
##                               32                               26 
##                   Women's rights                Military strength 
##                               15                               12 
##   Environment and climate change 
##                                9

Question 5 issues among 2016 Trump voters

trump <- genforward[which(genforward[["vote2016"]] == "Trump" & !is.na(genforward[["most_important_issue"]])), ]
trump_tab <- sort(prop.table(table(trump[["most_important_issue"]])) * 100,
                  decreasing = TRUE)
cat("\n5) 2016 Trump voters (n =", nrow(trump), ") - top issues (%):\n")
## 
## 5) 2016 Trump voters (n = 236 ) - top issues (%):
print(round(trump_tab[1:5], 2))
## 
##  Terrorism and homeland security                      Gun control 
##                            18.22                            10.59 
##                  Economic growth Morality and religion in society 
##                             8.90                             7.20 
##                        Education 
##                             6.78
top2 <- names(trump_tab)[1:2]

Question 6 issues among 2016 Clinton voters

clinton <- genforward[which(genforward[["vote2016"]] == "Clinton" & !is.na(genforward[["most_important_issue"]])), ]
clinton_tab <- prop.table(table(clinton[["most_important_issue"]])) * 100
cat("\n6) 2016 Clinton voters (n =", nrow(clinton), ") - Trump voters' top two issues (%):\n")
## 
## 6) 2016 Clinton voters (n = 853 ) - Trump voters' top two issues (%):
print(round(clinton_tab[top2], 2))
## 
## Terrorism and homeland security                     Gun control 
##                            4.45                           13.60