Load the necessary libraries

library(ggplot2)
library(reshape2)
library(scales)
library(dplyr)
library(forecast)
library(TSA)
library(tidyr)
library(stargazer)
library(rdrobust)

Effect of the 2023 War in Americans’ information seeking regarding Islamophobia and antisemitism

# Create the data frame
search_data <- tribble(
  ~Day, ~Antisemitism, ~Islamophobia,
  '9/14/23', 17, 2,
  '9/15/23', 14, 2,
  '9/16/23', 13, 2,
  '9/17/23', 15, 1,
  '9/18/23', 22, 1,
  '9/19/23', 25, 2,
  '9/20/23', 23, 2,
  '9/21/23', 23, 2,
  '9/22/23', 18, 1,
  '9/23/23', 16, 0,
  '9/24/23', 15, 2,
  '9/25/23', 20, 2,
  '9/26/23', 19, 1,
  '9/27/23', 21, 1,
  '9/28/23', 20, 1,
  '9/29/23', 17, 1,
  '9/30/23', 11, 1,
  '10/1/23', 11, 1,
  '10/2/23', 19, 1,
  '10/3/23', 17, 0.5,
  '10/4/23', 17, 2,
  '10/5/23', 17, 1,
  '10/6/23', 19, 1,
  '10/7/23', 19, 2,
  '10/8/23', 31, 2,
  '10/9/23', 53, 1,
  '10/10/23', 70, 4,
  '10/11/23', 94, 5,
  '10/12/23', 100, 5,
  '10/13/23', 96, 5,
  '10/14/23', 73, 4,
  '10/15/23', 72, 4,
  '10/16/23', 70, 10,
  '10/17/23', 73, 9,
  '10/18/23', 78, 6,
  '10/19/23', 74, 5,
  '10/20/23', 72, 8,
  '10/21/23', 58, 2,
  '10/22/23', 72, 2,
  '10/23/23', 69, 4,
  '10/24/23', 74, 6,
  '10/25/23', 68, 4,
  '10/26/23', 75, 5,
  '10/27/23', 71, 3,
  '10/28/23', 56, 3,
  '10/29/23', 63, 3
)


# Convert Day to Date type
search_data <- search_data %>%
  mutate(Day = as.Date(Day, format="%m/%d/%y"),
         PostWar = as.numeric(Day >= as.Date("2023-10-07")))

# T-test for Antisemitism
t_test_antisemitism <- t.test(Antisemitism ~ PostWar, data = search_data)

# T-test for Islamophobia
t_test_islamophobia <- t.test(Islamophobia ~ PostWar, data = search_data)

# Print the t-test results
print(t_test_antisemitism)
## 
##  Welch Two Sample t-test
## 
## data:  Antisemitism by PostWar
## t = -13.35, df = 23.863, p-value = 1.458e-12
## alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
## 95 percent confidence interval:
##  -58.83692 -43.07612
## sample estimates:
## mean in group 0 mean in group 1 
##        17.78261        68.73913
print(t_test_islamophobia)
## 
##  Welch Two Sample t-test
## 
## data:  Islamophobia by PostWar
## t = -6.3452, df = 25.007, p-value = 1.215e-06
## alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
## 95 percent confidence interval:
##  -4.117713 -2.099678
## sample estimates:
## mean in group 0 mean in group 1 
##        1.326087        4.434783
# Regression Discontinuity Design (RDD) using linear regression
# Adding interaction term for PostWar period to allow different slopes
search_data$rdd_day <- as.numeric(search_data$Day) # Numeric transformation for regression
search_data$interaction_term <- search_data$PostWar * search_data$rdd_day

# Model for Antisemitism
model_antisemitism <- lm(Antisemitism ~ rdd_day + PostWar + interaction_term, data = search_data)

# Model for Islamophobia
model_islamophobia <- lm(Islamophobia ~ rdd_day + PostWar + interaction_term, data = search_data)

# Summary of the RDD models
stargazer(model_antisemitism, model_islamophobia, type = "text", 
            title = "Google Searches for Antisemitism and Islamophobia Over Time", 
                 subtitle = "Source: Google Trends  | Author :Mohamed Dhia Hammami",
            align = TRUE)
## 
## Google Searches for Antisemitism and Islamophobia Over Time
## ==========================================================
##                                   Dependent variable:     
##                               ----------------------------
##                                Antisemitism  Islamophobia 
##                                    (1)            (2)     
## ----------------------------------------------------------
## rdd_day                           -0.064        -0.039    
##                                  (0.411)        (0.053)   
##                                                           
## PostWar                         -9,402.681    -1,122.076  
##                                (11,423.390)   (1,473.073) 
##                                                           
## interaction_term                  0.481          0.057    
##                                  (0.582)        (0.075)   
##                                                           
## Constant                        1,278.282       757.625   
##                                (8,072.828)    (1,041.009) 
##                                                           
## ----------------------------------------------------------
## Observations                        46            46      
## R2                                0.807          0.486    
## Adjusted R2                       0.793          0.449    
## Residual Std. Error (df = 42)     13.086         1.687    
## F Statistic (df = 3; 42)        58.476***      13.227***  
## ==========================================================
## Note:                          *p<0.1; **p<0.05; ***p<0.01
## 
## Google Searches for Antisemitism and Islamophobia Over Time
## ====================================================
## Source: Google Trends | Author :Mohamed Dhia Hammami
## ----------------------------------------------------
# Visualization for Antisemitism and Islamophobia
ggplot(search_data, aes(x = Day)) +
  geom_line(aes(y = Antisemitism, color = "Antisemitism")) +
  geom_line(aes(y = Islamophobia, color = "Islamophobia")) +
  geom_vline(xintercept = as.Date("2023-10-07"), linetype="dashed") +
  labs(title = "Google Searches for Antisemitism and Islamophobia Over Time",
       subtitle = "Source: Google Trends  | Author :Mohamed Dhia Hammami",
       x = "Date", y = "Search Frequency") +
  scale_color_manual(values = c("Antisemitism" = "blue", "Islamophobia" = "red")) +
  theme_minimal()

##News Data Preparation

# Create a data frame with the data about the number of articles covering Antisemitism and/or Islamophobia by outlet
news_data <- data.frame(
  NewsSource = c("NY Times", "USA Today", "Fox News", "NPR", "Politico", 
                 "LA Times", "CBS News", "Axios"),
  Antisemitism = c(274, 55, 75, 16, 35, 48, 17, 23),
  Islamophobia = c(8, 1, 21, 7, 4, 2,  1, 2),
  Both = c(30, 23, 19, 5, 12, 12, 6, 12)
)

# Melt the data for use with ggplot2
news_data_long <- reshape2::melt(news_data, id.vars = 'NewsSource')


# Summarize the data to get total counts for each category
total_counts <- news_data_long %>%
  group_by(variable) %>%
  summarise(Total = sum(value))

# Calculate the percentages for labels
total_counts <- total_counts %>% 
  mutate(Percentage = Total / sum(Total),
         Label = scales::percent(Percentage))

###News Data Visualization

# Create the bar chart
ggplot(news_data_long, aes(x = NewsSource, y = value, fill = variable)) +
  geom_bar(stat = 'identity', position = 'stack') +
  scale_fill_manual(values = c("Antisemitism" = "skyblue", 
                               "Islamophobia" = "lightgreen", 
                               "Both" = "salmon")) +
  labs(title = "US media coverage of Islamophobia and Antisemitism",
       subtitle = "Source: Nexis Uni (Oct 7 - Nov 11, 2023)  | Author :Mohamed Dhia Hammami",
       x = "News Source",
       y = "Number of Articles") +
  theme_minimal() +
  theme(legend.position = "bottom", 
        axis.text.x = element_text(angle = 45, hjust = 1))

# Create the histogram (dodged bar chart)
ggplot(news_data_long, aes(x = NewsSource, y = value, fill = variable)) +
  geom_bar(stat = 'identity', position = 'dodge') +
  scale_fill_manual(values = c("Antisemitism" = "skyblue", 
                               "Islamophobia" = "lightgreen", 
                               "Both" = "salmon")) +
  labs(title = "US media coverage of Islamophobia and Antisemitism since Oct 7, 2023",
       subtitle = "Source: Nexis Uni (Oct 7 - Nov 11, 2023)  | Author :Mohamed Dhia Hammami",
       x = "News Source",
       y = "Number of Articles") +
  theme_minimal() +
  theme(legend.position = "bottom")

# Create the stacked bar chart with proportions 
ggplot(news_data_long, aes(x = NewsSource, y = value, fill = variable)) +
  geom_bar(stat = 'identity', position = 'fill') +
  scale_fill_manual(values = c("Antisemitism" = "skyblue", 
                               "Islamophobia" = "lightgreen", 
                               "Both" = "salmon")) +
  scale_y_continuous(labels = percent_format(scale = 100)) +
  labs(title = "US media coverage of Islamophobia and Antisemitism since Oct 7, 2023",
       subtitle = "Source: Nexis Uni (Oct 7 - Nov 11, 2023)  | Author :Mohamed Dhia Hammami",
       x = "News Source",
       y = "Proportion of Articles") +
  theme_minimal()

# Create the pie chart with percentages
ggplot(total_counts, aes(x = "", y = Total, fill = variable, label = Label)) +
  geom_bar(stat = "identity", width = 1) +
  coord_polar("y", start = 0) +
  geom_text(aes(label = Label), position = position_stack(vjust = 0.5)) +
  scale_fill_manual(values = c("Antisemitism" = "skyblue",
                               "Islamophobia" = "lightgreen",
                               "Both" = "salmon")) +
  labs(title = "Distribution of US Media Coverage on Islamophobia and Antisemitism",
       subtitle = "Source: Nexis Uni (Oct 7 - Nov 11, 2023) | Author :Mohamed Dhia Hammami",
       fill = "Category") +
  theme_void() +
  theme(legend.position = "right")

FBI Data Preparation

  # FBI data with occurence of wars (=1 if death >100)
  fbi_data <- data.frame(
    Year = c(2022, 2021, 2020, 2019, 2018, 2017, 2016, 2015, 2014, 2013, 2012, 2011, 2010, 2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002, 2001, 2000, 1999, 1998, 1997, 1996, 1995, 1994, 1993, 1992, 1991),
    War = as.factor(c(1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0)),
    Jewish = c(1124, 824, 854, 1053, 851, 953, 646, 666, 613, 632, 868, 769, 827, 932, 1032, 972, 967, 935, 965, 931, 938, 1051, 1121, 1112, 1087, 1088, 1109, 1059, 919, 1140, 1020, 837),
    Arabs = c(92, 103, 82, 101, 80, 104, 51, 39, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 80, 254, 38, 24, 25, 34, 40, 55, 52, 55, 60, 73),
    Muslim = c(158, 153, 129, 181, 192, 277, 310, 258, 156, 138, 162, 160, 160, 107, 165, 115, 158, 138, 158, 148, 156, 499, 29, 32, 21, 28, 27, 29, 17, 13, 15, 10)
  )
  

# Creating a new column that combines Arabs and Muslims
fbi_data$Arabs_Muslim <- fbi_data$Arabs + fbi_data$Muslim

# Create a dataframe for plotting
  plot_data <- fbi_data %>%
    select(Year, War, Jewish, Arabs_Muslim) %>%
    pivot_longer(cols = c(Jewish, Arabs_Muslim), names_to = "Group", values_to = "Crimes")
  
# Identify the war years for shading
war_years <- fbi_data %>%
  filter(War == 1) %>%
  distinct(Year)

Effect of Isreal-Gaza military confrontations

  # Plot with shaded war years
  ggplot(plot_data, aes(x = Year, y = Crimes, color = Group, group = Group)) +
    geom_rect(data = war_years, aes(xmin = Year - 0.5, xmax = Year + 0.5, ymin = -Inf, ymax = +Inf), 
              inherit.aes = FALSE, fill = "grey", alpha = 0.2) +
    geom_line() +
    geom_point(aes(shape = factor(War))) +
    geom_smooth(method = "lm", se = TRUE, aes(group = Group, color = Group)) + # Add regression line with color by group
    scale_color_manual(values = c("Jewish" = "blue", "Arabs_Muslim" = "red")) + # Define colors for each group
    labs(title = "Evolution of Reported Hate Crimes",
         subtitle = "Source: FBI’s Uniform Crime Reporting (UCR) Program | Author :Mohamed Dhia Hammami",
         x = "Year",
         y = "Number of Hate Crimes",
         color = "Group",
         shape = "War Year") +
    theme_minimal() +
    theme(plot.subtitle = element_text(hjust = 0.5), # Center the subtitle
          legend.position = "bottom")
## `geom_smooth()` using formula = 'y ~ x'

## War effect on anti-Jewish and anti-Muslim/Arab hate crimes

plot_data <- fbi_data %>%
    mutate(Group = case_when(
      War == 1 ~ "War Years",
      War == 0 ~ "Non-War Years"
    )) %>%
    group_by(Group) %>%
    summarise(Jewish_mean = mean(Jewish),
              Arabs_Muslim_mean = mean(Arabs + Muslim),
              .groups = 'drop') %>%
    pivot_longer(cols = c(Jewish_mean, Arabs_Muslim_mean), names_to = "Crime Type", values_to = "Mean Crimes")
  
  # Rename the levels for the plot
  plot_data$`Crime Type` <- recode(plot_data$`Crime Type`, 
                                   Jewish_mean = "Anti-Jewish",
                                   Arabs_Muslim_mean = "Anti-Arabs/Muslim")
  
  # Create the grouped bar chart
  ggplot(plot_data, aes(x =`Crime Type` , y = `Mean Crimes`, fill = `Group`)) +
    geom_bar(stat = "identity", position = position_dodge()) +
    labs(title = "Average Hate Crimes During War and Non-War Years",
         subtitle = "Source: FBI’s Uniform Crime Reporting (UCR) Program | Author :Mohamed Dhia Hammami",
         x = "Crime Type",
         y = "Average Number of Hate Crimes",
         fill = "Year Group") +
    scale_fill_brewer(palette = "Pastel1") +
    theme_minimal() +
    theme(plot.subtitle = element_text(hjust = 0.5)) +
    geom_text(aes(label = round(`Mean Crimes`, 1)), position = position_dodge(width = 0.9), vjust = -0.25)

  # Perform t-test for the Jewish group
  t_test_Jewish <- t.test(Jewish ~ War, data = fbi_data)
  
  # Perform t-test for the Arab/Muslim group
  # First, create a combined Arab/Muslim column
  fbi_data$Arabs_Muslim <- fbi_data$Arabs + fbi_data$Muslim
  t_test_Arabs_Muslim <- t.test(Arabs_Muslim ~ War, data = fbi_data)
  
  # Output the results of the t-tests
  print(t_test_Jewish)
## 
##  Welch Two Sample t-test
## 
## data:  Jewish by War
## t = 0.29189, df = 29.182, p-value = 0.7724
## alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
## 95 percent confidence interval:
##  -95.91434 127.85944
## sample estimates:
## mean in group 0 mean in group 1 
##        941.7059        925.7333
  print(t_test_Arabs_Muslim)
## 
##  Welch Two Sample t-test
## 
## data:  Arabs_Muslim by War
## t = 0.82611, df = 22.012, p-value = 0.4176
## alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
## 95 percent confidence interval:
##  -58.74265 136.53088
## sample estimates:
## mean in group 0 mean in group 1 
##        198.2941        159.4000

Effect of Trump Election

  # Add a binary indicator for post-2016 years
  fbi_data <- fbi_data %>%
    mutate(post_2016 = ifelse(Year > 2016, 1, 0),
           year_since_cutoff = Year - 2016) # Create a year-since-cutoff variable
  
  # Plotting with ggplot2
  ggplot(fbi_data, aes(x = Year)) +
    geom_line(aes(y = Jewish, color = "Jewish")) + # Line for Jewish crimes
    geom_line(aes(y = Arabs_Muslim, color = "Arabs_Muslim")) + # Line for Arab/Muslim crimes
    scale_color_manual(values = c("Jewish" = "blue", "Arabs_Muslim" = "darkgreen")) +
    geom_vline(xintercept = 2016.5, linetype = "dashed", color = "red", linewidth = 1) +
    # Add regression lines for Jewish crimes pre and post-2016
    geom_smooth(data = subset(fbi_data, post_2016 == 0 & !is.na(Jewish)), aes(y = Jewish, x = Year, color = "Pre-2016"), method = "lm", se = T) +
    geom_smooth(data = subset(fbi_data, post_2016 == 1 & !is.na(Jewish)), aes(y = Jewish, x = Year, color = "Post-2016"), method = "lm", se = T) +
    # Add regression lines for Arab/Muslim crimes pre and post-2016
    geom_smooth(data = subset(fbi_data, post_2016 == 0 & !is.na(Arabs_Muslim)), aes(y = Arabs_Muslim, x = Year, color = "Pre-2016"), method = "lm", se = T) +
    geom_smooth(data = subset(fbi_data, post_2016 == 1 & !is.na(Arabs_Muslim)), aes(y = Arabs_Muslim, x = Year, color = "Post-2016"), method = "lm", se = T) +
    labs(title = "Effect of Trump's Election on Reported Hate Crimes",
         subtitle = "Source: FBI’s Uniform Crime Reporting (UCR) Program
Author : Mohamed Dhia Hammami",
         x = "Year", y = "Reported Hate Crimes") +
    theme_minimal() +
    theme(legend.position = "bottom", legend.title = element_blank(),
          axis.text.x = element_text(angle = 45, vjust = 0.5)) 
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'

Statistical estimation of the effect of Trump election

  # Fit a linear regression model for Jewish hate crimes
  model_jewish <- lm(Jewish ~ post_2016 * year_since_cutoff, data = fbi_data)
  
  # Fit a linear regression model for Arab/Muslim hate crimes
  model_arabs_muslim <- lm(Arabs_Muslim ~ post_2016 * year_since_cutoff, data = fbi_data)
  
  # Output the results using stargazer
  stargazer(model_jewish, model_arabs_muslim, type = "text", 
            title = "RDD Analysis of Hate Crimes Reporting Before and After 2016", 
            align = TRUE)
## 
## RDD Analysis of Hate Crimes Reporting Before and After 2016
## ==========================================================
##                                   Dependent variable:     
##                               ----------------------------
##                                   Jewish     Arabs_Muslim 
##                                    (1)            (2)     
## ----------------------------------------------------------
## post_2016                        145.991        117.426   
##                                 (114.426)      (129.041)  
##                                                           
## year_since_cutoff               -15.398***      6.178*    
##                                  (2.974)        (3.354)   
##                                                           
## post_2016:year_since_cutoff       31.827        -28.293   
##                                  (27.353)      (30.847)   
##                                                           
## Constant                        739.675***    235.308***  
##                                  (43.359)      (48.896)   
##                                                           
## ----------------------------------------------------------
## Observations                        32            32      
## R2                                0.493          0.222    
## Adjusted R2                       0.439          0.139    
## Residual Std. Error (df = 28)    113.748        128.275   
## F Statistic (df = 3; 28)         9.071***       2.662*    
## ==========================================================
## Note:                          *p<0.1; **p<0.05; ***p<0.01