#means

S5 %>%
  group_by(condition) %>%
  summarise_at(.vars = c("ViolentPolice_sc", "ViolentProtester_sc"), .funs = mean)
## # A tibble: 4 × 3
##   condition          ViolentPolice_sc ViolentProtester_sc
##   <fct>                         <dbl>               <dbl>
## 1 control                        8.01                8.44
## 2 police_violence               84.4                18.3 
## 3 protester_violence            13.5                89.2 
## 4 mutual_violence               64.8                73.9
S5 %>%
  group_by(condition) %>%
  summarise_at(.vars = c("MoralPolice_sc", "MoralProtester_sc"), .funs = mean)
## # A tibble: 4 × 3
##   condition          MoralPolice_sc MoralProtester_sc
##   <fct>                       <dbl>             <dbl>
## 1 control                      88.8              83.0
## 2 police_violence              15.4              71.3
## 3 protester_violence           75.2              10.9
## 4 mutual_violence              34.7              24.4
S5 %>%
  group_by(condition) %>%
  summarise_at(.vars = c("HarmfulPolice_sc", "HarmfulProtester_sc"), .funs = mean)
## # A tibble: 4 × 3
##   condition          HarmfulPolice_sc HarmfulProtester_sc
##   <fct>                         <dbl>               <dbl>
## 1 control                        8.06                9.97
## 2 police_violence               84.5                20.8 
## 3 protester_violence            15.0                89.5 
## 4 mutual_violence               67.5                75.9
S5 %>%
  group_by(condition) %>%
  summarise_at(.vars = c("TrustPolice_sc", "TrustGov_sc"), .funs = mean)
## # A tibble: 4 × 3
##   condition          TrustPolice_sc TrustGov_sc
##   <fct>                       <dbl>       <dbl>
## 1 control                      71.4        58.3
## 2 police_violence              24.4        40.1
## 3 protester_violence           67.6        49.9
## 4 mutual_violence              43.4        45.8

#effects on trust

summary(model <- lm(TrustPolice_sc ~ condD1*condD2*condD3*ideology, S5, na.action=na.omit))
## 
## Call:
## lm(formula = TrustPolice_sc ~ condD1 * condD2 * condD3 * ideology, 
##     data = S5, na.action = na.omit)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -80.712 -18.644   0.333  17.342  72.783 
## 
## Coefficients: (8 not defined because of singularities)
##                                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                    49.99827    6.20183   8.062 1.39e-14 ***
## condD1                        -48.24654    8.89643  -5.423 1.13e-07 ***
## condD2                         -9.10310    9.73585  -0.935 0.350466    
## condD3                        -28.62519    8.59080  -3.332 0.000959 ***
## ideology                        5.75203    1.48203   3.881 0.000125 ***
## condD1:condD2                        NA         NA      NA       NA    
## condD1:condD3                        NA         NA      NA       NA    
## condD2:condD3                        NA         NA      NA       NA    
## condD1:ideology                 0.40856    2.14739   0.190 0.849223    
## condD2:ideology                 0.88408    2.26528   0.390 0.696586    
## condD3:ideology                 0.09214    2.03506   0.045 0.963915    
## condD1:condD2:condD3                 NA         NA      NA       NA    
## condD1:condD2:ideology               NA         NA      NA       NA    
## condD1:condD3:ideology               NA         NA      NA       NA    
## condD2:condD3:ideology               NA         NA      NA       NA    
## condD1:condD2:condD3:ideology        NA         NA      NA       NA    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 26.32 on 331 degrees of freedom
## Multiple R-squared:  0.4213, Adjusted R-squared:  0.4091 
## F-statistic: 34.43 on 7 and 331 DF,  p-value: < 2.2e-16
summary(model <- lm(TrustGov_sc ~ condD1*condD2*condD3*ideology, S5, na.action=na.omit))
## 
## Call:
## lm(formula = TrustGov_sc ~ condD1 * condD2 * condD3 * ideology, 
##     data = S5, na.action = na.omit)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -54.419 -19.725   3.185  17.579  54.578 
## 
## Coefficients: (8 not defined because of singularities)
##                               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                    47.4533     6.0362   7.861  5.4e-14 ***
## condD1                        -14.7323     8.6589  -1.701   0.0898 .  
## condD2                          6.5975     9.4759   0.696   0.4868    
## condD3                         -2.8467     8.3614  -0.340   0.7337    
## ideology                        2.9110     1.4425   2.018   0.0444 *  
## condD1:condD2                       NA         NA      NA       NA    
## condD1:condD3                       NA         NA      NA       NA    
## condD2:condD3                       NA         NA      NA       NA    
## condD1:ideology                -0.8991     2.0901  -0.430   0.6674    
## condD2:ideology                -3.9503     2.2048  -1.792   0.0741 .  
## condD3:ideology                -2.5960     1.9807  -1.311   0.1909    
## condD1:condD2:condD3                NA         NA      NA       NA    
## condD1:condD2:ideology              NA         NA      NA       NA    
## condD1:condD3:ideology              NA         NA      NA       NA    
## condD2:condD3:ideology              NA         NA      NA       NA    
## condD1:condD2:condD3:ideology       NA         NA      NA       NA    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 25.61 on 331 degrees of freedom
## Multiple R-squared:  0.08103,    Adjusted R-squared:  0.06159 
## F-statistic: 4.169 on 7 and 331 DF,  p-value: 0.0002042

#plot trust in the police

S5_long <- S5 %>%
  pivot_longer(cols = c(TrustGov_sc, TrustPolice_sc, MoralPolice_sc, HarmfulPolice_sc, PolicySupport), 
               names_to = "DV", values_to = "value")

S5_long$DV <- factor(S5_long$DV, levels = c("HarmfulPolice_sc", "MoralPolice_sc", "TrustPolice_sc", "TrustGov_sc", "PolicySupport"))


# Plotting
p_trustpol <- ggplot(S5_long, aes(x = DV, y = value, fill = condition)) +
  stat_summary(fun = mean, geom = "bar", position = position_dodge(width = 0.8), width = 0.75) +
  stat_summary(fun.data = mean_cl_boot, geom = "errorbar", colour = "grey", 
               position = position_dodge(width = 0.8), width = 0.2) +
  scale_y_continuous(limits = c(0, 100)) +
  theme_bw() +
  scale_fill_manual(values = c("#ffffb3","#80b1d3", "#8dd3c7", "#fb8072")) +
  labs(x = "Condition", 
       y = "Score", 
       title = "Effects on dependent variables") +
  theme(
    plot.title = element_text(hjust = 0.5, size = 14), 
    plot.subtitle = element_text(hjust = 0.5),
    plot.caption = element_text(hjust = 0.5, face = "italic")
  ) 

# Print the plot
print(p_trustpol)

#violin plot with ideology

S5 %>%
  filter(!is.na(ideology.cat) & !is.na(TrustPolice_sc)) %>% 
ggplot(aes(x = condition, y = TrustPolice_sc, fill = ideology.cat))+
  geom_split_violin(trim = FALSE, alpha = .4)+
  geom_boxplot(width = .2, alpha = .6, position = position_dodge(.25))+
  scale_fill_viridis_d(option = "E") +
  stat_summary(fun = "mean", geom = "point", position = position_dodge(width = 0.25)) +
  stat_summary(fun.data = "mean_se", geom = "errorbar", width = .1, position = position_dodge(width = 0.25))+
  scale_x_discrete(name = "Condition", labels = c("control", "police_violence", "protester_violence", "mutual_violence")) +
         scale_y_continuous(name = "Trust in the police") +
        coord_cartesian(ylim = c(0, 100)) +  # Set the y-axis limits
         theme_minimal()+
  scale_fill_manual(values=c('tomato2',"steelblue2"), name = "Ideology")+
                       labs(title = "Effects of police violence on trust",
                       caption = "How much do you trust Philadelphia police department?")+
        theme(
                 plot.title = element_text(hjust = 0.5, size = 16, face = 'bold', margin = margin(b = 20)), 
                 plot.caption = element_text(hjust = 0.5, face = "italic", size = 12),
                 axis.text.x = element_text(size = 14, margin = margin(t = 10)),
                 axis.title.y = element_text(size = 16, face = 'bold', margin = margin(r = 15)),
                 axis.title.x = element_text(size = 14, face = 'bold', margin = margin(t = 15, b = 15)),
                 legend.text = element_text(size = 13),
                 legend.title = element_text(size = 14, face = 'bold'),
                 plot.margin = margin(t = 20, l = 20, b = 20)
       )
## Scale for fill is already present.
## Adding another scale for fill, which will replace the existing scale.