Start

options(digits = 3)
options(contrasts=c("contr.treatment", "contr.treatment"))
library(pacman)
p_load(ggrepel, lavaanPlot, mediation, kirkegaard, dplyr, readr, googlesheets4, lavaan, polycor, rms, readxl, dkstat, ggeffects)

theme_set(theme_bw())

#ad hoc func
Inf_to_NA = function(x) {
  x[is.infinite(x)] = NA
  x
}

describe = function(x) psych::describe(x) %>% as.matrix()

Data

#load data
silence({
  kb16 = read_rds("data/Kirkegaard and Bjerrekær 2016 data.rds")
  kb16_full = read_excel("data/Kirkegaard and Bjerrekær 2016 data raw.xlsx")
  kb16_aggr = read_rds("data/Kirkegaard and Bjerrekær 2016 data aggr.rds") %>% 
    rownames_to_column("ISO")
    
  dk_fiscal = read_csv("data/denmark.csv") %>% 
    mutate(
      #add synonym to avoid breaking existing code
      dnk_net_fiscal_contribution = dnk_net_fiscal_contribution_2014
    )
  
  #merge
  dk_fiscal = full_join(dk_fiscal, kb16_aggr, by = "ISO") %>% 
    mutate(
      dk_fiscal = dnk_net_fiscal_contribution_2014,
      dk_benefits_use = Denmark.Social_benefits_30_39,
      Muslim_frac = IslamPewResearch2010
    ) %>% 
    arrange(
      ISO
    )
  
  #new data
  #old google sheets package code
  # gs = gs_url("https://docs.google.com/spreadsheets/d/1jsaXush5hD2rlyhzA0VaB8LR1eRb1h9mJOphTwCgeiU/edit#gid=1277242299")
  # d = gs_read(gs)
  #new package
  # d = read_sheet("https://docs.google.com/spreadsheets/d/1jsaXush5hD2rlyhzA0VaB8LR1eRb1h9mJOphTwCgeiU/edit#gid=1277242299")
  #local copy
  # d = read_excel("data/this survey.xlsx")
  d = read_excel("data/this survey 2020-03-01.xlsx")
  
  #dst data
  folk1a_2017q2 = read_excel("data/FOLK1A, 2017Q2.xlsx", range = "B3:D130") %>% 
    set_colnames(c("age", "men", "women")) %>% 
    mutate(age = age %>% str_replace(" år", "") %>% as.numeric(),
           total = men + women)
  HFUDD10_2017 = read_excel("data/HFUDD10 2017.xlsx", range = "B3:J12")
  names(HFUDD10_2017)[1] = "Age"
})

#Extra Muslim data
#a few interpolated values for Muslim % for older countries
kirk17 = read_rds("data/Kirkegaard 2017.rds")

#UK data from Carl 2016
noah16 = readxl::read_xlsx("data/Immigrant Attitudes Crime.xlsx") %>% 
  #improve column names
  df_legalize_names() %>% 
  #rename/transform to UK variants
  mutate(
    Net_opposition_UK = Net_opposition,
    crime_rate_UK = c(standardize(Log_crime_rate) + standardize(Log_incarceration_rate)) %>% standardize(),
    ISO = pu_translate(Country)
  )
## New names:
## * `` -> ...23
## * `` -> ...24

Rename, recode, restructure

#controls
control_items = str_detect(names(d), "Vælg ") %>% which()

#subset, rename, score
d_controls = d[control_items]
d_controls[[5]] = d_controls[[5]] %in% 60:70
d_controls %<>% score_items(c("Samme antal indvandrere", "Færre indvandrere", "Negativt", "Positivt", T))
names(d_controls) = "control_" + 1:5
d_controls %<>% map_df(as.logical)

#prefs
pref_items = str_detect(names(d), "det samme antal indvandrere") %>% which() %>% setdiff(control_items)
econ_items = str_detect(names(d), "indvandrergrupper i Danmark bidrager til fælleskassen") %>% which() %>% setdiff(control_items)

#subset
d_prefs = d[pref_items]
d_econ = d[econ_items]

#sort variables by name function
df_sort_vars = function(x) {
  x[sort(names(x))]
}

#rename, and to ISO-3
names(d_prefs) = str_match(names(d_prefs), "^[^:]+") %>% as.vector() %>% pu_translate()
names(d_econ) = str_match(names(d_econ), "^[^:]+") %>% as.vector() %>% pu_translate()
d_prefs %<>% df_sort_vars()
d_econ %<>% df_sort_vars()

#party agreements
party_items = str_detect(names(d), "med hvert politiske parti") %>% which() %>% setdiff(control_items)
d_parties = d[party_items]
names(d_parties) = str_match(names(d_parties), "^[^:]+")

#more rename
d$postcode = d$`URL Variable: pc` %>% as.numeric()
d$education = d$`Hvad er din højeste uddannelse?` %>% 
  mapvalues(c("Vil ikke oplyse", "0", "Anden uddannelse", "Folkeskole (til og med 10 klasse/real)"),
            c(NA, "Grundskole", NA, "Grundskole"), warn_missing = F) %>% 
  ordered(levels = c("Grundskole",
                     "Gymnasial uddannelse (Alm. gymnasium/HF/HH/HGX/HTX)",
                     "Erhvervsuddannelser (HG, EFG, mesterlære m.v.)",
                     "Højere uddannelse af kort varighed (under 3 år)",
                     "Højere uddannelse af mellemlang varighed (3-4 år)",
                     "Højere uddannelse af lang varighed (5 år eller derover)"))
d$education_num = d$education %>% as.numeric()

Quality control

#no variance in estimates
d_controls$control_no_var = map_lgl(1:nrow(d), function(x) {
  d[x, econ_items] %>% unlist %>% all_the_same() %>% not()
})

#QC relations
polycor::hetcor(d_controls %>% as.data.frame())
## 
## Two-Step Estimates
## 
## Correlations/Type of Correlation:
##                control_1  control_2  control_3  control_4  control_5
## control_1              1 Polychoric Polychoric Polychoric Polychoric
## control_2          0.517          1 Polychoric Polychoric Polychoric
## control_3          0.621      0.709          1 Polychoric Polychoric
## control_4          0.693      0.759       0.91          1 Polychoric
## control_5          0.574      0.442      0.599      0.674          1
## control_no_var     0.226      0.417      0.547      0.604      0.215
##                control_no_var
## control_1          Polychoric
## control_2          Polychoric
## control_3          Polychoric
## control_4          Polychoric
## control_5          Polychoric
## control_no_var              1
## 
## Standard Errors:
##                control_1 control_2 control_3 control_4 control_5
## control_1                                                       
## control_2         0.0609                                        
## control_3         0.0504    0.0422                              
## control_4         0.0454    0.0383    0.0187                    
## control_5         0.0524    0.0587    0.0462    0.0423          
## control_no_var    0.0846    0.0722    0.0601    0.0568    0.0751
## 
## n = 850
#QC scores
QC_score = rowSums(d_controls)
QC_score5 = rowSums(d_controls[-6])

#for each criterion
colMeans(d_controls)
##      control_1      control_2      control_3      control_4      control_5 
##          0.868          0.854          0.798          0.826          0.744 
## control_no_var 
##          0.904
#overall distribution of QC scores
table2(QC_score)
#if we ignore the 6th
table2(QC_score5)
#percent who was OK with first 5 who failed the 6th
table(QC_score5, d_controls$control_no_var)
##          
## QC_score5 FALSE TRUE
##         0     8   10
##         1    17   45
##         2    15   47
##         3     7   40
##         4     6  150
##         5    29  476
#keep origs
origs = list(
  d_econ = d_econ,
  d_prefs = d_prefs,
  d = d
)

#subset
d_econ = d_econ[QC_score == 6, ]
d_prefs = d_prefs[QC_score == 6, ]
d = d[QC_score == 6, ]

Subset and more recoding

#recode
d_econ_num = d_econ %>%  map_df(~plyr::mapvalues(., from = c("Meget negativt", "Negativt", "Lidt negativt", "Neutralt", "Lidt positivt", "Positivt", "Meget positivt"), to = 1:7, warn_missing = F) %>% as.numeric())
d_prefs_num = d_prefs %>%  map_df(~plyr::mapvalues(., from = c("Ingen indvandrere", "Færre indvandrere", "Samme antal indvandrere", "Flere indvandrere"), to = c(1, 1, -1, -1), warn_missing = F) %>% as.numeric())
d_prefs_num_alt = d_prefs %>% map_df(~plyr::mapvalues(., from = c("Ingen indvandrere", "Færre indvandrere", "Samme antal indvandrere", "Flere indvandrere"), to = 1:4, warn_missing = F) %>% as.numeric())

#basic
d$gender = d$`URL Variable: gender` %>% plyr::mapvalues(c(1, 2), to = c("Male", "Female")) %>% factor(levels = c("Male", "Female"))
d$age = d$`URL Variable: yob` %>% subtract(2017) %>% abs()

#aux
d$Muslims_are_treated_well = d[[90]]
d$DK_admit_only_net_positive_immigrants = d[[91]]
d$nonwesterns_are_net_positive = d[[92]]
d$party_vote = d[[89]] %>%
  mapvalues(from = c("Stemme blankt", "Vil ikke stemme"),
            to = c("Vote blank", "Would not vote")) %>% 
  fct_relevel("Socialdemokraterne")

#binary vote split, to boost power
d$block_vote = d$party_vote %>% 
  as.character() %>% 
  mapvalues(
    from = c("Socialdemokraterne", "Alternativet", "Enhedslisten", "Radikale Venstre", "Socialistisk Folkeparti", "Dansk Folkeparti", "Konservative Folkeparti", "Kristendemokraterne", "Liberal Alliance", "Nye Borgerlige", "Venstre"),
    to = c(rep("left-block", 5), rep("right-block", 6))
  ) %>% factor() %>% fct_relevel("right-block")
d$block_vote %>% table2()
#parties
d_parties = d[str_detect(names(d), "hvor 0 helt uenig, og 100 er helt enig") %>% which() %>% setdiff(control_items)]
names(d_parties) = str_match(names(d_parties), "[^:]+") %>% as.vector() %>% str_replace_all(" ", "_")
d = cbind(d, d_parties)

#recode path
d$path_parsed = plyr::alply(d$Path, .margins = 1, function(x) {
  orders = c(A = str_locate(x, "9")[1], B = str_locate(x, "10")[1], C = str_locate(x, "11")[1])
  str_c(names(sort(orders)), collapse = "")
}) %>% unlist()

#recode collection date
d$collected = d$`Date Submitted` %>% parse_date(format = "%d %B %Y %H:%M:%S")

#better country names
dk_fiscal$Names = pu_translate(dk_fiscal$ISO, reverse = T)

#add Muslim estimates for a few ex-countries
dk_fiscal[, c("Names", "Muslim_frac")] %>% .[miss_by_case(.) == 1, ]
#join
dk_fiscal = left_join(dk_fiscal, kirk17[c("ISO", "Muslim")], by = "ISO")

#assert no missing
dk_fiscal[, c("Names", "Muslim")] %>% .[miss_by_case(.) == 1, ] %>% nrow() %equals% 0 %>% assert_that()
## [1] TRUE
#subset of countries included in the new survey
dk_fiscal_sub = dk_fiscal %>% filter(!is.na(dk_fiscal))

#exclude Syria (recode to NA)
assert_that(dk_fiscal_sub$ISO[26] == "SYR")
## [1] TRUE
#function to do it on the fly
no_SYR = function(x) {
  if (is.data.frame(x)) {
    x[26, ] = NA
    return(x)
  }
  
  x[26] = NA
  x
}

#sort dk_fiscal
dk_fiscal_sub = dplyr::arrange(dk_fiscal_sub, ISO)

#assert same order
assert_that(names(d_econ) %equals% names(d_prefs))
## [1] TRUE
assert_that(names(d_econ) %equals% dk_fiscal_sub$ISO)
## [1] TRUE
#aggregate
dk_fiscal_sub$mean_estimate_fiscal = colMeans(d_econ_num)

#aggregate prefs
dk_fiscal_sub$net_opposition = colMeans(d_prefs_num)
dk_fiscal_sub$mean_pref = colMeans(d_prefs_num_alt)

#split half to partially avoid the method variance
set.seed(1)
split = sample(c(T, F), size = nrow(d), replace = T)
dk_fiscal_sub$net_opposition_split1 = colMeans(d_prefs_num[split, ])
dk_fiscal_sub$net_opposition_split2 = colMeans(d_prefs_num[!split, ])
dk_fiscal_sub$mean_estimate_fiscal_split1 = colMeans(d_econ_num[split, ])
dk_fiscal_sub$mean_estimate_fiscal_split2 = colMeans(d_econ_num[!split, ])

Reanalysis of Kirkegaard & Bjerrekær 2016

Determine the aggregate accuracy in the subset of 32 countries used for the present study.

#correlation between outcomes
GG_scatter(dk_fiscal_sub, "dk_benefits_use", "dk_fiscal", case_names = "Names") +
  xlab("Benefits use in Denmark") + 
  ylab("Net fiscal contribution in Denmark")
## `geom_smooth()` using formula 'y ~ x'

GG_scatter(dk_fiscal_sub %>% no_SYR(), "dk_benefits_use", "dk_fiscal", case_names = "Names") +
  xlab("Benefits use in Denmark") + 
  ylab("Net fiscal contribution in Denmark") +
  labs(caption = "Syria excluded")
## `geom_smooth()` using formula 'y ~ x'

#stereotype accuracy in reduced sample
GG_scatter(dk_fiscal, "dk_benefits_use", "mean_estimate", case_names = "Names") +
  xlab("Benefits use in Denmark") + 
  ylab("Mean estimate of benefits use in Denmark")
## `geom_smooth()` using formula 'y ~ x'

GG_scatter(dk_fiscal_sub, "dk_benefits_use", "mean_estimate", case_names = "Names") +
  xlab("Benefits use in Denmark") + 
  ylab("Mean estimate of benefits use in Denmark")
## `geom_smooth()` using formula 'y ~ x'

#cross-accuracy
GG_scatter(dk_fiscal, "dk_fiscal", "mean_estimate", case_names = "Names") +
  xlab("Net fiscal contribution") + 
  ylab("Mean estimate of benefits use in Denmark")
## `geom_smooth()` using formula 'y ~ x'

GG_scatter(dk_fiscal %>% filter(ISO != "SYR"), "dk_fiscal", "mean_estimate", case_names = "Names") +
  xlab("Net fiscal contribution") + 
  ylab("Mean estimate of benefits use in Denmark") +
  labs(caption = "Syria excluded")
## `geom_smooth()` using formula 'y ~ x'

#compare Muslim bias measures
#can we make a bias measure that works without ratio scale data?
#estimtes df
kb16_ests = kb16 %>% select(Afghanistan:Østrig)

#to ISO
colnames(kb16_ests) = pu_translate(colnames(kb16_ests))
## No exact match: Bosnien Hercegovina
## Best fuzzy match found: Bosnien Hercegovina -> Bosnien-Hercegovina with distance 1.00
#reorder cols
kb16_ests = kb16_ests[sort(colnames(kb16_ests))]

#assert the order is the same
assert_that(all(colnames(kb16_ests) == dk_fiscal$ISO))
## [1] TRUE
#calculate new metrics
muslim_bias_scalefree = plyr::adply(kb16_ests, .margins = 1, function(r) {
  d = tibble(
    #names
    name = dk_fiscal$Names,
    
    #extract estimates
    ests = r %>% unlist() %>% as.vector(),
    
    #standardize within person
    #this prevents elevation effects
    ests_z = ests %>% standardize(),
    
    #get outcomes
    true_values = dk_fiscal$dk_benefits_use,
    true_values_z = true_values %>% standardize(),
    
    #Muslim, frac
    muslim_pct = dk_fiscal$Muslim
  )
  
  #regress
  fit = lm(true_values_z ~ ests_z, data = d)
  
  #add more vars
  d %<>% mutate(
    #save resids, standardized
    resids_z = fit %>% resid() %>% standardize()
  )
  
  # browser()
  
  #return
  data.frame(
    #resid z x muslim
    muslim_resid_r = wtd.cors(d$resids_z, d$muslim_pct) %>% as.vector() %>% `*`(-1),
    
    #note that abs must be taken from the resids
    muslim_resid_r_abs = wtd.cors(abs(d$resids_z), d$muslim_pct) %>% as.vector() %>% `*`(-1),
    
    #mean methods
    wtd_mean_resid_muslim = wtd_mean(d$resids_z, d$muslim_pct) %>% `*`(-1),
    wtd_mean_resid_muslim_abs = wtd_mean(abs(d$resids_z), d$muslim_pct) %>% `*`(-1)
  )
  
}, .expand = F)

#add to main data
kb16 = cbind(
  kb16,
  muslim_bias_scalefree[-1]
)

#person-level Muslim bias metrics
muslim_bias_vars = c("muslim_resid_r",
                "wtd_mean_resid_muslim",
                "muslim_elevation_error",
                "muslim_error_r",
                
                #abs versions
                "muslim_resid_r_abs",
                "wtd_mean_resid_muslim_abs",
                "muslim_elevation_error_abs",
                "muslim_error_abs_r"
                )

#correlations
wtd.cors(kb16[muslim_bias_vars])
##                            muslim_resid_r wtd_mean_resid_muslim
## muslim_resid_r                     1.0000                1.0000
## wtd_mean_resid_muslim              1.0000                1.0000
## muslim_elevation_error             0.7105                0.7105
## muslim_error_r                     0.6450                0.6450
## muslim_resid_r_abs                 0.7508                0.7508
## wtd_mean_resid_muslim_abs          0.7802                0.7802
## muslim_elevation_error_abs        -0.0673               -0.0673
## muslim_error_abs_r                -0.0890               -0.0890
##                            muslim_elevation_error muslim_error_r
## muslim_resid_r                              0.710          0.645
## wtd_mean_resid_muslim                       0.710          0.645
## muslim_elevation_error                      1.000          0.964
## muslim_error_r                              0.964          1.000
## muslim_resid_r_abs                          0.497          0.442
## wtd_mean_resid_muslim_abs                   0.523          0.466
## muslim_elevation_error_abs                 -0.101         -0.262
## muslim_error_abs_r                         -0.274         -0.427
##                            muslim_resid_r_abs wtd_mean_resid_muslim_abs
## muslim_resid_r                         0.7508                    0.7802
## wtd_mean_resid_muslim                  0.7508                    0.7802
## muslim_elevation_error                 0.4970                    0.5228
## muslim_error_r                         0.4417                    0.4660
## muslim_resid_r_abs                     1.0000                    0.9896
## wtd_mean_resid_muslim_abs              0.9896                    1.0000
## muslim_elevation_error_abs            -0.0702                   -0.0841
## muslim_error_abs_r                    -0.0880                   -0.1018
##                            muslim_elevation_error_abs muslim_error_abs_r
## muslim_resid_r                                -0.0673             -0.089
## wtd_mean_resid_muslim                         -0.0673             -0.089
## muslim_elevation_error                        -0.1009             -0.274
## muslim_error_r                                -0.2625             -0.427
## muslim_resid_r_abs                            -0.0702             -0.088
## wtd_mean_resid_muslim_abs                     -0.0841             -0.102
## muslim_elevation_error_abs                     1.0000              0.948
## muslim_error_abs_r                             0.9480              1.000
#subset
muslim_bias_vars2 = c("wtd_mean_resid_muslim",
                      "muslim_error_r",
                      "wtd_mean_resid_muslim_abs",
                      "muslim_error_abs_r")



#useful predictors from previous study still useful?
muslim_bias_useful_preds = c("Nationalism", "Conservatism", "Muslim_problem", "Future_perf_immi")

cor_matrix(kb16[c(muslim_bias_vars2, muslim_bias_useful_preds)], CI = .95) %>%
  .[muslim_bias_vars2, muslim_bias_useful_preds]
##                           Nationalism           Conservatism        
## wtd_mean_resid_muslim     "0.12 [0.04 0.21]"    "0.15 [0.06 0.23]"  
## muslim_error_r            "0.30 [0.21 0.38]"    "0.18 [0.09 0.27]"  
## wtd_mean_resid_muslim_abs "0.07 [-0.02 0.16]"   "0.13 [0.04 0.22]"  
## muslim_error_abs_r        "-0.12 [-0.21 -0.03]" "-0.08 [-0.17 0.01]"
##                           Muslim_problem        Future_perf_immi     
## wtd_mean_resid_muslim     "0.14 [0.05 0.23]"    "-0.14 [-0.22 -0.05]"
## muslim_error_r            "0.32 [0.24 0.40]"    "-0.31 [-0.39 -0.23]"
## wtd_mean_resid_muslim_abs "0.09 [0.00 0.18]"    "-0.13 [-0.22 -0.04]"
## muslim_error_abs_r        "-0.14 [-0.23 -0.05]" "0.13 [0.04 0.21]"
#sample size for cor matrix
kb16 %>% nrow()
## [1] 484

Aggregate

Descriptive

#plot contributions by origin
ggplot(dk_fiscal_sub, aes(fct_reorder(Names, dk_fiscal), dk_fiscal * 1000 / 7.45, fill = Muslim)) +
  geom_bar(stat = "identity") +
  scale_x_discrete("Country of origin") +
  scale_y_continuous("Net fiscal contribution, 2014, in EUR/person/year", labels = scales::comma) +
  scale_fill_gradient("Muslim%", label = scales::percent, low = "darkblue", high = "green", limits = c(0, 1)) +
  theme(axis.text.x = element_text(angle = -30, hjust = 0))

GG_save("figures/fiscal_estimates.png")

Accuracy

#test-retest of aggr stereotypes
GG_scatter(dk_fiscal_sub, "mean_estimate_fiscal", "mean_estimate", case_names = "Names", repel_names = T) +
  xlab("Estimate of net fiscal contribution") + 
  ylab("Estimate of % of 30-39 year olds\non unemployment benefit")
## `geom_smooth()` using formula 'y ~ x'

GG_save("figures/aggr_retest_stereotypes.png")
## `geom_smooth()` using formula 'y ~ x'
#without SYR
GG_scatter(dk_fiscal_sub %>% no_SYR(), "mean_estimate_fiscal", "mean_estimate", case_names = "Names", repel_names = T) +
  xlab("Estimate of net fiscal contribution") + 
  ylab("Estimate of % of 30-39 year olds\non unemployment benefit") +
  labs(caption = "Syria excluded")
## `geom_smooth()` using formula 'y ~ x'

GG_save("figures/aggr_retest_stereotypes_noSYR.png")
## `geom_smooth()` using formula 'y ~ x'
#stereotype accuracy aggregate
GG_scatter(dk_fiscal_sub, "dk_fiscal", "mean_estimate_fiscal", case_names = "Names", repel_names = T) +
  scale_y_continuous("Mean estimate of net fiscal contribution of group") +
  scale_x_continuous("Group's net fiscal contribution (EUR/person/year)\nDanish Ministry of Finance estimate")
## `geom_smooth()` using formula 'y ~ x'

GG_save("figures/aggregate_accuracy.png")
## `geom_smooth()` using formula 'y ~ x'
#without SYR
dk_fiscal_sub %>% no_SYR() %>% 
  GG_scatter("dk_fiscal", "mean_estimate_fiscal", case_names = "Names", repel_names = T) +
  scale_y_continuous("Mean estimate of net fiscal contribution of group") +
  scale_x_continuous("Group's net fiscal contribution\nDanish Ministry of Finance estimate") +
  labs(caption = "Syria excluded")
## `geom_smooth()` using formula 'y ~ x'

GG_save("figures/aggregate_accuracy_no_SYR.png")
## `geom_smooth()` using formula 'y ~ x'
#test
cor.test(dk_fiscal_sub$dnk_net_fiscal_contribution_2014, dk_fiscal_sub$mean_estimate_fiscal)
## 
##  Pearson's product-moment correlation
## 
## data:  dk_fiscal_sub$dnk_net_fiscal_contribution_2014 and dk_fiscal_sub$mean_estimate_fiscal
## t = 7, df = 30, p-value = 3e-08
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.637 0.902
## sample estimates:
##   cor 
## 0.806
cor.test(dk_fiscal_sub$dnk_net_fiscal_contribution_2015, dk_fiscal_sub$mean_estimate_fiscal)
## 
##  Pearson's product-moment correlation
## 
## data:  dk_fiscal_sub$dnk_net_fiscal_contribution_2015 and dk_fiscal_sub$mean_estimate_fiscal
## t = 9, df = 30, p-value = 2e-09
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.698 0.920
## sample estimates:
##   cor 
## 0.842

Muslim bias

#model
(aggregate_accuracy_fit = lm(dk_fiscal ~ mean_estimate_fiscal, data = dk_fiscal_sub, na.action = na.exclude) %>% MOD_summary())
## 
##     ---- Model summary ----    
## Model coefficients
##                       Beta    SE CI_lower CI_upper
## mean_estimate_fiscal 0.806 0.108    0.586     1.03
## 
## 
## Model meta-data
##     outcome  N df   R2 R2-adj. R2-cv
## 1 dk_fiscal 32 30 0.65   0.639    NA
## 
## 
## Etas from analysis of variance
##                        Eta Eta_partial
## mean_estimate_fiscal 0.806       0.806
(aggregate_accuracy_fit_noSYR = lm(dk_fiscal ~ mean_estimate_fiscal, data = dk_fiscal_sub %>% no_SYR(), na.action = na.exclude) %>% MOD_summary())
## 
##     ---- Model summary ----    
## Model coefficients
##                       Beta     SE CI_lower CI_upper
## mean_estimate_fiscal 0.847 0.0986    0.645     1.05
## 
## 
## Model meta-data
##     outcome  N df    R2 R2-adj. R2-cv
## 1 dk_fiscal 31 29 0.718   0.708    NA
## 
## 
## Etas from analysis of variance
##                        Eta Eta_partial
## mean_estimate_fiscal 0.847       0.847
#slopes
ols(dk_fiscal ~ mean_estimate_fiscal, data = dk_fiscal_sub)
## Linear Regression Model
##  
##  ols(formula = dk_fiscal ~ mean_estimate_fiscal, data = dk_fiscal_sub)
##  
##                  Model Likelihood     Discrimination    
##                     Ratio Test           Indexes        
##  Obs       32    LR chi2     33.62    R2       0.650    
##  sigma47.4278    d.f.            1    R2 adj   0.639    
##  d.f.      30    Pr(> chi2) 0.0000    g       73.819    
##  
##  Residuals
##  
##       Min       1Q   Median       3Q      Max 
##  -177.478  -13.408    5.013   21.852   94.585 
##  
##  
##                       Coef      S.E.    t     Pr(>|t|)
##  Intercept            -241.9446 31.0867 -7.78 <0.0001 
##  mean_estimate_fiscal   58.1153  7.7817  7.47 <0.0001 
## 
ols(dk_fiscal ~ mean_estimate_fiscal, data = dk_fiscal_sub %>% no_SYR())
## Frequencies of Missing Values Due to Each Variable
##            dk_fiscal mean_estimate_fiscal 
##                    1                    1 
## 
## Linear Regression Model
##  
##  ols(formula = dk_fiscal ~ mean_estimate_fiscal, data = dk_fiscal_sub %>% 
##      no_SYR())
##  
##  
##                  Model Likelihood     Discrimination    
##                     Ratio Test           Indexes        
##  Obs       31    LR chi2     39.22    R2       0.718    
##  sigma33.2572    d.f.            1    R2 adj   0.708    
##  d.f.      29    Pr(> chi2) 0.0000    g       60.452    
##  
##  Residuals
##  
##      Min      1Q  Median      3Q     Max 
##  -55.357 -23.245  -2.337  20.048  76.975 
##  
##  
##                       Coef      S.E.    t     Pr(>|t|)
##  Intercept            -200.2897 23.0083 -8.71 <0.0001 
##  mean_estimate_fiscal   48.9076  5.6942  8.59 <0.0001 
## 
#save resids
dk_fiscal_sub$aggr_model_resid = resid(aggregate_accuracy_fit$model_obj) %>% standardize()
dk_fiscal_sub$aggr_model_resid_noSYR = resid(aggregate_accuracy_fit_noSYR$model_obj) %>% standardize()

#plot vs Muslim
dk_fiscal_sub %>% 
  GG_scatter("Muslim", "aggr_model_resid", case_names = "Names", repel_names = T) +
  scale_x_continuous("Muslim population (%)", labels = scales::percent) +
  scale_y_continuous("Stereotype estimation error\nNet fiscal contribution")
## `geom_smooth()` using formula 'y ~ x'

GG_save("figures/aggregate_muslim_bias.png")
## `geom_smooth()` using formula 'y ~ x'
dk_fiscal_sub %>% 
  GG_scatter("Muslim", "aggr_model_resid_noSYR", case_names = "Names", repel_names = T) +
  scale_x_continuous("Muslim population (%)", labels = scales::percent) +
  scale_y_continuous("Stereotype estimation error\nNet fiscal contribution") +
  labs(caption = "Syria excluded")
## `geom_smooth()` using formula 'y ~ x'

GG_save("figures/aggregate_muslim_bias_noSYR.png")
## `geom_smooth()` using formula 'y ~ x'
#old data model
(aggregate_accuracy_fit_old = lm(dk_benefits_use ~ mean_estimate, data = dk_fiscal_sub, na.action = na.exclude) %>% MOD_summary())
## 
##     ---- Model summary ----    
## Model coefficients
##               Beta    SE CI_lower CI_upper
## mean_estimate 0.84 0.099    0.638     1.04
## 
## 
## Model meta-data
##           outcome  N df    R2 R2-adj. R2-cv
## 1 dk_benefits_use 32 30 0.706   0.696    NA
## 
## 
## Etas from analysis of variance
##                Eta Eta_partial
## mean_estimate 0.84        0.84
(aggregate_accuracy_fit_old_all = lm(dk_benefits_use ~ mean_estimate, data = dk_fiscal, na.action = na.exclude) %>% MOD_summary())
## 
##     ---- Model summary ----    
## Model coefficients
##                Beta     SE CI_lower CI_upper
## mean_estimate 0.696 0.0871    0.522     0.87
## 
## 
## Model meta-data
##           outcome  N df    R2 R2-adj. R2-cv
## 1 dk_benefits_use 70 68 0.484   0.477    NA
## 
## 
## Etas from analysis of variance
##                 Eta Eta_partial
## mean_estimate 0.696       0.696
#save resids
#multiply by -1 because the outcome is reverse in desirability
dk_fiscal_sub$aggr_model_resid_old = resid(aggregate_accuracy_fit_old$model_obj) %>% standardize() %>% `*`(-1)
dk_fiscal$aggr_model_resid_old = resid(aggregate_accuracy_fit_old_all$model_obj) %>% standardize() %>% `*`(-1)

#old data, same countries, new metric
dk_fiscal_sub %>% 
  GG_scatter("Muslim", "aggr_model_resid_old", case_names = "Names", repel_names = T, text_pos = "tr") +
  scale_x_continuous("Muslim population (%)", labels = scales::percent) +
  scale_y_continuous("Stereotype residual")
## `geom_smooth()` using formula 'y ~ x'

GG_save("figures/aggregate_muslim_bias_old_data_new_metric.png")
## `geom_smooth()` using formula 'y ~ x'
#old data, all countries, new metric
dk_fiscal %>% 
  GG_scatter("Muslim", "aggr_model_resid_old", case_names = "Names", repel_names = T) +
  scale_x_continuous("Muslim population (%)", labels = scales::percent) +
  scale_y_continuous("Stereotype residual")
## `geom_smooth()` using formula 'y ~ x'

GG_save("figures/aggregate_muslim_bias_old_data_new_metric_all_countries.png")
## `geom_smooth()` using formula 'y ~ x'
#old data, same countries, old metric
dk_fiscal_sub %>% 
  GG_scatter("Muslim", "delta_est", case_names = "Names", repel_names = T) +
  scale_x_continuous("Muslim population (%)", labels = scales::percent) +
  scale_y_continuous("Stereotype residual")
## `geom_smooth()` using formula 'y ~ x'

GG_save("figures/aggregate_muslim_bias_old_data.png")
## `geom_smooth()` using formula 'y ~ x'
#old data, all countries, old metric
dk_fiscal %>% 
  GG_scatter("Muslim", "delta_est", case_names = "Names", repel_names = T) +
  scale_x_continuous("Muslim population (%)", labels = scales::percent) +
  scale_y_continuous("Stereotype residual")
## `geom_smooth()` using formula 'y ~ x'

GG_save("figures/aggregate_muslim_bias_old_data_all.png")
## `geom_smooth()` using formula 'y ~ x'
#cors
(subset_cors = dk_fiscal_sub %>% {cor_matrix(.[c("Muslim", "aggr_model_resid", "aggr_model_resid_old", "delta_est", "aggr_model_resid_noSYR")], CI = .95)})
##                        Muslim                aggr_model_resid    
## Muslim                 "1"                   "-0.25 [-0.55 0.11]"
## aggr_model_resid       "-0.25 [-0.55 0.11]"  "1"                 
## aggr_model_resid_old   "-0.11 [-0.44 0.25]"  "0.62 [0.34 0.79]"  
## delta_est              "-0.39 [-0.65 -0.05]" "0.65 [0.40 0.82]"  
## aggr_model_resid_noSYR "-0.30 [-0.59 0.06]"  "0.96 [0.91 0.98]"  
##                        aggr_model_resid_old delta_est            
## Muslim                 "-0.11 [-0.44 0.25]" "-0.39 [-0.65 -0.05]"
## aggr_model_resid       "0.62 [0.34 0.79]"   "0.65 [0.40 0.82]"   
## aggr_model_resid_old   "1"                  "0.94 [0.88 0.97]"   
## delta_est              "0.94 [0.88 0.97]"   "1"                  
## aggr_model_resid_noSYR "0.80 [0.63 0.90]"   "0.82 [0.65 0.91]"   
##                        aggr_model_resid_noSYR
## Muslim                 "-0.30 [-0.59 0.06]"  
## aggr_model_resid       "0.96 [0.91 0.98]"    
## aggr_model_resid_old   "0.80 [0.63 0.90]"    
## delta_est              "0.82 [0.65 0.91]"    
## aggr_model_resid_noSYR "1"
(all_cors = dk_fiscal %>% {cor_matrix(.[c("Muslim", "aggr_model_resid_old", "delta_est")], CI = .95)})
##                      Muslim                aggr_model_resid_old 
## Muslim               "1"                   "-0.27 [-0.48 -0.04]"
## aggr_model_resid_old "-0.27 [-0.48 -0.04]" "1"                  
## delta_est            "-0.34 [-0.53 -0.12]" "0.99 [0.99 1.00]"   
##                      delta_est            
## Muslim               "-0.34 [-0.53 -0.12]"
## aggr_model_resid_old "0.99 [0.99 1.00]"   
## delta_est            "1"
#table
tibble(
  data = c("new", "new", "old", "old", "old", "old"),
  n = c(32, 31, 32, 70, 32, 70),
  metric = c("Muslim resid r", "Muslim resid r", "Muslim resid r", "Muslim resid r", "Muslim error r", "Muslim error r"),
  value = c(
    subset_cors[1, 2],
    subset_cors[1, 5],
    subset_cors[1, 3],
    all_cors[1, 2],
    subset_cors[1, 4],
    all_cors[1, 3]
  )
)

Immigrant preferences and stereotypes

#fiscal x prefs
GG_scatter(dk_fiscal_sub, "dk_fiscal", "net_opposition", case_names = "Names", repel_names = T) +
  scale_x_continuous("Net fiscal contribution") +
  scale_y_continuous("Net opposition")
## `geom_smooth()` using formula 'y ~ x'

GG_save("figures/aggr_fiscal_net_opposition.png")
## `geom_smooth()` using formula 'y ~ x'
dk_fiscal_sub %>% 
  filter(ISO != "SYR") %>% 
  GG_scatter("dk_fiscal", "net_opposition", case_names = "Names", repel_names = T) +
  scale_x_continuous("Net fiscal contribution") +
  scale_y_continuous("Net opposition") +
  labs(caption = "Syria excluded")
## `geom_smooth()` using formula 'y ~ x'

GG_save("figures/aggr_fiscal_net_opposition_no_SYR.png")
## `geom_smooth()` using formula 'y ~ x'
#stereotypes x prefs
GG_scatter(dk_fiscal_sub, "mean_estimate_fiscal", "net_opposition", case_names = "Names", repel_names = T) +
  scale_x_continuous("Mean estimate of net fiscal contribution") +
  scale_y_continuous("Net opposition")
## `geom_smooth()` using formula 'y ~ x'

GG_save("figures/aggr_stereotype_net_opposition.png")
## `geom_smooth()` using formula 'y ~ x'
dk_fiscal_sub %>% 
  filter(ISO != "SYR") %>% 
  GG_scatter("mean_estimate_fiscal", "net_opposition", case_names = "Names", repel_names = T) +
  scale_x_continuous("Mean estimate of net fiscal contribution") +
  scale_y_continuous("Net opposition") +
  labs(caption = "Syria excluded")
## `geom_smooth()` using formula 'y ~ x'

GG_save("figures/aggr_stereotype_net_opposition_no_SYR.png")
## `geom_smooth()` using formula 'y ~ x'
#path model
path_model = "net_opposition ~ mean_estimate_fiscal + dk_fiscal + Muslim
mean_estimate_fiscal ~ dk_fiscal
Muslim ~~ dk_fiscal"

(path_fit = lavaan::sem(path_model, data = dk_fiscal_sub, std.ov = T))
## lavaan 0.6-5 ended normally after 45 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                          9
##                                                       
##   Number of observations                            32
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                 3.741
##   Degrees of freedom                                 1
##   P-value (Chi-square)                           0.053
(path_fit_noSYR = lavaan::sem(path_model, data = dk_fiscal_sub %>% no_SYR(), std.ov = T))
## lavaan 0.6-5 ended normally after 39 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                          9
##                                                       
##                                                   Used       Total
##   Number of observations                            31          32
##                                                                   
## Model Test User Model:
##                                                       
##   Test statistic                                 1.029
##   Degrees of freedom                                 1
##   P-value (Chi-square)                           0.310
fitmeasures(path_fit)
##                npar                fmin               chisq                  df 
##               9.000               0.058               3.741               1.000 
##              pvalue      baseline.chisq         baseline.df     baseline.pvalue 
##               0.053             170.438               6.000               0.000 
##                 cfi                 tli                nnfi                 rfi 
##               0.983               0.900               0.900               0.868 
##                 nfi                pnfi                 ifi                 rni 
##               0.978               0.163               0.984               0.983 
##                logl   unrestricted.logl                 aic                 bic 
##             -96.244             -94.373             210.487             223.679 
##              ntotal                bic2               rmsea      rmsea.ci.lower 
##              32.000             195.622               0.293               0.000 
##      rmsea.ci.upper        rmsea.pvalue                 rmr          rmr_nomean 
##               0.633               0.063               0.060               0.060 
##                srmr        srmr_bentler srmr_bentler_nomean                crmr 
##               0.062               0.062               0.062               0.079 
##         crmr_nomean          srmr_mplus   srmr_mplus_nomean               cn_05 
##               0.079               0.061               0.061              33.861 
##               cn_01                 gfi                agfi                pgfi 
##              57.757               0.948               0.477               0.095 
##                 mfi                ecvi 
##               0.958               0.679
fitmeasures(path_fit_noSYR)
##                npar                fmin               chisq                  df 
##               9.000               0.017               1.029               1.000 
##              pvalue      baseline.chisq         baseline.df     baseline.pvalue 
##               0.310             170.111               6.000               0.000 
##                 cfi                 tli                nnfi                 rfi 
##               1.000               0.999               0.999               0.964 
##                 nfi                pnfi                 ifi                 rni 
##               0.994               0.166               1.000               1.000 
##                logl   unrestricted.logl                 aic                 bic 
##             -89.375             -88.860             196.750             209.655 
##              ntotal                bic2               rmsea      rmsea.ci.lower 
##              31.000             181.616               0.031               0.000 
##      rmsea.ci.upper        rmsea.pvalue                 rmr          rmr_nomean 
##               0.477               0.329               0.028               0.028 
##                srmr        srmr_bentler srmr_bentler_nomean                crmr 
##               0.029               0.029               0.029               0.037 
##         crmr_nomean          srmr_mplus   srmr_mplus_nomean               cn_05 
##               0.037               0.029               0.029             116.691 
##               cn_01                 gfi                agfi                pgfi 
##             200.819               0.984               0.839               0.098 
##                 mfi                ecvi 
##               1.000               0.614
parameterestimates(path_fit, standardized = T)
parameterestimates(path_fit_noSYR, standardized = T)
#plot
lavaanPlot(model = path_fit, coefs = T, stand = T)
lavaanPlot(model = path_fit_noSYR, coefs = T, stand = T)
#simple modeling
MOD_summary(lm(net_opposition ~ mean_estimate_fiscal + dk_fiscal + Muslim, data = dk_fiscal_sub))
## 
##     ---- Model summary ----    
## Model coefficients
##                         Beta     SE CI_lower CI_upper
## mean_estimate_fiscal -1.0607 0.0619 -1.18754   -0.934
## dk_fiscal             0.1265 0.0626 -0.00167    0.255
## Muslim                0.0304 0.0535 -0.07927    0.140
## 
## 
## Model meta-data
##          outcome  N df    R2 R2-adj. R2-cv
## 1 net_opposition 32 28 0.967   0.963    NA
## 
## 
## Etas from analysis of variance
##                         Eta Eta_partial
## mean_estimate_fiscal 0.5917       0.955
## dk_fiscal            0.0698       0.357
## Muslim               0.0196       0.107
MOD_summary(lm(net_opposition ~ mean_estimate_fiscal + dk_fiscal + Muslim, data = dk_fiscal_sub %>% no_SYR()))
## 
##     ---- Model summary ----    
## Model coefficients
##                         Beta     SE CI_lower CI_upper
## mean_estimate_fiscal -1.0248 0.0686  -1.1656   -0.884
## dk_fiscal             0.0692 0.0742  -0.0830    0.221
## Muslim                0.0225 0.0551  -0.0906    0.136
## 
## 
## Model meta-data
##          outcome  N df    R2 R2-adj. R2-cv
## 1 net_opposition 31 27 0.965   0.961    NA
## 
## 
## Etas from analysis of variance
##                         Eta Eta_partial
## mean_estimate_fiscal 0.5354      0.9445
## dk_fiscal            0.0334      0.1766
## Muslim               0.0146      0.0783
#mediation models
#with SYR
mediation::mediate(
  lm(mean_estimate_fiscal ~ dk_fiscal, data = dk_fiscal_sub),
  lm(net_opposition ~ mean_estimate_fiscal + dk_fiscal, data = dk_fiscal_sub),
  treat = "dk_fiscal", mediator = "mean_estimate_fiscal"
  ) %>% summary()
## 
## Causal Mediation Analysis 
## 
## Quasi-Bayesian Confidence Intervals
## 
##                 Estimate 95% CI Lower 95% CI Upper p-value    
## ACME           -5.02e-03    -6.44e-03         0.00  <2e-16 ***
## ADE             6.55e-04     1.11e-05         0.00   0.048 *  
## Total Effect   -4.37e-03    -5.77e-03         0.00  <2e-16 ***
## Prop. Mediated  1.15e+00     1.00e+00         1.37  <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Sample Size Used: 32 
## 
## 
## Simulations: 1000
#without SYR
mediation::mediate(
  lm(mean_estimate_fiscal ~ dk_fiscal, data = dk_fiscal_sub %>% no_SYR()),
  lm(net_opposition ~ mean_estimate_fiscal + dk_fiscal, data = dk_fiscal_sub %>% no_SYR()),
  treat = "dk_fiscal", mediator = "mean_estimate_fiscal"
  ) %>% summary()
## 
## Causal Mediation Analysis 
## 
## Quasi-Bayesian Confidence Intervals
## 
##                 Estimate 95% CI Lower 95% CI Upper p-value    
## ACME           -0.006434    -0.008083         0.00  <2e-16 ***
## ADE             0.000433    -0.000568         0.00    0.38    
## Total Effect   -0.006001    -0.007416         0.00  <2e-16 ***
## Prop. Mediated  1.071389     0.912137         1.27  <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Sample Size Used: 31 
## 
## 
## Simulations: 1000
#cor matrix
cor_matrix(dk_fiscal_sub[c("dnk_net_fiscal_contribution_2014", "mean_estimate_fiscal", "Muslim", "net_opposition")],
           CI = .95)
##                                  dnk_net_fiscal_contribution_2014
## dnk_net_fiscal_contribution_2014 "1"                             
## mean_estimate_fiscal             "0.81 [0.64 0.90]"              
## Muslim                           "-0.73 [-0.86 -0.51]"           
## net_opposition                   "-0.75 [-0.87 -0.55]"           
##                                  mean_estimate_fiscal  Muslim               
## dnk_net_fiscal_contribution_2014 "0.81 [0.64 0.90]"    "-0.73 [-0.86 -0.51]"
## mean_estimate_fiscal             "1"                   "-0.72 [-0.86 -0.50]"
## Muslim                           "-0.72 [-0.86 -0.50]" "1"                  
## net_opposition                   "-0.98 [-0.99 -0.96]" "0.70 [0.47 0.85]"   
##                                  net_opposition       
## dnk_net_fiscal_contribution_2014 "-0.75 [-0.87 -0.55]"
## mean_estimate_fiscal             "-0.98 [-0.99 -0.96]"
## Muslim                           "0.70 [0.47 0.85]"   
## net_opposition                   "1"
cor_matrix(dk_fiscal_sub[c("dnk_net_fiscal_contribution_2014", "mean_estimate_fiscal", "Muslim", "net_opposition")] %>% no_SYR(),
           CI = .95)
##                                  dnk_net_fiscal_contribution_2014
## dnk_net_fiscal_contribution_2014 "1"                             
## mean_estimate_fiscal             "0.85 [0.70 0.92]"              
## Muslim                           "-0.75 [-0.87 -0.54]"           
## net_opposition                   "-0.82 [-0.91 -0.65]"           
##                                  mean_estimate_fiscal  Muslim               
## dnk_net_fiscal_contribution_2014 "0.85 [0.70 0.92]"    "-0.75 [-0.87 -0.54]"
## mean_estimate_fiscal             "1"                   "-0.70 [-0.84 -0.46]"
## Muslim                           "-0.70 [-0.84 -0.46]" "1"                  
## net_opposition                   "-0.98 [-0.99 -0.96]" "0.69 [0.44 0.84]"   
##                                  net_opposition       
## dnk_net_fiscal_contribution_2014 "-0.82 [-0.91 -0.65]"
## mean_estimate_fiscal             "-0.98 [-0.99 -0.96]"
## Muslim                           "0.69 [0.44 0.84]"   
## net_opposition                   "1"

Comparison with Carl 2016

Comparison with the UK findings.

#merge in UK data into main
dk_fiscal = full_join(dk_fiscal, noah16[c("ISO", "crime_rate_UK", "Log_incarceration_rate", "Log_crime_rate", "Net_opposition_UK")], by = "ISO")

#join DK prefs and accuracy into main
dk_fiscal = full_join(dk_fiscal, dk_fiscal_sub[c("ISO", "mean_estimate_fiscal", "net_opposition")], by = "ISO")

#correlations
dk_fiscal[c("dk_benefits_use", "dk_fiscal", "crime_rate_UK", "mean_estimate", "mean_estimate_fiscal", "net_opposition", "Net_opposition_UK", "Muslim")] %>% 
  set_colnames(c("DK: benefits use", "DK: net fiscal contribution", "UK: crime rate", 
"DK: mean estimate benefits", "DK: mean estimate fiscal", "DK: Net opposition", 
"UK: Net opposition", "Muslim% in home country")) %>% 
  cor_matrix()
##                             DK: benefits use DK: net fiscal contribution
## DK: benefits use                       1.000                      -0.893
## DK: net fiscal contribution           -0.893                       1.000
## UK: crime rate                         0.505                      -0.410
## DK: mean estimate benefits             0.696                      -0.891
## DK: mean estimate fiscal              -0.724                       0.806
## DK: Net opposition                     0.676                      -0.751
## UK: Net opposition                     0.601                      -0.849
## Muslim% in home country                0.680                      -0.729
##                             UK: crime rate DK: mean estimate benefits
## DK: benefits use                     0.505                      0.696
## DK: net fiscal contribution         -0.410                     -0.891
## UK: crime rate                       1.000                      0.696
## DK: mean estimate benefits           0.696                      1.000
## DK: mean estimate fiscal            -0.699                     -0.941
## DK: Net opposition                   0.728                      0.902
## UK: Net opposition                   0.678                      0.853
## Muslim% in home country              0.418                      0.695
##                             DK: mean estimate fiscal DK: Net opposition
## DK: benefits use                              -0.724              0.676
## DK: net fiscal contribution                    0.806             -0.751
## UK: crime rate                                -0.699              0.728
## DK: mean estimate benefits                    -0.941              0.902
## DK: mean estimate fiscal                       1.000             -0.981
## DK: Net opposition                            -0.981              1.000
## UK: Net opposition                            -0.949              0.966
## Muslim% in home country                       -0.723              0.705
##                             UK: Net opposition Muslim% in home country
## DK: benefits use                         0.601                   0.680
## DK: net fiscal contribution             -0.849                  -0.729
## UK: crime rate                           0.678                   0.418
## DK: mean estimate benefits               0.853                   0.695
## DK: mean estimate fiscal                -0.949                  -0.723
## DK: Net opposition                       0.966                   0.705
## UK: Net opposition                       1.000                   0.578
## Muslim% in home country                  0.578                   1.000
dk_fiscal %>% filter(ISO != "SYR") %>% .[, c("dk_benefits_use", "dk_fiscal", "crime_rate_UK", "mean_estimate", "mean_estimate_fiscal", "net_opposition", "Net_opposition_UK", "Muslim")] %>% 
  set_colnames(c("DK: benefits use", "DK: net fiscal contribution", "UK: crime rate", 
"DK: mean estimate benefits", "DK: mean estimate fiscal", "DK: Net opposition", 
"UK: Net opposition", "Muslim% in home country")) %>% 
  cor_matrix()
##                             DK: benefits use DK: net fiscal contribution
## DK: benefits use                       1.000                      -0.910
## DK: net fiscal contribution           -0.910                       1.000
## UK: crime rate                         0.505                      -0.410
## DK: mean estimate benefits             0.660                      -0.887
## DK: mean estimate fiscal              -0.695                       0.847
## DK: Net opposition                     0.658                      -0.816
## UK: Net opposition                     0.601                      -0.849
## Muslim% in home country                0.660                      -0.750
##                             UK: crime rate DK: mean estimate benefits
## DK: benefits use                     0.505                      0.660
## DK: net fiscal contribution         -0.410                     -0.887
## UK: crime rate                       1.000                      0.696
## DK: mean estimate benefits           0.696                      1.000
## DK: mean estimate fiscal            -0.699                     -0.946
## DK: Net opposition                   0.728                      0.918
## UK: Net opposition                   0.678                      0.853
## Muslim% in home country              0.418                      0.677
##                             DK: mean estimate fiscal DK: Net opposition
## DK: benefits use                              -0.695              0.658
## DK: net fiscal contribution                    0.847             -0.816
## UK: crime rate                                -0.699              0.728
## DK: mean estimate benefits                    -0.946              0.918
## DK: mean estimate fiscal                       1.000             -0.982
## DK: Net opposition                            -0.982              1.000
## UK: Net opposition                            -0.949              0.966
## Muslim% in home country                       -0.699              0.687
##                             UK: Net opposition Muslim% in home country
## DK: benefits use                         0.601                   0.660
## DK: net fiscal contribution             -0.849                  -0.750
## UK: crime rate                           0.678                   0.418
## DK: mean estimate benefits               0.853                   0.677
## DK: mean estimate fiscal                -0.949                  -0.699
## DK: Net opposition                       0.966                   0.687
## UK: Net opposition                       1.000                   0.578
## Muslim% in home country                  0.578                   1.000
#plot with UK net opposition
GG_scatter(dk_fiscal, "net_opposition", "Net_opposition_UK", case_names = "ISO") +
  xlab("Net opposition: Denmark") +
  ylab("Net opposition: UK") +
  ggtitle("Net opposition to immigrants by country of origin",
          "Danish data from study in review, UK data from YouGov")
## `geom_smooth()` using formula 'y ~ x'

GG_save("figures/net_opposition_DK_UK.png")
## `geom_smooth()` using formula 'y ~ x'

Method variance

#compare the split half means
dk_fiscal_sub %>% 
  select(net_opposition, mean_estimate_fiscal, net_opposition_split1:mean_estimate_fiscal_split2) %>% 
  wtd.cors()
##                             net_opposition mean_estimate_fiscal
## net_opposition                       1.000               -0.981
## mean_estimate_fiscal                -0.981                1.000
## net_opposition_split1                0.998               -0.977
## net_opposition_split2                0.998               -0.980
## mean_estimate_fiscal_split1         -0.978                0.999
## mean_estimate_fiscal_split2         -0.982                0.999
##                             net_opposition_split1 net_opposition_split2
## net_opposition                              0.998                 0.998
## mean_estimate_fiscal                       -0.977                -0.980
## net_opposition_split1                       1.000                 0.991
## net_opposition_split2                       0.991                 1.000
## mean_estimate_fiscal_split1                -0.975                -0.977
## mean_estimate_fiscal_split2                -0.977                -0.982
##                             mean_estimate_fiscal_split1
## net_opposition                                   -0.978
## mean_estimate_fiscal                              0.999
## net_opposition_split1                            -0.975
## net_opposition_split2                            -0.977
## mean_estimate_fiscal_split1                       1.000
## mean_estimate_fiscal_split2                       0.996
##                             mean_estimate_fiscal_split2
## net_opposition                                   -0.982
## mean_estimate_fiscal                              0.999
## net_opposition_split1                            -0.977
## net_opposition_split2                            -0.982
## mean_estimate_fiscal_split1                       0.996
## mean_estimate_fiscal_split2                       1.000

Individual

Descriptives

#Our sample
#age
GG_denhist(d$age) +
  xlab("Age")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

describe(d$age) %>% print()
##    vars   n mean sd median trimmed  mad min max range  skew kurtosis    se
## X1    1 476 39.3 13     40      39 16.3  18  65    47 0.102    -1.06 0.594
#gender
table2(d$gender)
#expected
folk1a_2017q2 %>% 
  filter(age %in% 18:65) %>% 
  {
    c(
      mean = wtd_mean(.$age, .$total),
      SD = wtd_sd(.$age, .$total),
      male = sum(.$men) / sum(.$total)
    )
  }
##   mean     SD   male 
## 41.432 14.000  0.505
#education distribution
table(d$education) %>% prop.table()
## 
##                                              Grundskole 
##                                                  0.0906 
##     Gymnasial uddannelse (Alm. gymnasium/HF/HH/HGX/HTX) 
##                                                  0.1413 
##          Erhvervsuddannelser (HG, EFG, mesterlære m.v.) 
##                                                  0.2355 
##         Højere uddannelse af kort varighed (under 3 år) 
##                                                  0.1667 
##       Højere uddannelse af mellemlang varighed (3-4 år) 
##                                                  0.2246 
## Højere uddannelse af lang varighed (5 år eller derover) 
##                                                  0.1413
#expected is a little tricky, we have to merge but we can't exactly
HFUDD10_2017[-1] %>% 
  colSums() %>% 
  prop.table()
##                                H10 Grundskole 
##                                       0.20015 
##                    H20 Gymnasiale uddannelser 
##                                       0.11295 
##               H30 Erhvervsfaglige uddannelser 
##                                       0.33007 
##       H40 Korte videregående uddannelser, KVU 
##                                       0.05499 
## H50 Mellemlange videregående uddannelser, MVU 
##                                       0.15929 
##                 H60 Bacheloruddannelser, BACH 
##                                       0.02785 
##       H70 Lange videregående uddannelser, LVU 
##                                       0.10513 
##               H80 Ph.d. og forskeruddannelser 
##                                       0.00957
#party voting intentions
table2(d$party_vote) %>% print(n = Inf)
## # A tibble: 14 x 3
##    Group                   Count Percent
##    <chr>                   <dbl>   <dbl>
##  1 Dansk Folkeparti           83   17.4 
##  2 Socialdemokraterne         79   16.6 
##  3 Venstre                    53   11.1 
##  4 Vote blank                 45    9.45
##  5 Enhedslisten               36    7.56
##  6 Socialistisk Folkeparti    33    6.93
##  7 Liberal Alliance           29    6.09
##  8 Radikale Venstre           29    6.09
##  9 Alternativet               28    5.88
## 10 Would not vote             23    4.83
## 11 Nye Borgerlige             21    4.41
## 12 Konservative Folkeparti    11    2.31
## 13 Kristendemokraterne         6    1.26
## 14 <NA>                        0    0
#collection time
d$collected %>% mean()
## [1] "2017-06-27"
d$collected %>% range()
## [1] "2017-05-28" "2017-07-19"
d %>% 
  ggplot(aes(collected)) +
  geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#comparison
#https://en.wikipedia.org/wiki/Opinion_polling_for_the_next_Danish_general_election#2017
opinion_polls = tibble(
  "Socialdemokraterne" = c(25.6, 25.3),
  "Dansk Folkeparti" = c(17.4 , 20.9),
  "Venstre" = c(20.3, 17.2),
  "Enhedslisten" = c(7.4, 8.5),
  "Liberal Alliance" = c(5.5, 6.1),
  "Alternativet" = c(6.0, 5.7),
  "Radikale Venstre" = c(5.6, 6.3),
  "Socialistisk Folkeparti" = c(4.5, 5.2),
  "Konservative Folkeparti" = c(4.5, 3.5),
  "Kristendemokraterne" = c(0.6, 0.6),
  "Nye Borgerlige" = c(2.2, 0.7)
) %>% t()
party_names = rownames(opinion_polls)
opinion_polls = opinion_polls %>% as_tibble() %>% mutate(avg_polls = rowMeans(.))
## Warning: `as_tibble.matrix()` requires a matrix with column names or a `.name_repair` argument. Using compatibility `.name_repair`.
## This warning is displayed once per session.
colnames(opinion_polls) = c("Gallup_July 6_2017", "Voxmeter_July_2_2017", "avg_polls")
opinion_polls$party_name = party_names

#add our values
our_poll = d %>% filter(!party_vote %in% c("Vil ikke stemme", "Stemme blankt")) %>% pull(party_vote) %>% table2()
colnames(our_poll) = c("party_name", "count", "this_sample")

left_join(opinion_polls, our_poll %>% select(party_name, this_sample)) %>% 
  GG_scatter("avg_polls", "this_sample", case_names = "party_name", repel_names = T)
## Joining, by = "party_name"
## `geom_smooth()` using formula 'y ~ x'

#economic estimates 2014 and 2015
GG_scatter(dk_fiscal, "dnk_net_fiscal_contribution_2014", "dnk_net_fiscal_contribution_2015", case_names = "ISO", repel_names = T) +
  geom_abline(yintercept = 0, slope = 1, linetype = "dashed") +
  xlab("Net fiscal contribution, 2014") +
  ylab("Net fiscal contribution, 2015")
## Warning: Ignoring unknown parameters: yintercept
## `geom_smooth()` using formula 'y ~ x'

GG_save("figures/fiscal_contribs_2014_2015.png")
## `geom_smooth()` using formula 'y ~ x'

Accuracy

#sort dk_fiscal
dk_fiscal_sub = dplyr::arrange(dk_fiscal_sub, ISO)

#assert same order
assert_that(names(d_econ) %equals% names(d_prefs))
## [1] TRUE
assert_that(names(d_econ) %equals% dk_fiscal_sub$ISO)
## [1] TRUE
#score multiple metrics at a time
indi_accuracy = silence(score_accuracy(d_econ_num, criterion = dk_fiscal_sub$dnk_net_fiscal_contribution_2014, methods = c("pearson_r")))
indi_accuracy_noSYR = silence(score_accuracy(d_econ_num %>% no_SYR(), criterion = dk_fiscal_sub$dnk_net_fiscal_contribution_2014 %>% no_SYR(), methods = c("pearson_r")))

indi_accuracy %>% describe() %>% print()
##    vars   n  mean    sd median trimmed   mad    min   max range  skew kurtosis
## X1    1 476 0.578 0.199  0.624    0.61 0.137 -0.451 0.874  1.33 -2.03     5.71
##         se
## X1 0.00911
indi_accuracy_noSYR %>% describe() %>% print()
##    vars   n  mean    sd median trimmed   mad    min   max range  skew kurtosis
## X1    1 474 0.598 0.209  0.655   0.631 0.137 -0.538 0.878  1.42 -2.14     6.43
##        se
## X1 0.0096
#with syria
indi_accuracy %>% 
  GG_denhist(vline = "median") +
  scale_x_continuous("Stereotype accuracy\nPearson r correlation with true values", breaks = seq(-1, 1, .1))
## Warning in GG_denhist(., vline = "median"): received a data frame but no var:
## used the only available column
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

GG_save("figures/indi_accu_dist.png")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#without
indi_accuracy_noSYR %>% 
  GG_denhist(vline = "median") +
  scale_x_continuous("Stereotype accuracy\nPearson r correlation with true values", breaks = seq(-1, 1, .1)) +
  labs(caption = "Syria excluded")
## Warning in GG_denhist(., vline = "median"): received a data frame but no var:
## used the only available column
## Warning: Removed 2 rows containing non-finite values (stat_bin).
## Warning: Removed 2 rows containing non-finite values (stat_density).
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 2 rows containing non-finite values (stat_bin).

## Warning: Removed 2 rows containing non-finite values (stat_density).

GG_save("figures/indi_accu_dist_noSYR.png")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 2 rows containing non-finite values (stat_bin).

## Warning: Removed 2 rows containing non-finite values (stat_density).
#cutoffs
wtd_mean(indi_accuracy$pearson_r >= .3)
## [1] 0.912
wtd_mean(indi_accuracy$pearson_r >= .5)
## [1] 0.775
wtd_mean(indi_accuracy_noSYR$pearson_r >= .3)
## [1] 0.918
wtd_mean(indi_accuracy_noSYR$pearson_r >= .5)
## [1] 0.789
#add metric to main data
d$stereotype_accuracy = indi_accuracy$pearson_r
d$stereotype_accuracy_noSYR = indi_accuracy_noSYR$pearson_r

Muslim bias

#example case
example_muslim_bias = tibble(
  Muslim = dk_fiscal_sub$Muslim,
  true = dk_fiscal_sub$dk_fiscal,
  estimate = d_econ_num[105, ] %>% unlist(),
  name = dk_fiscal_sub$Names
)

#fit
example_muslim_bias_fit = lm(true ~ estimate, data = example_muslim_bias)
MOD_summary(example_muslim_bias_fit)
## 
##     ---- Model summary ----    
## Model coefficients
##           Beta    SE CI_lower CI_upper
## estimate 0.616 0.144    0.322     0.91
## 
## 
## Model meta-data
##   outcome  N df    R2 R2-adj. R2-cv
## 1    true 32 30 0.379   0.359    NA
## 
## 
## Etas from analysis of variance
##            Eta Eta_partial
## estimate 0.616       0.616
example_muslim_bias$resid = resid(example_muslim_bias_fit)

#scatter
GG_scatter(example_muslim_bias, "estimate", "true", case_names = "name") +
  scale_x_continuous("Estimated net economic contribution", breaks = 1:7) +
  ylab("Net economic contribution")
## `geom_smooth()` using formula 'y ~ x'

GG_scatter(example_muslim_bias, "Muslim", "resid", case_names = "name") +
  scale_x_continuous("Muslim % in home country", labels = scales::percent) +
  ylab("Residual of criterion ~ estimate")
## `geom_smooth()` using formula 'y ~ x'

#adjust for Muslim bias
muslim_fit = lm(resid ~ Muslim, data = example_muslim_bias)
example_muslim_bias$estimate_adj = example_muslim_bias$estimate %>% standardize() + predict(muslim_fit) %>% standardize()

GG_scatter(example_muslim_bias, "estimate_adj", "true", case_names = "name") +
  scale_x_continuous("Estimated net economic contribution") +
  ylab("Net economic contribution")
## `geom_smooth()` using formula 'y ~ x'

example_muslim_bias_fit2 = lm(true ~ estimate_adj, data = example_muslim_bias)
example_muslim_bias$resid2 = resid(example_muslim_bias_fit2)

GG_scatter(example_muslim_bias, "Muslim", "resid2", case_names = "name") +
  scale_x_continuous("Muslim % in home country", labels = scales::percent) +
  ylab("Residual of criterion ~ estimate")
## `geom_smooth()` using formula 'y ~ x'

#calculate muslim bias per person
indi_Muslim_bias = plyr::adply(d_econ_num, .margins = 1, function(r) {
  #local data
  d_ = tibble(
    true = dk_fiscal_sub$dk_fiscal,
    estimate = r %>% unlist(),
    Muslim = dk_fiscal_sub$Muslim
  )
  
  #fit accuracy
  fit_accu = lm(true ~ estimate, data = d_)
  
  #resids
  d_$resids = resid(fit_accu) %>% standardize()

  #out
  tibble(
    resid_x_Muslim = wtd.cors(d_$Muslim, d_$resids) %>% as.vector(),
    resid_wtd_mean = wtd_mean(d_$resids, d_$Muslim)
  )
})

indi_Muslim_bias_noSYR = plyr::adply(d_econ_num[-26], .margins = 1, function(r) {
  #local data
  d_ = tibble(
    true = dk_fiscal_sub$dk_fiscal[-26],
    estimate = r %>% unlist(),
    Muslim = dk_fiscal_sub$Muslim[-26]
  )
  
  #fit accuracy
  fit_accu = lm(true ~ estimate, data = d_)
  
  #resids
  d_$resids = resid(fit_accu) %>% standardize()

  #out
  tibble(
    resid_x_Muslim = wtd.cors(d_$Muslim, d_$resids) %>% as.vector(),
    resid_wtd_mean = wtd_mean(d_$resids, d_$Muslim)
  )
})

#save to main
d$Muslim_bias_r = indi_Muslim_bias$resid_x_Muslim
d$Muslim_bias_wtd_mean = indi_Muslim_bias$resid_wtd_mean
d$Muslim_bias_r_noSYR = indi_Muslim_bias_noSYR$resid_x_Muslim
d$Muslim_bias_wtd_mean_noSYR = indi_Muslim_bias_noSYR$resid_wtd_mean

#compare metrics
GG_scatter(d, "Muslim_bias_r", "Muslim_bias_wtd_mean") +
  xlab("Muslim bias (correlation metric)") +
  ylab("Muslim bias (weighted mean residual metric)")
## `geom_smooth()` using formula 'y ~ x'

#distribution
describe(d$Muslim_bias_r)
##    vars   n   mean    sd median trimmed   mad    min    max range  skew
## X1    1 476 -0.486 0.161 -0.494  -0.494 0.166 -0.756 0.0307 0.787 0.463
##    kurtosis      se
## X1   -0.159 0.00737
sum(d$Muslim_bias_r > 0)
## [1] 1
describe(d$Muslim_bias_r_noSYR)
##    vars   n   mean    sd median trimmed   mad    min   max range skew kurtosis
## X1    1 476 -0.528 0.162 -0.547   -0.54 0.164 -0.805 0.014 0.819 0.67    0.302
##         se
## X1 0.00743
sum(d$Muslim_bias_r_noSYR > 0)
## [1] 2
GG_denhist(d, "Muslim_bias_r") +
  xlab("Muslim bias (correlation metric)")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

GG_save("figures/muslim_bias_dist.png")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
GG_denhist(d, "Muslim_bias_r_noSYR") +
  xlab("Muslim bias (correlation metric)") +
  labs(caption = "Syria excluded")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

GG_save("figures/muslim_bias_dist_noSYR.png")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Mediation analysis

#example
mediation_example = tibble(
    preference = d_prefs_num_alt[146, ] %>% unlist(),
    stereotype = d_econ_num[146, ] %>% unlist(),
    Muslim = dk_fiscal_sub$Muslim,
    name = dk_fiscal_sub$Names
  )

#fit
(mediation_example_ORM = rms::orm(preference ~ stereotype, data = mediation_example))
## Logistic (Proportional Odds) Ordinal Regression Model
##  
##  rms::orm(formula = preference ~ stereotype, data = mediation_example)
##  
##  
##  Frequencies of Responses
##  
##   1  2  3  4 
##   5 10 16  1 
##  
##                        Model Likelihood          Discrimination          Rank Discrim.    
##                           Ratio Test                 Indexes                Indexes       
##  Obs            32    LR chi2      19.48    R2                  0.512    rho     0.666    
##  Distinct Y      4    d.f.             1    g                   2.207                     
##  Median Y        3    Pr(> chi2) <0.0001    gr                  9.088                     
##  max |deriv| 2e-05    Score chi2   18.34    |Pr(Y>=median)-0.5| 0.312                     
##                       Pr(> chi2) <0.0001                                                  
##  
##             Coef    S.E.   Wald Z Pr(>|Z|)
##  y>=2       -2.0647 1.0707 -1.93  0.0538  
##  y>=3       -4.6738 1.3593 -3.44  0.0006  
##  y>=4       -9.4871 2.0820 -4.56  <0.0001 
##  stereotype  1.0753 0.2924  3.68  0.0002  
## 
(mediation_example_OLS = rms::ols(preference ~ stereotype, data = mediation_example))
## Linear Regression Model
##  
##  rms::ols(formula = preference ~ stereotype, data = mediation_example)
##  
##                 Model Likelihood     Discrimination    
##                    Ratio Test           Indexes        
##  Obs      32    LR chi2     18.12    R2       0.432    
##  sigma0.6108    d.f.            1    R2 adj   0.413    
##  d.f.     30    Pr(> chi2) 0.0000    g        0.601    
##  
##  Residuals
##  
##        Min        1Q    Median        3Q       Max 
##  -1.296482 -0.296482 -0.003769  0.118090  1.996231 
##  
##  
##             Coef   S.E.   t    Pr(>|t|)
##  Intercept  1.1256 0.2889 3.90 0.0005  
##  stereotype 0.2927 0.0612 4.78 <0.0001 
## 
#plots
mediation_example$resid = resid(mediation_example_OLS)

GG_scatter(mediation_example, "stereotype", "preference", case_names = "name", repel_names = T) +
  scale_x_continuous(breaks = 1:7) + 
  scale_y_continuous(breaks = 1:4, limit = c(NA, 4.5))
## `geom_smooth()` using formula 'y ~ x'

GG_scatter(mediation_example, "Muslim", "resid", case_names = "name", repel_names = T) +
  scale_x_continuous("Muslim % in home country", labels = scales::percent) +
  ylab("Residual of preference ~ stereotype")
## `geom_smooth()` using formula 'y ~ x'

#stereotypes and preferences
mediation_model = plyr::ldply(seq_along_rows(d), function(ri) {

  #collect data
  d_ = tibble(
    preference = d_prefs_num_alt[ri, ] %>% unlist(),
    stereotype = d_econ_num[ri, ] %>% unlist(),
    Muslim = dk_fiscal_sub$Muslim,
    preference_ordinal = ordered(preference),
    stereotype_ordinal = ordered(stereotype)
  )
  
  #out
  out = tibble(
    # mediation_ORM = NA,
    mediation_r = NA,
    mediation_lr = NA,
    # Muslim_resid_ORM = NA,
    Muslim_resid_OLS = NA
  )
  
  #fit models
  # ORM = try({
  #   ORM_fit = silence(rms::orm(preference ~ stereotype, data = d_))
  #   out$Muslim_resid_ORM = silence(wtd.cors(resid(ORM_fit), d_$Muslim) %>% as.vector())
  #   out$mediation_ORM = ORM_fit$stats[11] %>% sqrt()
  # }, silent = T)
  
  OLS = try({
      OLS_fit = silence(rms::ols(preference ~ stereotype, data = d_))
      out$Muslim_resid_OLS = silence(wtd.cors(resid(OLS_fit), d_$Muslim) %>% as.vector() %>% Inf_to_NA())
      out$mediation_r = wtd.cors(d_$preference, d_$stereotype) %>% as.vector() %>% Inf_to_NA()
  }, silent = T)

  silence({LC = try({
    #latent correlations
    lc = hetcor(as.data.frame(d_[c(4, 5, 3)])) %>% .$correlations
    
    #mediation latent
    out$mediation_lr = lc %>% .[2, 1]
    
    #partial correlation
    out$Muslim_partial = partial.r(lc, c(1, 3), 2) %>% .[2, 1]
  }, silent = T)})
  
  out
})

rownames(mediation_model) = 1:nrow(mediation_model)

#no SYR
mediation_model_noSYR = plyr::ldply(seq_along_rows(d), function(ri) {

  #collect data
  d_ = tibble(
    preference = d_prefs_num_alt[ri, -26] %>% unlist(),
    stereotype = d_econ_num[ri, -26] %>% unlist(),
    Muslim = dk_fiscal_sub$Muslim[-26],
    preference_ordinal = ordered(preference),
    stereotype_ordinal = ordered(stereotype)
  )
  
  #out
  out = tibble(
    # mediation_ORM = NA,
    mediation_r = NA,
    mediation_lr = NA,
    # Muslim_resid_ORM = NA,
    Muslim_resid_OLS = NA
  )
  
  #fit models
  # ORM = try({
  #   ORM_fit = silence(rms::orm(preference ~ stereotype, data = d_))
  #   out$Muslim_resid_ORM = silence(wtd.cors(resid(ORM_fit), d_$Muslim) %>% as.vector())
  #   out$mediation_ORM = ORM_fit$stats[11] %>% sqrt()
  # }, silent = T)
  
  OLS = try({
      OLS_fit = silence(rms::ols(preference ~ stereotype, data = d_))
      out$Muslim_resid_OLS = silence(wtd.cors(resid(OLS_fit), d_$Muslim) %>% as.vector() %>% Inf_to_NA())
      out$mediation_r = wtd.cors(d_$preference, d_$stereotype) %>% as.vector() %>% Inf_to_NA()
  }, silent = T)

  silence({LC = try({
    #latent correlations
    lc = hetcor(as.data.frame(d_[c(4, 5, 3)])) %>% .$correlations
    
    #mediation latent
    out$mediation_lr = lc %>% .[2, 1]
    
    #partial correlation
    out$Muslim_partial = partial.r(lc, c(1, 3), 2) %>% .[2, 1]
  }, silent = T)})
  
  out
})

rownames(mediation_model_noSYR) = 1:nrow(mediation_model_noSYR)

#stats
describe(mediation_model$mediation_r) %>% print()
##    vars   n  mean    sd median trimmed   mad    min   max range  skew kurtosis
## X1    1 438 0.567 0.342  0.668   0.625 0.229 -0.804 0.979  1.78 -1.71     2.94
##        se
## X1 0.0163
describe(mediation_model$mediation_lr) %>% print()
##    vars   n  mean    sd median trimmed   mad    min max range  skew kurtosis
## X1    1 380 0.634 0.412  0.777   0.724 0.204 -0.916   1  1.92 -2.07     3.92
##        se
## X1 0.0212
describe(mediation_model$Muslim_resid_OLS) %>% print()
##    vars   n   mean    sd median trimmed   mad    min   max range  skew kurtosis
## X1    1 476 -0.109 0.246 -0.129  -0.114 0.241 -0.788 0.632  1.42 0.212   -0.234
##        se
## X1 0.0113
describe(mediation_model$Muslim_partial) %>% print()
##    vars   n  mean    sd median trimmed   mad min max range  skew kurtosis
## X1    1 368 -0.21 0.423 -0.272  -0.233 0.422  -1   1     2 0.533   0.0408
##        se
## X1 0.0221
describe(mediation_model_noSYR$mediation_r) %>% print()
##    vars   n  mean    sd median trimmed   mad    min   max range  skew kurtosis
## X1    1 435 0.569 0.335  0.661   0.627 0.227 -0.801 0.978  1.78 -1.75     3.18
##        se
## X1 0.0161
describe(mediation_model_noSYR$mediation_lr) %>% print()
##    vars   n  mean    sd median trimmed   mad    min max range  skew kurtosis
## X1    1 376 0.645 0.399   0.78   0.731 0.195 -0.913   1  1.91 -2.12     4.26
##        se
## X1 0.0206
describe(mediation_model_noSYR$Muslim_resid_OLS) %>% print()
##    vars   n   mean    sd median trimmed   mad    min   max range  skew kurtosis
## X1    1 449 -0.135 0.235  -0.15  -0.142 0.222 -0.786 0.592  1.38 0.257 -0.00304
##        se
## X1 0.0111
describe(mediation_model_noSYR$Muslim_partial) %>% print()
##    vars   n   mean    sd median trimmed  mad min max range  skew kurtosis
## X1    1 362 -0.218 0.424 -0.266  -0.242 0.41  -1   1     2 0.577    0.188
##        se
## X1 0.0223
#cors
wtd.cors(mediation_model)
##                  mediation_r mediation_lr Muslim_resid_OLS Muslim_partial
## mediation_r            1.000        0.956           -0.282         -0.342
## mediation_lr           0.956        1.000           -0.340         -0.344
## Muslim_resid_OLS      -0.282       -0.340            1.000          0.827
## Muslim_partial        -0.342       -0.344            0.827          1.000
wtd.cors(mediation_model_noSYR)
##                  mediation_r mediation_lr Muslim_resid_OLS Muslim_partial
## mediation_r            1.000        0.955           -0.239         -0.291
## mediation_lr           0.955        1.000           -0.291         -0.282
## Muslim_resid_OLS      -0.239       -0.291            1.000          0.818
## Muslim_partial        -0.291       -0.282            0.818          1.000
#dists
GG_denhist(mediation_model, "mediation_r", vline = median) +
  xlab("Strength of mediation (correlation)")
## Warning: Removed 38 rows containing non-finite values (stat_bin).
## Warning: Removed 38 rows containing non-finite values (stat_density).
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 38 rows containing non-finite values (stat_bin).

## Warning: Removed 38 rows containing non-finite values (stat_density).

GG_denhist(mediation_model, "Muslim_resid_OLS", vline = median) +
  xlab("Correlation of Muslim % with residual from preference ~ stereotype")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

GG_denhist(mediation_model, "Muslim_partial", vline = median) +
  xlab("Partial correlation of Muslim % with preference holding stereotype constant")
## Warning: Removed 108 rows containing non-finite values (stat_bin).
## Warning: Removed 108 rows containing non-finite values (stat_density).
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 108 rows containing non-finite values (stat_bin).

## Warning: Removed 108 rows containing non-finite values (stat_density).

#no SYR
GG_denhist(mediation_model_noSYR, "mediation_r", vline = median) +
  xlab("Strength of mediation (correlation)")
## Warning: Removed 41 rows containing non-finite values (stat_bin).
## Warning: Removed 41 rows containing non-finite values (stat_density).
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 41 rows containing non-finite values (stat_bin).

## Warning: Removed 41 rows containing non-finite values (stat_density).

GG_denhist(mediation_model_noSYR, "Muslim_resid_OLS", vline = median) +
  xlab("Correlation of Muslim % with residual from preference ~ stereotype")
## Warning: Removed 27 rows containing non-finite values (stat_bin).
## Warning: Removed 27 rows containing non-finite values (stat_density).
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 27 rows containing non-finite values (stat_bin).

## Warning: Removed 27 rows containing non-finite values (stat_density).

GG_denhist(mediation_model_noSYR, "Muslim_partial", vline = median) +
  xlab("Partial correlation of Muslim % with preference holding stereotype constant")
## Warning: Removed 114 rows containing non-finite values (stat_bin).
## Warning: Removed 114 rows containing non-finite values (stat_density).
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 114 rows containing non-finite values (stat_bin).

## Warning: Removed 114 rows containing non-finite values (stat_density).

#add to main data
d$Muslim_preference = mediation_model$Muslim_partial
d$Muslim_preference_noSYR = mediation_model_noSYR$Muslim_partial

#describe
d$Muslim_preference %>% describe()
##    vars   n  mean    sd median trimmed   mad min max range  skew kurtosis
## X1    1 368 -0.21 0.423 -0.272  -0.233 0.422  -1   1     2 0.533   0.0408
##        se
## X1 0.0221
d$Muslim_preference_noSYR %>% describe()
##    vars   n   mean    sd median trimmed  mad min max range  skew kurtosis
## X1    1 362 -0.218 0.424 -0.266  -0.242 0.41  -1   1     2 0.577    0.188
##        se
## X1 0.0223

Predictors of individual primary outcomes

#define var groups
main_outcomes = c("stereotype_accuracy", "Muslim_bias_r", "Muslim_preference")
main_outcomes_noSYR = c("stereotype_accuracy_noSYR", "Muslim_bias_r_noSYR", "Muslim_preference_noSYR")
predictors = c("age", "education_num", "Muslims_are_treated_well", "DK_admit_only_net_positive_immigrants", "nonwesterns_are_net_positive")

#cors
wtd.cors(d[c(main_outcomes, predictors)])
##                                       stereotype_accuracy Muslim_bias_r
## stereotype_accuracy                                1.0000        0.7424
## Muslim_bias_r                                      0.7424        1.0000
## Muslim_preference                                 -0.0437       -0.0516
## age                                                0.1397        0.0897
## education_num                                      0.1778        0.1929
## Muslims_are_treated_well                           0.0855        0.1021
## DK_admit_only_net_positive_immigrants              0.1536        0.1791
## nonwesterns_are_net_positive                      -0.1244       -0.1593
##                                       Muslim_preference     age education_num
## stereotype_accuracy                             -0.0437  0.1397        0.1778
## Muslim_bias_r                                   -0.0516  0.0897        0.1929
## Muslim_preference                                1.0000 -0.1179        0.0173
## age                                             -0.1179  1.0000       -0.0519
## education_num                                    0.0173 -0.0519        1.0000
## Muslims_are_treated_well                        -0.1864  0.1283       -0.0917
## DK_admit_only_net_positive_immigrants           -0.3146  0.0981       -0.1099
## nonwesterns_are_net_positive                     0.0888 -0.1106        0.1505
##                                       Muslims_are_treated_well
## stereotype_accuracy                                     0.0855
## Muslim_bias_r                                           0.1021
## Muslim_preference                                      -0.1864
## age                                                     0.1283
## education_num                                          -0.0917
## Muslims_are_treated_well                                1.0000
## DK_admit_only_net_positive_immigrants                   0.4928
## nonwesterns_are_net_positive                           -0.1930
##                                       DK_admit_only_net_positive_immigrants
## stereotype_accuracy                                                  0.1536
## Muslim_bias_r                                                        0.1791
## Muslim_preference                                                   -0.3146
## age                                                                  0.0981
## education_num                                                       -0.1099
## Muslims_are_treated_well                                             0.4928
## DK_admit_only_net_positive_immigrants                                1.0000
## nonwesterns_are_net_positive                                        -0.2130
##                                       nonwesterns_are_net_positive
## stereotype_accuracy                                        -0.1244
## Muslim_bias_r                                              -0.1593
## Muslim_preference                                           0.0888
## age                                                        -0.1106
## education_num                                               0.1505
## Muslims_are_treated_well                                   -0.1930
## DK_admit_only_net_positive_immigrants                      -0.2130
## nonwesterns_are_net_positive                                1.0000
wtd.cors(d[c(main_outcomes_noSYR, predictors)])
##                                       stereotype_accuracy_noSYR
## stereotype_accuracy_noSYR                               1.00000
## Muslim_bias_r_noSYR                                     0.68823
## Muslim_preference_noSYR                                -0.00366
## age                                                     0.17603
## education_num                                           0.16281
## Muslims_are_treated_well                                0.08657
## DK_admit_only_net_positive_immigrants                   0.14437
## nonwesterns_are_net_positive                           -0.12769
##                                       Muslim_bias_r_noSYR
## stereotype_accuracy_noSYR                          0.6882
## Muslim_bias_r_noSYR                                1.0000
## Muslim_preference_noSYR                           -0.0254
## age                                                0.1059
## education_num                                      0.1878
## Muslims_are_treated_well                           0.1040
## DK_admit_only_net_positive_immigrants              0.1807
## nonwesterns_are_net_positive                      -0.1612
##                                       Muslim_preference_noSYR     age
## stereotype_accuracy_noSYR                            -0.00366  0.1760
## Muslim_bias_r_noSYR                                  -0.02540  0.1059
## Muslim_preference_noSYR                               1.00000 -0.1161
## age                                                  -0.11608  1.0000
## education_num                                         0.03314 -0.0519
## Muslims_are_treated_well                             -0.17431  0.1283
## DK_admit_only_net_positive_immigrants                -0.29203  0.0981
## nonwesterns_are_net_positive                          0.08461 -0.1106
##                                       education_num Muslims_are_treated_well
## stereotype_accuracy_noSYR                    0.1628                   0.0866
## Muslim_bias_r_noSYR                          0.1878                   0.1040
## Muslim_preference_noSYR                      0.0331                  -0.1743
## age                                         -0.0519                   0.1283
## education_num                                1.0000                  -0.0917
## Muslims_are_treated_well                    -0.0917                   1.0000
## DK_admit_only_net_positive_immigrants       -0.1099                   0.4928
## nonwesterns_are_net_positive                 0.1505                  -0.1930
##                                       DK_admit_only_net_positive_immigrants
## stereotype_accuracy_noSYR                                            0.1444
## Muslim_bias_r_noSYR                                                  0.1807
## Muslim_preference_noSYR                                             -0.2920
## age                                                                  0.0981
## education_num                                                       -0.1099
## Muslims_are_treated_well                                             0.4928
## DK_admit_only_net_positive_immigrants                                1.0000
## nonwesterns_are_net_positive                                        -0.2130
##                                       nonwesterns_are_net_positive
## stereotype_accuracy_noSYR                                  -0.1277
## Muslim_bias_r_noSYR                                        -0.1612
## Muslim_preference_noSYR                                     0.0846
## age                                                        -0.1106
## education_num                                               0.1505
## Muslims_are_treated_well                                   -0.1930
## DK_admit_only_net_positive_immigrants                      -0.2130
## nonwesterns_are_net_positive                                1.0000
#plot
GG_scatter(d, "stereotype_accuracy", "Muslim_bias_r") +
  geom_smooth(se = F) +
  scale_x_continuous("Stereotype accuracy\nCorrelation of estimates with criterion data", breaks = seq(-1, 1, .2)) +
  ylab("Muslim bias in estimate\nCorrelation of estimate residual with Muslim%")
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

GG_save("figures/accuracy_bias.png")
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
GG_scatter(d, "stereotype_accuracy_noSYR", "Muslim_bias_r_noSYR") +
  geom_smooth(se = F) +
  scale_x_continuous("Stereotype accuracy\nCorrelation of estimates with criterion data", breaks = seq(-1, 1, .2)) +
  ylab("Muslim bias in estimate\nCorrelation of estimate residual with Muslim%") +
  labs(caption = "Syria excluded")
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

GG_save("figures/accuracy_bias_noSYR.png")
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
#models
#with Syria
list(
  rms::ols(stereotype_accuracy ~ age + gender, data = d %>% df_standardize(messages = F)),
  rms::ols(stereotype_accuracy ~ age + gender + education_num, data = d %>% df_standardize(messages = F)),
  rms::ols(stereotype_accuracy ~ age + gender + block_vote, data = d %>% df_standardize(messages = F)),
  rms::ols(stereotype_accuracy ~ age + gender + education_num + block_vote, data = d %>% df_standardize(messages = F)),
  rms::ols(stereotype_accuracy ~ age + gender + party_vote, data = d %>% df_standardize(messages = F)),
  rms::ols(stereotype_accuracy ~ age + gender + education_num + party_vote, data = d %>% df_standardize(messages = F))
) %>% 
  summarize_models(asterisks = c(.05, .01, .005)) %>% 
  DT::datatable()
#without
list(
  rms::ols(stereotype_accuracy_noSYR ~ age + gender, data = d %>% df_standardize(messages = F)),
  rms::ols(stereotype_accuracy_noSYR ~ age + gender + education_num, data = d %>% df_standardize(messages = F)),
  rms::ols(stereotype_accuracy_noSYR ~ age + gender + block_vote, data = d %>% df_standardize(messages = F)),
  rms::ols(stereotype_accuracy_noSYR ~ age + gender + education_num + block_vote, data = d %>% df_standardize(messages = F)),
  rms::ols(stereotype_accuracy_noSYR ~ age + gender + party_vote, data = d %>% df_standardize(messages = F)),
  rms::ols(stereotype_accuracy_noSYR ~ age + gender + education_num + party_vote, data = d %>% df_standardize(messages = F))
) %>% 
  summarize_models(asterisks = c(.05, .01, .005)) %>% 
  DT::datatable()
#not standardized
#models
#with Syria
list(
  rms::ols(stereotype_accuracy ~ age + gender, data = d),
  rms::ols(stereotype_accuracy ~ age + gender + education_num, data = d),
  rms::ols(stereotype_accuracy ~ age + gender + block_vote, data = d),
  rms::ols(stereotype_accuracy ~ age + gender + education_num + block_vote, data = d),
  rms::ols(stereotype_accuracy ~ age + gender + party_vote, data = d),
  rms::ols(stereotype_accuracy ~ age + gender + education_num + party_vote, data = d)
) %>% 
  summarize_models(beta_digits = 4, asterisks = c(.05, .01, .005)) %>% 
  DT::datatable()
#without
list(
  rms::ols(stereotype_accuracy_noSYR ~ age + gender, data = d),
  rms::ols(stereotype_accuracy_noSYR ~ age + gender + education_num, data = d),
  rms::ols(stereotype_accuracy_noSYR ~ age + gender + block_vote, data = d),
  rms::ols(stereotype_accuracy_noSYR ~ age + gender + education_num + block_vote, data = d),
  rms::ols(stereotype_accuracy_noSYR ~ age + gender + party_vote, data = d),
  rms::ols(stereotype_accuracy_noSYR ~ age + gender + education_num + party_vote, data = d)
) %>% 
  summarize_models(beta_digits = 4, asterisks = c(.05, .01, .005)) %>% 
  DT::datatable()
#party models
#with SYR
rms::ols(stereotype_accuracy ~ party_vote, data = d)
## Linear Regression Model
##  
##  rms::ols(formula = stereotype_accuracy ~ party_vote, data = d)
##  
##                 Model Likelihood     Discrimination    
##                    Ratio Test           Indexes        
##  Obs     476    LR chi2     16.57    R2       0.034    
##  sigma0.1977    d.f.           12    R2 adj   0.009    
##  d.f.    463    Pr(> chi2) 0.1665    g        0.041    
##  
##  Residuals
##  
##       Min       1Q   Median       3Q      Max 
##  -1.02772 -0.06558  0.04243  0.13307  0.31234 
##  
##  
##                                     Coef    S.E.   t     Pr(>|t|)
##  Intercept                           0.5884 0.0222 26.45 <0.0001 
##  party_vote=Alternativet            -0.0471 0.0435 -1.08 0.2793  
##  party_vote=Dansk Folkeparti        -0.0115 0.0311 -0.37 0.7127  
##  party_vote=Enhedslisten            -0.0284 0.0398 -0.71 0.4761  
##  party_vote=Konservative Folkeparti  0.0705 0.0636  1.11 0.2687  
##  party_vote=Kristendemokraterne      0.0776 0.0837  0.93 0.3544  
##  party_vote=Liberal Alliance         0.0613 0.0429  1.43 0.1543  
##  party_vote=Nye Borgerlige          -0.0154 0.0485 -0.32 0.7510  
##  party_vote=Radikale Venstre        -0.0263 0.0429 -0.61 0.5408  
##  party_vote=Socialistisk Folkeparti -0.0689 0.0410 -1.68 0.0935  
##  party_vote=Venstre                  0.0357 0.0351  1.02 0.3094  
##  party_vote=Vote blank              -0.0477 0.0369 -1.29 0.1973  
##  party_vote=Would not vote          -0.0399 0.0469 -0.85 0.3952  
## 
rms::ols(Muslim_bias_r ~ party_vote, data = d)
## Linear Regression Model
##  
##  rms::ols(formula = Muslim_bias_r ~ party_vote, data = d)
##  
##                 Model Likelihood     Discrimination    
##                    Ratio Test           Indexes        
##  Obs     476    LR chi2     24.43    R2       0.050    
##  sigma0.1586    d.f.           12    R2 adj   0.025    
##  d.f.    463    Pr(> chi2) 0.0178    g        0.041    
##  
##  Residuals
##  
##       Min       1Q   Median       3Q      Max 
##  -0.30640 -0.12056 -0.01092  0.10114  0.47576 
##  
##  
##                                     Coef    S.E.   t      Pr(>|t|)
##  Intercept                          -0.5024 0.0178 -28.15 <0.0001 
##  party_vote=Alternativet            -0.0324 0.0349  -0.93 0.3541  
##  party_vote=Dansk Folkeparti         0.0452 0.0249   1.81 0.0708  
##  party_vote=Enhedslisten            -0.0272 0.0319  -0.85 0.3944  
##  party_vote=Konservative Folkeparti  0.0813 0.0511   1.59 0.1121  
##  party_vote=Kristendemokraterne      0.0805 0.0672   1.20 0.2314  
##  party_vote=Liberal Alliance         0.0855 0.0344   2.48 0.0134  
##  party_vote=Nye Borgerlige           0.0499 0.0389   1.28 0.2006  
##  party_vote=Radikale Venstre         0.0140 0.0344   0.41 0.6838  
##  party_vote=Socialistisk Folkeparti -0.0212 0.0329  -0.64 0.5204  
##  party_vote=Venstre                  0.0394 0.0282   1.40 0.1629  
##  party_vote=Vote blank              -0.0260 0.0296  -0.88 0.3808  
##  party_vote=Would not vote           0.0185 0.0376   0.49 0.6221  
## 
rms::ols(Muslim_preference ~ party_vote, data = d)
## Frequencies of Missing Values Due to Each Variable
## Muslim_preference        party_vote 
##               108                 0 
## 
## Linear Regression Model
##  
##  rms::ols(formula = Muslim_preference ~ party_vote, data = d)
##  
##  
##                 Model Likelihood     Discrimination    
##                    Ratio Test           Indexes        
##  Obs     368    LR chi2     24.64    R2       0.065    
##  sigma0.4164    d.f.           12    R2 adj   0.033    
##  d.f.    355    Pr(> chi2) 0.0166    g        0.097    
##  
##  Residuals
##  
##       Min       1Q   Median       3Q      Max 
##  -0.93362 -0.27710 -0.03843  0.27817  1.28037 
##  
##  
##                                     Coef    S.E.   t     Pr(>|t|)
##  Intercept                          -0.2526 0.0533 -4.74 <0.0001 
##  party_vote=Alternativet             0.3989 0.1073  3.72 0.0002  
##  party_vote=Dansk Folkeparti        -0.0278 0.0742 -0.37 0.7084  
##  party_vote=Enhedslisten             0.0627 0.0950  0.66 0.5100  
##  party_vote=Konservative Folkeparti  0.0857 0.1420  0.60 0.5466  
##  party_vote=Kristendemokraterne     -0.1242 0.2149 -0.58 0.5637  
##  party_vote=Liberal Alliance         0.0888 0.1035  0.86 0.3914  
##  party_vote=Nye Borgerlige           0.0250 0.1142  0.22 0.8267  
##  party_vote=Radikale Venstre         0.2321 0.1035  2.24 0.0256  
##  party_vote=Socialistisk Folkeparti  0.0133 0.1003  0.13 0.8949  
##  party_vote=Venstre                 -0.0287 0.0808 -0.36 0.7225  
##  party_vote=Vote blank               0.0306 0.0909  0.34 0.7369  
##  party_vote=Would not vote          -0.0088 0.1169 -0.08 0.9399  
## 
#without
rms::ols(stereotype_accuracy_noSYR ~ party_vote, data = d)
## Frequencies of Missing Values Due to Each Variable
## stereotype_accuracy_noSYR                party_vote 
##                         2                         0 
## 
## Linear Regression Model
##  
##  rms::ols(formula = stereotype_accuracy_noSYR ~ party_vote, data = d)
##  
##  
##                 Model Likelihood     Discrimination    
##                    Ratio Test           Indexes        
##  Obs     474    LR chi2     19.30    R2       0.040    
##  sigma0.2074    d.f.           12    R2 adj   0.015    
##  d.f.    461    Pr(> chi2) 0.0815    g        0.047    
##  
##  Residuals
##  
##       Min       1Q   Median       3Q      Max 
##  -1.14052 -0.06976  0.05080  0.13141  0.31223 
##  
##  
##                                     Coef    S.E.   t     Pr(>|t|)
##  Intercept                           0.6160 0.0233 26.40 <0.0001 
##  party_vote=Alternativet            -0.0574 0.0462 -1.24 0.2153  
##  party_vote=Dansk Folkeparti        -0.0130 0.0326 -0.40 0.6891  
##  party_vote=Enhedslisten            -0.0266 0.0417 -0.64 0.5246  
##  party_vote=Konservative Folkeparti  0.0366 0.0667  0.55 0.5838  
##  party_vote=Kristendemokraterne      0.0896 0.0878  1.02 0.3081  
##  party_vote=Liberal Alliance         0.0545 0.0450  1.21 0.2267  
##  party_vote=Nye Borgerlige          -0.0585 0.0519 -1.13 0.2600  
##  party_vote=Radikale Venstre        -0.0318 0.0450 -0.71 0.4806  
##  party_vote=Socialistisk Folkeparti -0.0909 0.0430 -2.12 0.0350  
##  party_vote=Venstre                  0.0362 0.0368  0.98 0.3257  
##  party_vote=Vote blank              -0.0621 0.0387 -1.60 0.1096  
##  party_vote=Would not vote          -0.0597 0.0491 -1.22 0.2250  
## 
rms::ols(Muslim_bias_r_noSYR ~ party_vote, data = d)
## Linear Regression Model
##  
##  rms::ols(formula = Muslim_bias_r_noSYR ~ party_vote, data = d)
##  
##                 Model Likelihood     Discrimination    
##                    Ratio Test           Indexes        
##  Obs     476    LR chi2     26.77    R2       0.055    
##  sigma0.1596    d.f.           12    R2 adj   0.030    
##  d.f.    463    Pr(> chi2) 0.0083    g        0.043    
##  
##  Residuals
##  
##       Min       1Q   Median       3Q      Max 
##  -0.30145 -0.12258 -0.01139  0.09631  0.52648 
##  
##  
##                                     Coef    S.E.   t      Pr(>|t|)
##  Intercept                          -0.5428 0.0180 -30.22 <0.0001 
##  party_vote=Alternativet            -0.0435 0.0351  -1.24 0.2158  
##  party_vote=Dansk Folkeparti         0.0475 0.0251   1.90 0.0587  
##  party_vote=Enhedslisten            -0.0308 0.0321  -0.96 0.3378  
##  party_vote=Konservative Folkeparti  0.0721 0.0514   1.40 0.1611  
##  party_vote=Kristendemokraterne      0.0735 0.0676   1.09 0.2771  
##  party_vote=Liberal Alliance         0.0847 0.0347   2.45 0.0148  
##  party_vote=Nye Borgerlige           0.0576 0.0392   1.47 0.1422  
##  party_vote=Radikale Venstre         0.0166 0.0347   0.48 0.6317  
##  party_vote=Socialistisk Folkeparti -0.0296 0.0331  -0.90 0.3709  
##  party_vote=Venstre                  0.0344 0.0283   1.21 0.2253  
##  party_vote=Vote blank              -0.0280 0.0298  -0.94 0.3474  
##  party_vote=Would not vote           0.0200 0.0378   0.53 0.5964  
## 
rms::ols(Muslim_preference_noSYR ~ party_vote, data = d)
## Frequencies of Missing Values Due to Each Variable
## Muslim_preference_noSYR              party_vote 
##                     114                       0 
## 
## Linear Regression Model
##  
##  rms::ols(formula = Muslim_preference_noSYR ~ party_vote, data = d)
##  
##  
##                 Model Likelihood     Discrimination    
##                    Ratio Test           Indexes        
##  Obs     362    LR chi2     22.55    R2       0.060    
##  sigma0.4184    d.f.           12    R2 adj   0.028    
##  d.f.    349    Pr(> chi2) 0.0318    g        0.094    
##  
##  Residuals
##  
##       Min       1Q   Median       3Q      Max 
##  -0.98054 -0.27540 -0.02135  0.27211  1.28241 
##  
##  
##                                     Coef    S.E.   t     Pr(>|t|)
##  Intercept                          -0.2824 0.0545 -5.19 <0.0001 
##  party_vote=Alternativet             0.4084 0.1127  3.62 0.0003  
##  party_vote=Dansk Folkeparti         0.0027 0.0752  0.04 0.9715  
##  party_vote=Enhedslisten             0.0861 0.0960  0.90 0.3702  
##  party_vote=Konservative Folkeparti  0.1140 0.1497  0.76 0.4468  
##  party_vote=Kristendemokraterne     -0.0872 0.2162 -0.40 0.6871  
##  party_vote=Liberal Alliance         0.1227 0.1045  1.17 0.2414  
##  party_vote=Nye Borgerlige           0.0496 0.1152  0.43 0.6668  
##  party_vote=Radikale Venstre         0.2630 0.1045  2.52 0.0123  
##  party_vote=Socialistisk Folkeparti  0.0240 0.1028  0.23 0.8160  
##  party_vote=Venstre                  0.0037 0.0818  0.05 0.9640  
##  party_vote=Vote blank               0.0703 0.0918  0.77 0.4446  
##  party_vote=Would not vote           0.0291 0.1179  0.25 0.8052  
## 
#cohen d
SMD_matrix(d$stereotype_accuracy, d$party_vote)
##                         Socialdemokraterne Alternativet Dansk Folkeparti
## Socialdemokraterne                      NA      0.23824           0.0579
## Alternativet                        0.2382           NA          -0.1803
## Dansk Folkeparti                    0.0579     -0.18033               NA
## Enhedslisten                        0.1434     -0.09484           0.0855
## Konservative Folkeparti            -0.3564     -0.59463          -0.4143
## Kristendemokraterne                -0.3926     -0.63083          -0.4505
## Liberal Alliance                   -0.3098     -0.54804          -0.3677
## Nye Borgerlige                      0.0779     -0.16030           0.0200
## Radikale Venstre                    0.1329     -0.10536           0.0750
## Socialistisk Folkeparti             0.3483      0.11008           0.2904
## Venstre                            -0.1807     -0.41892          -0.2386
## Vote blank                          0.2412      0.00292           0.1832
## Would not vote                      0.2016     -0.03660           0.1437
##                         Enhedslisten Konservative Folkeparti
## Socialdemokraterne            0.1434                 -0.3564
## Alternativet                 -0.0948                 -0.5946
## Dansk Folkeparti              0.0855                 -0.4143
## Enhedslisten                      NA                 -0.4998
## Konservative Folkeparti      -0.4998                      NA
## Kristendemokraterne          -0.5360                 -0.0362
## Liberal Alliance             -0.4532                  0.0466
## Nye Borgerlige               -0.0655                  0.4343
## Radikale Venstre             -0.0105                  0.4893
## Socialistisk Folkeparti       0.2049                  0.7047
## Venstre                      -0.3241                  0.1757
## Vote blank                    0.0978                  0.5975
## Would not vote                0.0582                  0.5580
##                         Kristendemokraterne Liberal Alliance Nye Borgerlige
## Socialdemokraterne                  -0.3926          -0.3098         0.0779
## Alternativet                        -0.6308          -0.5480        -0.1603
## Dansk Folkeparti                    -0.4505          -0.3677         0.0200
## Enhedslisten                        -0.5360          -0.4532        -0.0655
## Konservative Folkeparti             -0.0362           0.0466         0.4343
## Kristendemokraterne                      NA           0.0828         0.4705
## Liberal Alliance                     0.0828               NA         0.3877
## Nye Borgerlige                       0.4705           0.3877             NA
## Radikale Venstre                     0.5255           0.4427         0.0549
## Socialistisk Folkeparti              0.7409           0.6581         0.2704
## Venstre                              0.2119           0.1291        -0.2586
## Vote blank                           0.6337           0.5510         0.1632
## Would not vote                       0.5942           0.5114         0.1237
##                         Radikale Venstre Socialistisk Folkeparti Venstre
## Socialdemokraterne                0.1329                   0.348  -0.181
## Alternativet                     -0.1054                   0.110  -0.419
## Dansk Folkeparti                  0.0750                   0.290  -0.239
## Enhedslisten                     -0.0105                   0.205  -0.324
## Konservative Folkeparti           0.4893                   0.705   0.176
## Kristendemokraterne               0.5255                   0.741   0.212
## Liberal Alliance                  0.4427                   0.658   0.129
## Nye Borgerlige                    0.0549                   0.270  -0.259
## Radikale Venstre                      NA                   0.215  -0.314
## Socialistisk Folkeparti           0.2154                      NA  -0.529
## Venstre                          -0.3136                  -0.529      NA
## Vote blank                        0.1083                  -0.107   0.422
## Would not vote                    0.0688                  -0.147   0.382
##                         Vote blank Would not vote
## Socialdemokraterne         0.24116         0.2016
## Alternativet               0.00292        -0.0366
## Dansk Folkeparti           0.18325         0.1437
## Enhedslisten               0.09776         0.0582
## Konservative Folkeparti    0.59755         0.5580
## Kristendemokraterne        0.63375         0.5942
## Liberal Alliance           0.55095         0.5114
## Nye Borgerlige             0.16322         0.1237
## Radikale Venstre           0.10827         0.0688
## Socialistisk Folkeparti   -0.10716        -0.1467
## Venstre                    0.42183         0.3823
## Vote blank                      NA        -0.0395
## Would not vote            -0.03952             NA
SMD_matrix(d$Muslim_bias_r, d$party_vote)
##                         Socialdemokraterne Alternativet Dansk Folkeparti
## Socialdemokraterne                      NA       0.2040          -0.2847
## Alternativet                        0.2040           NA          -0.4887
## Dansk Folkeparti                   -0.2847      -0.4887               NA
## Enhedslisten                        0.1714      -0.0326           0.4561
## Konservative Folkeparti            -0.5124      -0.7164          -0.2277
## Kristendemokraterne                -0.5074      -0.7114          -0.2227
## Liberal Alliance                   -0.5391      -0.7431          -0.2544
## Nye Borgerlige                     -0.3147      -0.5187          -0.0300
## Radikale Venstre                   -0.0885      -0.2925           0.1962
## Socialistisk Folkeparti             0.1333      -0.0707           0.4180
## Venstre                            -0.2481      -0.4521           0.0365
## Vote blank                          0.1639      -0.0402           0.4485
## Would not vote                     -0.1169      -0.3209           0.1678
##                         Enhedslisten Konservative Folkeparti
## Socialdemokraterne           0.17142                -0.51236
## Alternativet                -0.03259                -0.71636
## Dansk Folkeparti             0.45609                -0.22769
## Enhedslisten                      NA                -0.68378
## Konservative Folkeparti     -0.68378                      NA
## Kristendemokraterne         -0.67883                 0.00495
## Liberal Alliance            -0.71049                -0.02671
## Nye Borgerlige              -0.48608                 0.19770
## Radikale Venstre            -0.25991                 0.42387
## Socialistisk Folkeparti     -0.03809                 0.64569
## Venstre                     -0.41955                 0.26423
## Vote blank                  -0.00757                 0.67621
## Would not vote              -0.28828                 0.39550
##                         Kristendemokraterne Liberal Alliance Nye Borgerlige
## Socialdemokraterne                 -0.50741          -0.5391        -0.3147
## Alternativet                       -0.71142          -0.7431        -0.5187
## Dansk Folkeparti                   -0.22274          -0.2544        -0.0300
## Enhedslisten                       -0.67883          -0.7105        -0.4861
## Konservative Folkeparti             0.00495          -0.0267         0.1977
## Kristendemokraterne                      NA          -0.0317         0.1928
## Liberal Alliance                   -0.03166               NA         0.2244
## Nye Borgerlige                      0.19275           0.2244             NA
## Radikale Venstre                    0.41892           0.4506         0.2262
## Socialistisk Folkeparti             0.64074           0.6724         0.4480
## Venstre                             0.25928           0.2909         0.0665
## Vote blank                          0.67126           0.7029         0.4785
## Would not vote                      0.39055           0.4222         0.1978
##                         Radikale Venstre Socialistisk Folkeparti Venstre
## Socialdemokraterne               -0.0885                  0.1333 -0.2481
## Alternativet                     -0.2925                 -0.0707 -0.4521
## Dansk Folkeparti                  0.1962                  0.4180  0.0365
## Enhedslisten                     -0.2599                 -0.0381 -0.4195
## Konservative Folkeparti           0.4239                  0.6457  0.2642
## Kristendemokraterne               0.4189                  0.6407  0.2593
## Liberal Alliance                  0.4506                  0.6724  0.2909
## Nye Borgerlige                    0.2262                  0.4480  0.0665
## Radikale Venstre                      NA                  0.2218 -0.1596
## Socialistisk Folkeparti           0.2218                      NA -0.3815
## Venstre                          -0.1596                 -0.3815      NA
## Vote blank                        0.2523                  0.0305  0.4120
## Would not vote                   -0.0284                 -0.2502  0.1313
##                         Vote blank Would not vote
## Socialdemokraterne         0.16385        -0.1169
## Alternativet              -0.04015        -0.3209
## Dansk Folkeparti           0.44853         0.1678
## Enhedslisten              -0.00757        -0.2883
## Konservative Folkeparti    0.67621         0.3955
## Kristendemokraterne        0.67126         0.3906
## Liberal Alliance           0.70292         0.4222
## Nye Borgerlige             0.47851         0.1978
## Radikale Venstre           0.25234        -0.0284
## Socialistisk Folkeparti    0.03052        -0.2502
## Venstre                    0.41198         0.1313
## Vote blank                      NA        -0.2807
## Would not vote            -0.28071             NA
SMD_matrix(d$Muslim_preference, d$party_vote)
##                         Socialdemokraterne Alternativet Dansk Folkeparti
## Socialdemokraterne                      NA       -0.958          0.06673
## Alternativet                       -0.9581           NA          1.02484
## Dansk Folkeparti                    0.0667        1.025               NA
## Enhedslisten                       -0.1505        0.808         -0.21727
## Konservative Folkeparti            -0.2059        0.752         -0.27259
## Kristendemokraterne                 0.2983        1.256          0.23153
## Liberal Alliance                   -0.2134        0.745         -0.28011
## Nye Borgerlige                     -0.0601        0.898         -0.12680
## Radikale Venstre                   -0.5574        0.401         -0.62417
## Socialistisk Folkeparti            -0.0319        0.926         -0.09859
## Venstre                             0.0690        1.027          0.00226
## Vote blank                         -0.0734        0.885         -0.14013
## Would not vote                      0.0212        0.979         -0.04555
##                         Enhedslisten Konservative Folkeparti
## Socialdemokraterne           -0.1505                -0.20587
## Alternativet                  0.8076                 0.75224
## Dansk Folkeparti             -0.2173                -0.27259
## Enhedslisten                      NA                -0.05533
## Konservative Folkeparti      -0.0553                      NA
## Kristendemokraterne           0.4488                 0.50413
## Liberal Alliance             -0.0628                -0.00752
## Nye Borgerlige                0.0905                 0.14579
## Radikale Venstre             -0.4069                -0.35157
## Socialistisk Folkeparti       0.1187                 0.17401
## Venstre                       0.2195                 0.27485
## Vote blank                    0.0771                 0.13247
## Would not vote                0.1717                 0.22704
##                         Kristendemokraterne Liberal Alliance Nye Borgerlige
## Socialdemokraterne                    0.298         -0.21339        -0.0601
## Alternativet                          1.256          0.74472         0.8980
## Dansk Folkeparti                      0.232         -0.28011        -0.1268
## Enhedslisten                          0.449         -0.06285         0.0905
## Konservative Folkeparti               0.504         -0.00752         0.1458
## Kristendemokraterne                      NA         -0.51165        -0.3583
## Liberal Alliance                     -0.512               NA         0.1533
## Nye Borgerlige                       -0.358          0.15331             NA
## Radikale Venstre                     -0.856         -0.34405        -0.4974
## Socialistisk Folkeparti              -0.330          0.18153         0.0282
## Venstre                              -0.229          0.28237         0.1291
## Vote blank                           -0.372          0.13999        -0.0133
## Would not vote                       -0.277          0.23456         0.0813
##                         Radikale Venstre Socialistisk Folkeparti  Venstre
## Socialdemokraterne                -0.557                 -0.0319  0.06898
## Alternativet                       0.401                  0.9263  1.02709
## Dansk Folkeparti                  -0.624                 -0.0986  0.00226
## Enhedslisten                      -0.407                  0.1187  0.21952
## Konservative Folkeparti           -0.352                  0.1740  0.27485
## Kristendemokraterne               -0.856                 -0.3301 -0.22928
## Liberal Alliance                  -0.344                  0.1815  0.28237
## Nye Borgerlige                    -0.497                  0.0282  0.12906
## Radikale Venstre                      NA                  0.5256  0.62642
## Socialistisk Folkeparti            0.526                      NA  0.10084
## Venstre                            0.626                  0.1008       NA
## Vote blank                         0.484                 -0.0415 -0.14239
## Would not vote                     0.579                  0.0530 -0.04781
##                         Vote blank Would not vote
## Socialdemokraterne         -0.0734         0.0212
## Alternativet                0.8847         0.9793
## Dansk Folkeparti           -0.1401        -0.0456
## Enhedslisten                0.0771         0.1717
## Konservative Folkeparti     0.1325         0.2270
## Kristendemokraterne        -0.3717        -0.2771
## Liberal Alliance            0.1400         0.2346
## Nye Borgerlige             -0.0133         0.0813
## Radikale Venstre            0.4840         0.5786
## Socialistisk Folkeparti    -0.0415         0.0530
## Venstre                    -0.1424        -0.0478
## Vote blank                      NA         0.0946
## Would not vote              0.0946             NA
SMD_matrix(d$stereotype_accuracy_noSYR, d$party_vote)
##                         Socialdemokraterne Alternativet Dansk Folkeparti
## Socialdemokraterne                      NA      0.27664           0.0629
## Alternativet                        0.2766           NA          -0.2137
## Dansk Folkeparti                    0.0629     -0.21371               NA
## Enhedslisten                        0.1281     -0.14859           0.0651
## Konservative Folkeparti            -0.1764     -0.45308          -0.2394
## Kristendemokraterne                -0.4321     -0.70873          -0.4950
## Liberal Alliance                   -0.2628     -0.53946          -0.3258
## Nye Borgerlige                      0.2823      0.00564           0.2194
## Radikale Venstre                    0.1533     -0.12339           0.0903
## Socialistisk Folkeparti             0.4384      0.16177           0.3755
## Venstre                            -0.1747     -0.45134          -0.2376
## Vote blank                          0.2994      0.02274           0.2365
## Would not vote                      0.2879      0.01124           0.2249
##                         Enhedslisten Konservative Folkeparti
## Socialdemokraterne            0.1281                -0.17644
## Alternativet                 -0.1486                -0.45308
## Dansk Folkeparti              0.0651                -0.23937
## Enhedslisten                      NA                -0.30450
## Konservative Folkeparti      -0.3045                      NA
## Kristendemokraterne          -0.5601                -0.25565
## Liberal Alliance             -0.3909                -0.08638
## Nye Borgerlige                0.1542                 0.45872
## Radikale Venstre              0.0252                 0.32969
## Socialistisk Folkeparti       0.3104                 0.61485
## Venstre                      -0.3028                 0.00174
## Vote blank                    0.1713                 0.47582
## Would not vote                0.1598                 0.46432
##                         Kristendemokraterne Liberal Alliance Nye Borgerlige
## Socialdemokraterne                   -0.432          -0.2628        0.28228
## Alternativet                         -0.709          -0.5395        0.00564
## Dansk Folkeparti                     -0.495          -0.3258        0.21935
## Enhedslisten                         -0.560          -0.3909        0.15422
## Konservative Folkeparti              -0.256          -0.0864        0.45872
## Kristendemokraterne                      NA           0.1693        0.71437
## Liberal Alliance                      0.169               NA        0.54510
## Nye Borgerlige                        0.714           0.5451             NA
## Radikale Venstre                      0.585           0.4161       -0.12902
## Socialistisk Folkeparti               0.870           0.7012        0.15613
## Venstre                               0.257           0.0881       -0.45698
## Vote blank                            0.731           0.5622        0.01710
## Would not vote                        0.720           0.5507        0.00560
##                         Radikale Venstre Socialistisk Folkeparti  Venstre
## Socialdemokraterne                0.1533                   0.438 -0.17470
## Alternativet                     -0.1234                   0.162 -0.45134
## Dansk Folkeparti                  0.0903                   0.375 -0.23762
## Enhedslisten                      0.0252                   0.310 -0.30275
## Konservative Folkeparti           0.3297                   0.615  0.00174
## Kristendemokraterne               0.5853                   0.870  0.25739
## Liberal Alliance                  0.4161                   0.701  0.08813
## Nye Borgerlige                   -0.1290                   0.156 -0.45698
## Radikale Venstre                      NA                   0.285 -0.32795
## Socialistisk Folkeparti           0.2852                      NA -0.61311
## Venstre                          -0.3280                  -0.613       NA
## Vote blank                        0.1461                  -0.139  0.47408
## Would not vote                    0.1346                  -0.151  0.46257
##                         Vote blank Would not vote
## Socialdemokraterne          0.2994         0.2879
## Alternativet                0.0227         0.0112
## Dansk Folkeparti            0.2365         0.2249
## Enhedslisten                0.1713         0.1598
## Konservative Folkeparti     0.4758         0.4643
## Kristendemokraterne         0.7315         0.7200
## Liberal Alliance            0.5622         0.5507
## Nye Borgerlige              0.0171         0.0056
## Radikale Venstre            0.1461         0.1346
## Socialistisk Folkeparti    -0.1390        -0.1505
## Venstre                     0.4741         0.4626
## Vote blank                      NA        -0.0115
## Would not vote             -0.0115             NA
SMD_matrix(d$Muslim_bias_r_noSYR, d$party_vote)
##                         Socialdemokraterne Alternativet Dansk Folkeparti
## Socialdemokraterne                      NA       0.2726          -0.2979
## Alternativet                         0.273           NA          -0.5705
## Dansk Folkeparti                    -0.298      -0.5705               NA
## Enhedslisten                         0.193      -0.0796           0.4908
## Konservative Folkeparti             -0.452      -0.7243          -0.1539
## Kristendemokraterne                 -0.461      -0.7333          -0.1629
## Liberal Alliance                    -0.531      -0.8035          -0.2330
## Nye Borgerlige                      -0.361      -0.6336          -0.0631
## Radikale Venstre                    -0.104      -0.3767           0.1937
## Socialistisk Folkeparti              0.186      -0.0869           0.4835
## Venstre                             -0.216      -0.4882           0.0823
## Vote blank                           0.176      -0.0969           0.4735
## Would not vote                      -0.126      -0.3981           0.1723
##                         Enhedslisten Konservative Folkeparti
## Socialdemokraterne           0.19293                -0.45174
## Alternativet                -0.07965                -0.72432
## Dansk Folkeparti             0.49081                -0.15386
## Enhedslisten                      NA                -0.64467
## Konservative Folkeparti     -0.64467                      NA
## Kristendemokraterne         -0.65369                -0.00902
## Liberal Alliance            -0.72386                -0.07919
## Nye Borgerlige              -0.55391                 0.09076
## Radikale Venstre            -0.29707                 0.34760
## Socialistisk Folkeparti     -0.00728                 0.63739
## Venstre                     -0.40853                 0.23614
## Vote blank                  -0.01727                 0.62740
## Would not vote              -0.31850                 0.32617
##                         Kristendemokraterne Liberal Alliance Nye Borgerlige
## Socialdemokraterne                 -0.46076          -0.5309        -0.3610
## Alternativet                       -0.73334          -0.8035        -0.6336
## Dansk Folkeparti                   -0.16288          -0.2330        -0.0631
## Enhedslisten                       -0.65369          -0.7239        -0.5539
## Konservative Folkeparti            -0.00902          -0.0792         0.0908
## Kristendemokraterne                      NA          -0.0702         0.0998
## Liberal Alliance                   -0.07017               NA         0.1699
## Nye Borgerlige                      0.09978           0.1699             NA
## Radikale Venstre                    0.35662           0.4268         0.2568
## Socialistisk Folkeparti             0.64641           0.7166         0.5466
## Venstre                             0.24516           0.3153         0.1454
## Vote blank                          0.63642           0.7066         0.5366
## Would not vote                      0.33519           0.4054         0.2354
##                         Radikale Venstre Socialistisk Folkeparti Venstre
## Socialdemokraterne               -0.1041                 0.18565 -0.2156
## Alternativet                     -0.3767                -0.08693 -0.4882
## Dansk Folkeparti                  0.1937                 0.48353  0.0823
## Enhedslisten                     -0.2971                -0.00728 -0.4085
## Konservative Folkeparti           0.3476                 0.63739  0.2361
## Kristendemokraterne               0.3566                 0.64641  0.2452
## Liberal Alliance                  0.4268                 0.71657  0.3153
## Nye Borgerlige                    0.2568                 0.54663  0.1454
## Radikale Venstre                      NA                 0.28979 -0.1115
## Socialistisk Folkeparti           0.2898                      NA -0.4013
## Venstre                          -0.1115                -0.40125      NA
## Vote blank                        0.2798                -0.00999  0.3913
## Would not vote                   -0.0214                -0.31121  0.0900
##                         Vote blank Would not vote
## Socialdemokraterne         0.17566        -0.1256
## Alternativet              -0.09692        -0.3981
## Dansk Folkeparti           0.47354         0.1723
## Enhedslisten              -0.01727        -0.3185
## Konservative Folkeparti    0.62740         0.3262
## Kristendemokraterne        0.63642         0.3352
## Liberal Alliance           0.70658         0.4054
## Nye Borgerlige             0.53664         0.2354
## Radikale Venstre           0.27980        -0.0214
## Socialistisk Folkeparti   -0.00999        -0.3112
## Venstre                    0.39126         0.0900
## Vote blank                      NA        -0.3012
## Would not vote            -0.30122             NA
SMD_matrix(d$Muslim_preference_noSYR, d$party_vote)
##                         Socialdemokraterne Alternativet Dansk Folkeparti
## Socialdemokraterne                      NA       -0.976         -0.00643
## Alternativet                      -0.97608           NA          0.96965
## Dansk Folkeparti                  -0.00643        0.970               NA
## Enhedslisten                      -0.20590        0.770         -0.19947
## Konservative Folkeparti           -0.27258        0.704         -0.26615
## Kristendemokraterne                0.20832        1.184          0.21475
## Liberal Alliance                  -0.29318        0.683         -0.28675
## Nye Borgerlige                    -0.11862        0.857         -0.11219
## Radikale Venstre                  -0.62852        0.348         -0.62209
## Socialistisk Folkeparti           -0.05726        0.919         -0.05083
## Venstre                           -0.00883        0.967         -0.00240
## Vote blank                        -0.16802        0.808         -0.16159
## Would not vote                    -0.06955        0.907         -0.06312
##                         Enhedslisten Konservative Folkeparti
## Socialdemokraterne           -0.2059                 -0.2726
## Alternativet                  0.7702                  0.7035
## Dansk Folkeparti             -0.1995                 -0.2661
## Enhedslisten                      NA                 -0.0667
## Konservative Folkeparti      -0.0667                      NA
## Kristendemokraterne           0.4142                  0.4809
## Liberal Alliance             -0.0873                 -0.0206
## Nye Borgerlige                0.0873                  0.1540
## Radikale Venstre             -0.4226                 -0.3559
## Socialistisk Folkeparti       0.1486                  0.2153
## Venstre                       0.1971                  0.2637
## Vote blank                    0.0379                  0.1046
## Would not vote                0.1363                  0.2030
##                         Kristendemokraterne Liberal Alliance Nye Borgerlige
## Socialdemokraterne                    0.208          -0.2932        -0.1186
## Alternativet                          1.184           0.6829         0.8575
## Dansk Folkeparti                      0.215          -0.2867        -0.1122
## Enhedslisten                          0.414          -0.0873         0.0873
## Konservative Folkeparti               0.481          -0.0206         0.1540
## Kristendemokraterne                      NA          -0.5015        -0.3269
## Liberal Alliance                     -0.501               NA         0.1746
## Nye Borgerlige                       -0.327           0.1746             NA
## Radikale Venstre                     -0.837          -0.3353        -0.5099
## Socialistisk Folkeparti              -0.266           0.2359         0.0614
## Venstre                              -0.217           0.2843         0.1098
## Vote blank                           -0.376           0.1252        -0.0494
## Would not vote                       -0.278           0.2236         0.0491
##                         Radikale Venstre Socialistisk Folkeparti  Venstre
## Socialdemokraterne                -0.629                 -0.0573 -0.00883
## Alternativet                       0.348                  0.9188  0.96724
## Dansk Folkeparti                  -0.622                 -0.0508 -0.00240
## Enhedslisten                      -0.423                  0.1486  0.19707
## Konservative Folkeparti           -0.356                  0.2153  0.26374
## Kristendemokraterne               -0.837                 -0.2656 -0.21715
## Liberal Alliance                  -0.335                  0.2359  0.28434
## Nye Borgerlige                    -0.510                  0.0614  0.10979
## Radikale Venstre                      NA                  0.5713  0.61969
## Socialistisk Folkeparti            0.571                      NA  0.04842
## Venstre                            0.620                  0.0484       NA
## Vote blank                         0.461                 -0.1108 -0.15918
## Would not vote                     0.559                 -0.0123 -0.06072
##                         Vote blank Would not vote
## Socialdemokraterne         -0.1680        -0.0696
## Alternativet                0.8081         0.9065
## Dansk Folkeparti           -0.1616        -0.0631
## Enhedslisten                0.0379         0.1363
## Konservative Folkeparti     0.1046         0.2030
## Kristendemokraterne        -0.3763        -0.2779
## Liberal Alliance            0.1252         0.2236
## Nye Borgerlige             -0.0494         0.0491
## Radikale Venstre            0.4605         0.5590
## Socialistisk Folkeparti    -0.1108        -0.0123
## Venstre                    -0.1592        -0.0607
## Vote blank                      NA         0.0985
## Would not vote              0.0985             NA
#simplified coding
#with SYR
rms::ols(stereotype_accuracy ~ block_vote, data = d)
## Linear Regression Model
##  
##  rms::ols(formula = stereotype_accuracy ~ block_vote, data = d)
##  
##                 Model Likelihood     Discrimination    
##                    Ratio Test           Indexes        
##  Obs     476    LR chi2      7.59    R2       0.016    
##  sigma0.1977    d.f.            3    R2 adj   0.010    
##  d.f.    472    Pr(> chi2) 0.0553    g        0.026    
##  
##  Residuals
##  
##       Min       1Q   Median       3Q      Max 
##  -1.05708 -0.06121  0.04658  0.12682  0.31228 
##  
##  
##                            Coef    S.E.   t     Pr(>|t|)
##  Intercept                  0.6064 0.0139 43.70 <0.0001 
##  block_vote=left-block     -0.0441 0.0196 -2.25 0.0246  
##  block_vote=Vote blank     -0.0656 0.0326 -2.01 0.0446  
##  block_vote=Would not vote -0.0578 0.0435 -1.33 0.1846  
## 
rms::ols(Muslim_bias_r ~ block_vote, data = d)
## Linear Regression Model
##  
##  rms::ols(formula = Muslim_bias_r ~ block_vote, data = d)
##  
##                 Model Likelihood     Discrimination    
##                    Ratio Test           Indexes        
##  Obs     476    LR chi2     19.86    R2       0.041    
##  sigma0.1579    d.f.            3    R2 adj   0.035    
##  d.f.    472    Pr(> chi2) 0.0002    g        0.034    
##  
##  Residuals
##  
##       Min       1Q   Median       3Q      Max 
##  -0.29360 -0.12292 -0.01088  0.10494  0.48637 
##  
##  
##                            Coef    S.E.   t      Pr(>|t|)
##  Intercept                 -0.4495 0.0111 -40.57 <0.0001 
##  block_vote=left-block     -0.0635 0.0156  -4.06 <0.0001 
##  block_vote=Vote blank     -0.0789 0.0260  -3.03 0.0026  
##  block_vote=Would not vote -0.0344 0.0347  -0.99 0.3230  
## 
rms::ols(Muslim_preference ~ block_vote, data = d)
## Frequencies of Missing Values Due to Each Variable
## Muslim_preference        block_vote 
##               108                 0 
## 
## Linear Regression Model
##  
##  rms::ols(formula = Muslim_preference ~ block_vote, data = d)
##  
##  
##                 Model Likelihood     Discrimination    
##                    Ratio Test           Indexes        
##  Obs     368    LR chi2      4.81    R2       0.013    
##  sigma0.4224    d.f.            3    R2 adj   0.005    
##  d.f.    364    Pr(> chi2) 0.1860    g        0.050    
##  
##  Residuals
##  
##       Min       1Q   Median       3Q      Max 
##  -0.84521 -0.29235 -0.04535  0.27061  1.26140 
##  
##  
##                            Coef    S.E.   t     Pr(>|t|)
##  Intercept                 -0.2551 0.0329 -7.76 <0.0001 
##  block_vote=left-block      0.1003 0.0472  2.12 0.0344  
##  block_vote=Vote blank      0.0331 0.0816  0.41 0.6854  
##  block_vote=Would not vote -0.0063 0.1106 -0.06 0.9546  
## 
#without
rms::ols(stereotype_accuracy_noSYR ~ block_vote, data = d)
## Frequencies of Missing Values Due to Each Variable
## stereotype_accuracy_noSYR                block_vote 
##                         2                         0 
## 
## Linear Regression Model
##  
##  rms::ols(formula = stereotype_accuracy_noSYR ~ block_vote, data = d)
##  
##  
##                 Model Likelihood     Discrimination    
##                    Ratio Test           Indexes        
##  Obs     474    LR chi2      7.69    R2       0.016    
##  sigma0.2079    d.f.            3    R2 adj   0.010    
##  d.f.    470    Pr(> chi2) 0.0528    g        0.028    
##  
##  Residuals
##  
##       Min       1Q   Median       3Q      Max 
##  -1.16439 -0.06770  0.05161  0.12999  0.28290 
##  
##  
##                            Coef    S.E.   t     Pr(>|t|)
##  Intercept                  0.6268 0.0146 42.85 <0.0001 
##  block_vote=left-block     -0.0423 0.0206 -2.05 0.0408  
##  block_vote=Vote blank     -0.0729 0.0343 -2.13 0.0339  
##  block_vote=Would not vote -0.0705 0.0458 -1.54 0.1239  
## 
rms::ols(Muslim_bias_r_noSYR ~ block_vote, data = d)
## Linear Regression Model
##  
##  rms::ols(formula = Muslim_bias_r_noSYR ~ block_vote, data = d)
##  
##                 Model Likelihood     Discrimination    
##                    Ratio Test           Indexes        
##  Obs     476    LR chi2     21.06    R2       0.043    
##  sigma0.1590    d.f.            3    R2 adj   0.037    
##  d.f.    472    Pr(> chi2) 0.0001    g        0.035    
##  
##  Residuals
##  
##       Min       1Q   Median       3Q      Max 
##  -0.29752 -0.12214 -0.01422  0.09913  0.54025 
##  
##  
##                            Coef    S.E.   t      Pr(>|t|)
##  Intercept                 -0.4902 0.0112 -43.92 <0.0001 
##  block_vote=left-block     -0.0663 0.0157  -4.21 <0.0001 
##  block_vote=Vote blank     -0.0806 0.0262  -3.08 0.0022  
##  block_vote=Would not vote -0.0325 0.0350  -0.93 0.3530  
## 
rms::ols(Muslim_preference_noSYR ~ block_vote, data = d)
## Frequencies of Missing Values Due to Each Variable
## Muslim_preference_noSYR              block_vote 
##                     114                       0 
## 
## Linear Regression Model
##  
##  rms::ols(formula = Muslim_preference_noSYR ~ block_vote, data = d)
##  
##  
##                 Model Likelihood     Discrimination    
##                    Ratio Test           Indexes        
##  Obs     362    LR chi2      2.89    R2       0.008    
##  sigma0.4244    d.f.            3    R2 adj   0.000    
##  d.f.    358    Pr(> chi2) 0.4094    g        0.039    
##  
##  Residuals
##  
##       Min       1Q   Median       3Q      Max 
##  -0.82491 -0.29259 -0.04352  0.25378  1.25456 
##  
##  
##                            Coef    S.E.   t     Pr(>|t|)
##  Intercept                 -0.2546 0.0331 -7.68 <0.0001 
##  block_vote=left-block      0.0795 0.0480  1.66 0.0984  
##  block_vote=Vote blank      0.0424 0.0820  0.52 0.6052  
##  block_vote=Would not vote  0.0012 0.1112  0.01 0.9911  
## 
#cohen d
cohen.d(d %>% select(stereotype_accuracy, stereotype_accuracy_noSYR, Muslim_bias_r, Muslim_bias_r_noSYR, Muslim_preference, Muslim_preference_noSYR, block_vote), "block_vote") %>% 
  {
    cat("p values for gaps:\n")
    print(.$p)
    .
  }
## p values for gaps:
##       stereotype_accuracy stereotype_accuracy_noSYR             Muslim_bias_r 
##                  2.44e-02                  3.96e-02                  5.54e-05 
##       Muslim_bias_r_noSYR         Muslim_preference   Muslim_preference_noSYR 
##                  3.21e-05                  3.16e-02                  9.35e-02
## Call: cohen.d(x = d %>% select(stereotype_accuracy, stereotype_accuracy_noSYR, 
##     Muslim_bias_r, Muslim_bias_r_noSYR, Muslim_preference, Muslim_preference_noSYR, 
##     block_vote), group = "block_vote")
## Cohen d statistic of difference between two means
##                           lower effect upper
## stereotype_accuracy       -0.42  -0.22 -0.03
## stereotype_accuracy_noSYR -0.40  -0.20 -0.01
## Muslim_bias_r             -0.60  -0.40 -0.20
## Muslim_bias_r_noSYR       -0.61  -0.42 -0.22
## Muslim_preference          0.02   0.24  0.46
## Muslim_preference_noSYR   -0.03   0.19  0.41
## 
## Multivariate (Mahalanobis) distance between groups
## [1] 0.65
## r equivalent of difference between two means
##       stereotype_accuracy stereotype_accuracy_noSYR             Muslim_bias_r 
##                     -0.11                     -0.10                     -0.20 
##       Muslim_bias_r_noSYR         Muslim_preference   Muslim_preference_noSYR 
##                     -0.20                      0.12                      0.09
#plot party means
GG_group_means(d, "stereotype_accuracy", "party_vote") +
  theme(axis.text.x = element_text(angle = -80, hjust = 0)) +
  scale_y_continuous("Stereotype accuracy\n(pearson r)") +
  scale_x_discrete("Intended party")
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.

GG_save("figures/accuracy_by_party.png")

GG_group_means(d %>% df_standardize(messages = F), "stereotype_accuracy", "party_vote") +
  theme(axis.text.x = element_text(angle = -80, hjust = 0)) +
  scale_y_continuous("Stereotype accuracy\n(pearson r, standardized)") +
  scale_x_discrete("Intended party")
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.

GG_save("figures/accuracy_by_party_standardized.png")

GG_group_means(d, "Muslim_bias_r", "party_vote") +
  theme(axis.text.x = element_text(angle = -80, hjust = 0)) +
  scale_y_continuous("Muslim bias in steroetype") +
  scale_x_discrete("Intended party")
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.

GG_save("figures/Muslim_bias_by_party.png")

GG_group_means(d %>% df_standardize(messages = F), "Muslim_bias_r", "party_vote") +
  theme(axis.text.x = element_text(angle = -80, hjust = 0)) +
  scale_y_continuous("Muslim bias in steroetype (standardized)") +
  scale_x_discrete("Intended party")
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.

GG_save("figures/Muslim_bias_by_party_standardized.png")

GG_group_means(d, "Muslim_preference", "party_vote") +
  theme(axis.text.x = element_text(angle = -80, hjust = 0)) +
  scale_y_continuous("Muslim preference") +
  scale_x_discrete("Intended party")
## Missing values were removed.
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.

GG_save("figures/Muslim_preference_by_party.png")

GG_group_means(d %>% df_standardize(messages = F), "Muslim_preference", "party_vote") +
  theme(axis.text.x = element_text(angle = -80, hjust = 0)) +
  scale_y_continuous("Muslim preference (standardized)") +
  scale_x_discrete("Intended party")
## Missing values were removed.
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.

GG_save("figures/Muslim_preference_by_party_standardized.png")

#no SYR
GG_group_means(d, "stereotype_accuracy_noSYR", "party_vote") +
  theme(axis.text.x = element_text(angle = -80, hjust = 0)) +
  scale_y_continuous("Stereotype accuracy\n(pearson r)") +
  scale_x_discrete("Intended party") +
  labs(caption = "Syria excluded")
## Missing values were removed.
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.

GG_save("figures/accuracy_by_party_noSYR.png")

GG_group_means(d %>% df_standardize(messages = F), "stereotype_accuracy_noSYR", "party_vote") +
  theme(axis.text.x = element_text(angle = -80, hjust = 0)) +
  scale_y_continuous("Stereotype accuracy\n(pearson r, standardized)") +
  scale_x_discrete("Intended party") +
  labs(caption = "Syria excluded")
## Missing values were removed.
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.

GG_save("figures/accuracy_by_party_noSYR_standardized.png")

GG_group_means(d, "Muslim_bias_r_noSYR", "party_vote") +
  theme(axis.text.x = element_text(angle = -80, hjust = 0)) +
  scale_y_continuous("Muslim bias in steroetype") +
  scale_x_discrete("Intended party") +
  labs(caption = "Syria excluded")
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.

GG_save("figures/Muslim_bias_by_party_noSYR.png")

GG_group_means(d %>% df_standardize(messages = F), "Muslim_bias_r_noSYR", "party_vote") +
  theme(axis.text.x = element_text(angle = -80, hjust = 0)) +
  scale_y_continuous("Muslim bias in steroetype (standardized)") +
  scale_x_discrete("Intended party") +
  labs(caption = "Syria excluded")
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.

GG_save("figures/Muslim_bias_by_party_noSYR_standardized.png")

GG_group_means(d, "Muslim_preference_noSYR", "party_vote") +
  theme(axis.text.x = element_text(angle = -80, hjust = 0)) +
  scale_y_continuous("Muslim preference") +
  scale_x_discrete("Intended party") +
  labs(caption = "Syria excluded")
## Missing values were removed.
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.

GG_save("figures/Muslim_preference_by_party_noSYR.png")

GG_group_means(d %>% df_standardize(messages = F), "Muslim_preference_noSYR", "party_vote") +
  theme(axis.text.x = element_text(angle = -80, hjust = 0)) +
  scale_y_continuous("Muslim preference (standardized)") +
  scale_x_discrete("Intended party") +
  labs(caption = "Syria excluded")
## Missing values were removed.
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.

GG_save("figures/Muslim_preference_by_party_noSYR_standardized.png")

Order of presentation

#order of presentation
rms::ols(stereotype_accuracy ~ path_parsed, data = d)
## Linear Regression Model
##  
##  rms::ols(formula = stereotype_accuracy ~ path_parsed, data = d)
##  
##                 Model Likelihood     Discrimination    
##                    Ratio Test           Indexes        
##  Obs     476    LR chi2      2.64    R2       0.006    
##  sigma0.1992    d.f.            5    R2 adj  -0.005    
##  d.f.    470    Pr(> chi2) 0.7547    g        0.017    
##  
##  Residuals
##  
##       Min       1Q   Median       3Q      Max 
##  -1.02251 -0.06574  0.04677  0.12806  0.28813 
##  
##  
##                  Coef    S.E.   t     Pr(>|t|)
##  Intercept        0.5857 0.0224 26.14 <0.0001 
##  path_parsed=ACB  0.0151 0.0317  0.48 0.6334  
##  path_parsed=BAC -0.0304 0.0310 -0.98 0.3279  
##  path_parsed=BCA -0.0081 0.0305 -0.27 0.7900  
##  path_parsed=CAB  0.0006 0.0332  0.02 0.9846  
##  path_parsed=CBA -0.0189 0.0323 -0.58 0.5594  
## 
rms::ols(Muslim_bias_r ~ path_parsed, data = d)
## Linear Regression Model
##  
##  rms::ols(formula = Muslim_bias_r ~ path_parsed, data = d)
##  
##                 Model Likelihood     Discrimination    
##                    Ratio Test           Indexes        
##  Obs     476    LR chi2      5.55    R2       0.012    
##  sigma0.1606    d.f.            5    R2 adj   0.001    
##  d.f.    470    Pr(> chi2) 0.3521    g        0.019    
##  
##  Residuals
##  
##       Min       1Q   Median       3Q      Max 
##  -0.27694 -0.12006 -0.01166  0.10619  0.50347 
##  
##  
##                  Coef    S.E.   t      Pr(>|t|)
##  Intercept       -0.4852 0.0181 -26.85 <0.0001 
##  path_parsed=ACB  0.0205 0.0256   0.80 0.4235  
##  path_parsed=BAC -0.0097 0.0250  -0.39 0.6978  
##  path_parsed=BCA  0.0061 0.0246   0.25 0.8029  
##  path_parsed=CAB  0.0125 0.0268   0.47 0.6410  
##  path_parsed=CBA -0.0346 0.0261  -1.33 0.1849  
## 
rms::ols(Muslim_preference ~ path_parsed, data = d)
## Frequencies of Missing Values Due to Each Variable
## Muslim_preference       path_parsed 
##               108                 0 
## 
## Linear Regression Model
##  
##  rms::ols(formula = Muslim_preference ~ path_parsed, data = d)
##  
##  
##                 Model Likelihood     Discrimination    
##                    Ratio Test           Indexes        
##  Obs     368    LR chi2      3.91    R2       0.011    
##  sigma0.4241    d.f.            5    R2 adj  -0.003    
##  d.f.    362    Pr(> chi2) 0.5618    g        0.047    
##  
##  Residuals
##  
##       Min       1Q   Median       3Q      Max 
##  -0.81406 -0.29094 -0.05197  0.27470  1.22128 
##  
##  
##                  Coef    S.E.   t     Pr(>|t|)
##  Intercept       -0.2213 0.0562 -3.94 <0.0001 
##  path_parsed=ACB -0.0398 0.0772 -0.52 0.6063  
##  path_parsed=BAC -0.0242 0.0764 -0.32 0.7514  
##  path_parsed=BCA  0.0353 0.0741  0.48 0.6337  
##  path_parsed=CAB  0.1037 0.0841  1.23 0.2183  
##  path_parsed=CBA  0.0130 0.0794  0.16 0.8699  
## 
rms::ols(stereotype_accuracy_noSYR ~ path_parsed, data = d)
## Frequencies of Missing Values Due to Each Variable
## stereotype_accuracy_noSYR               path_parsed 
##                         2                         0 
## 
## Linear Regression Model
##  
##  rms::ols(formula = stereotype_accuracy_noSYR ~ path_parsed, data = d)
##  
##  
##                 Model Likelihood     Discrimination    
##                    Ratio Test           Indexes        
##  Obs     474    LR chi2      1.29    R2       0.003    
##  sigma0.2098    d.f.            5    R2 adj  -0.008    
##  d.f.    468    Pr(> chi2) 0.9355    g        0.012    
##  
##  Residuals
##  
##       Min       1Q   Median       3Q      Max 
##  -1.12539 -0.06203  0.05790  0.13341  0.28529 
##  
##  
##                  Coef    S.E.   t     Pr(>|t|)
##  Intercept        0.6126 0.0236 25.96 <0.0001 
##  path_parsed=ACB -0.0022 0.0334 -0.07 0.9465  
##  path_parsed=BAC -0.0248 0.0327 -0.76 0.4481  
##  path_parsed=BCA -0.0281 0.0323 -0.87 0.3836  
##  path_parsed=CAB -0.0103 0.0350 -0.29 0.7694  
##  path_parsed=CBA -0.0174 0.0341 -0.51 0.6106  
## 
rms::ols(Muslim_bias_r_noSYR ~ path_parsed, data = d)
## Linear Regression Model
##  
##  rms::ols(formula = Muslim_bias_r_noSYR ~ path_parsed, data = d)
##  
##                 Model Likelihood     Discrimination    
##                    Ratio Test           Indexes        
##  Obs     476    LR chi2      4.96    R2       0.010    
##  sigma0.1621    d.f.            5    R2 adj   0.000    
##  d.f.    470    Pr(> chi2) 0.4208    g        0.017    
##  
##  Residuals
##  
##       Min       1Q   Median       3Q      Max 
##  -0.28542 -0.11790 -0.01894  0.09918  0.53380 
##  
##  
##                  Coef    S.E.   t      Pr(>|t|)
##  Intercept       -0.5263 0.0182 -28.86 <0.0001 
##  path_parsed=ACB  0.0151 0.0258   0.59 0.5578  
##  path_parsed=BAC -0.0087 0.0253  -0.34 0.7316  
##  path_parsed=BCA  0.0065 0.0248   0.26 0.7926  
##  path_parsed=CAB  0.0116 0.0270   0.43 0.6673  
##  path_parsed=CBA -0.0357 0.0263  -1.36 0.1752  
## 
rms::ols(Muslim_preference_noSYR ~ path_parsed, data = d)
## Frequencies of Missing Values Due to Each Variable
## Muslim_preference_noSYR             path_parsed 
##                     114                       0 
## 
## Linear Regression Model
##  
##  rms::ols(formula = Muslim_preference_noSYR ~ path_parsed, data = d)
##  
##  
##                 Model Likelihood     Discrimination    
##                    Ratio Test           Indexes        
##  Obs     362    LR chi2      4.91    R2       0.013    
##  sigma0.4244    d.f.            5    R2 adj   0.000    
##  d.f.    356    Pr(> chi2) 0.4268    g        0.053    
##  
##  Residuals
##  
##       Min       1Q   Median       3Q      Max 
##  -0.81910 -0.28468 -0.04521  0.24950  1.26611 
##  
##  
##                  Coef    S.E.   t     Pr(>|t|)
##  Intercept       -0.2363 0.0562 -4.20 <0.0001 
##  path_parsed=ACB -0.0264 0.0773 -0.34 0.7327  
##  path_parsed=BAC -0.0298 0.0773 -0.39 0.7004  
##  path_parsed=BCA  0.0554 0.0744  0.75 0.4564  
##  path_parsed=CAB  0.1209 0.0846  1.43 0.1540  
##  path_parsed=CBA  0.0115 0.0799  0.14 0.8854  
## 

Reviewer requests

#summary stats of estimates by country
d_econ_num_sumstats = d_econ_num %>% map_df(function(dd) {
  describe(dd) %>% 
    as.data.frame() %>% 
    mutate(
      prop_1 = mean(dd == 1),
      prop_7 = mean(dd == 7),
    )
}) %>% 
  select(mean, median, sd, mad, skew, kurtosis, prop_1, prop_7) %>% 
  df_add_affix(prefix = "est_")

#summary stats of preferences by country
d_prefs_num_sumstats = d_prefs_num_alt %>% map_df(function(dd) {
  describe(dd) %>% 
    as.data.frame() %>% 
    mutate(
      prop_1 = mean(dd == 1),
      prop_4 = mean(dd == 4),
    )
}) %>% 
  select(mean, median, sd, mad, skew, kurtosis, prop_1, prop_4) %>% 
  df_add_affix(prefix = "pref_")

#split age into 3 groups
d$age_bin = d$age %>% discretize(3, equal_range = F)

#join with data
d_reviewer = cbind(
  #estimates
  d_econ_num_sumstats,
  est_mean_young = colMeans(d_econ_num[d$age_bin == "[18,31]", ]),
  est_mean_middle = colMeans(d_econ_num[d$age_bin == "(31,46]", ]),
  est_mean_old = colMeans(d_econ_num[d$age_bin == "(46,65]", ]),
  #prefs
  d_prefs_num_sumstats,
  pref_mean_young = colMeans(d_prefs_num[d$age_bin == "[18,31]", ]),
  pref_mean_middle = colMeans(d_prefs_num[d$age_bin == "(31,46]", ]),
  pref_mean_old = colMeans(d_prefs_num[d$age_bin == "(46,65]", ]),
  #covariates
  muslim = dk_fiscal_sub$Muslim,
  net_fiscal = dk_fiscal_sub$dnk_net_fiscal_contribution_2014
)

#nice print
d_reviewer %>% df_round() %>% DT::datatable()
#estimates by age group in plot
d_reviewer %>% 
  select(est_mean_young:est_mean_old) %>% 
  mutate(name = dk_fiscal_sub$Names) %>% 
  gather(key = age_group, value = mean, -name) %>% 
  mutate(age_group = ordered(age_group, levels = c("est_mean_young", "est_mean_middle", "est_mean_old"))) %>%
  ggplot(aes(name, mean, fill = age_group)) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_x_discrete(NULL, guide = guide_axis(angle = -28)) +
  ylab("Mean estimate of net fiscal contribution") +
  scale_fill_discrete("Age group", labels = levels(d$age_bin))

GG_save("figures/age_groups_estimates.png")

#preferences by age group in plot
d_reviewer %>% 
  select(pref_mean_young:pref_mean_old) %>% 
  mutate(name = dk_fiscal_sub$Names) %>% 
  gather(key = age_group, value = mean, -name) %>% 
  mutate(age_group = ordered(age_group, levels = c("pref_mean_young", "pref_mean_middle", "pref_mean_old"))) %>% 
  ggplot(aes(name, mean, fill = age_group)) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_x_discrete(NULL, guide = guide_axis(angle = -28)) +
  ylab("Net opposition") +
  scale_fill_discrete("Age group", labels = levels(d$age_bin))

GG_save("figures/age_groups_preferences.png")

#summary stats
d_reviewer %>% 
  select(est_mean_young:est_mean_old, pref_mean_young:pref_mean_old) %>% 
  describe()
##                  vars  n    mean    sd  median trimmed   mad    min   max range
## est_mean_young      1 32  3.8469 1.026  3.7000  3.8514 1.274  2.087 5.450  3.36
## est_mean_middle     2 32  3.9407 1.052  3.8690  3.9675 1.377  1.982 5.530  3.55
## est_mean_old        3 32  3.7403 1.231  3.5372  3.7256 1.538  1.696 5.696  4.00
## pref_mean_young     4 32 -0.2734 0.350 -0.2125 -0.2755 0.389 -0.800 0.263  1.06
## pref_mean_middle    5 32 -0.1581 0.481 -0.0536 -0.1525 0.556 -0.857 0.607  1.46
## pref_mean_old       6 32  0.0173 0.556  0.2095  0.0385 0.551 -0.838 0.730  1.57
##                     skew kurtosis     se
## est_mean_young    0.1121    -1.22 0.1813
## est_mean_middle  -0.0424    -1.19 0.1859
## est_mean_old      0.2138    -1.30 0.2177
## pref_mean_young  -0.0832    -1.39 0.0619
## pref_mean_middle -0.2295    -1.43 0.0850
## pref_mean_old    -0.4061    -1.49 0.0983
#cors
d_reviewer %>% 
  wtd.cors()
##                  est_mean est_median    est_sd est_mad est_skew est_kurtosis
## est_mean            1.000      0.970 -0.329787     Inf   -0.976        0.641
## est_median          0.970      1.000 -0.288226     Inf   -0.948        0.631
## est_sd             -0.330     -0.288  1.000000     Inf    0.299       -0.723
## est_mad               Inf        Inf       Inf     NaN      Inf          Inf
## est_skew           -0.976     -0.948  0.298506     Inf    1.000       -0.606
## est_kurtosis        0.641      0.631 -0.722663     Inf   -0.606        1.000
## est_prop_1         -0.862     -0.845 -0.029734     Inf    0.880       -0.210
## est_prop_7          0.859      0.847 -0.455670     Inf   -0.826        0.838
## est_mean_young      0.995      0.969 -0.351903    -Inf   -0.980        0.650
## est_mean_middle     0.997      0.968 -0.277612     Inf   -0.976        0.599
## est_mean_old        0.996      0.963 -0.356958     Inf   -0.961        0.668
## pref_mean           0.987      0.954 -0.410820     Inf   -0.945        0.689
## pref_median         0.799      0.768 -0.225471     Inf   -0.738        0.489
## pref_sd            -0.953     -0.892  0.429427     NaN    0.915       -0.627
## pref_mad           -0.845     -0.771  0.359725    -Inf    0.774       -0.602
## pref_skew          -0.958     -0.928  0.229719     NaN    0.932       -0.503
## pref_kurtosis       0.883      0.818 -0.633750    -Inf   -0.812        0.827
## pref_prop_1        -0.960     -0.930  0.189298     Inf    0.942       -0.460
## pref_prop_4         0.856      0.830 -0.609950     Inf   -0.804        0.880
## pref_mean_young    -0.967     -0.934  0.354461     NaN    0.936       -0.618
## pref_mean_middle   -0.980     -0.948  0.409438    -Inf    0.931       -0.673
## pref_mean_old      -0.970     -0.931  0.468535     Inf    0.915       -0.731
## muslim             -0.723     -0.710  0.000769    -Inf    0.703       -0.260
## net_fiscal          0.806      0.775 -0.040950    -Inf   -0.804        0.310
##                  est_prop_1 est_prop_7 est_mean_young est_mean_middle
## est_mean            -0.8623      0.859          0.995           0.997
## est_median          -0.8449      0.847          0.969           0.968
## est_sd              -0.0297     -0.456         -0.352          -0.278
## est_mad                 Inf        Inf           -Inf             Inf
## est_skew             0.8801     -0.826         -0.980          -0.976
## est_kurtosis        -0.2098      0.838          0.650           0.599
## est_prop_1           1.0000     -0.555         -0.857          -0.885
## est_prop_7          -0.5555      1.000          0.855           0.839
## est_mean_young      -0.8565      0.855          1.000           0.990
## est_mean_middle     -0.8854      0.839          0.990           1.000
## est_mean_old        -0.8359      0.872          0.985           0.991
## pref_mean           -0.8168      0.868          0.981           0.980
## pref_median         -0.6663      0.639          0.789           0.799
## pref_sd              0.8050     -0.807         -0.948          -0.948
## pref_mad             0.6524     -0.718         -0.835          -0.835
## pref_skew            0.9104     -0.738         -0.952          -0.965
## pref_kurtosis       -0.5753      0.872          0.878           0.859
## pref_prop_1          0.9430     -0.723         -0.952          -0.970
## pref_prop_4         -0.5206      0.946          0.855           0.829
## pref_mean_young      0.8391     -0.807         -0.969          -0.965
## pref_mean_middle     0.8153     -0.843         -0.974          -0.974
## pref_mean_old        0.7587     -0.870         -0.961          -0.958
## muslim               0.7690     -0.479         -0.717          -0.748
## net_fiscal          -0.8342      0.572          0.815           0.818
##                  est_mean_old pref_mean pref_median pref_sd pref_mad pref_skew
## est_mean                0.996     0.987       0.799  -0.953   -0.845    -0.958
## est_median              0.963     0.954       0.768  -0.892   -0.771    -0.928
## est_sd                 -0.357    -0.411      -0.225   0.429    0.360     0.230
## est_mad                   Inf       Inf         Inf     NaN     -Inf       NaN
## est_skew               -0.961    -0.945      -0.738   0.915    0.774     0.932
## est_kurtosis            0.668     0.689       0.489  -0.627   -0.602    -0.503
## est_prop_1             -0.836    -0.817      -0.666   0.805    0.652     0.910
## est_prop_7              0.872     0.868       0.639  -0.807   -0.718    -0.738
## est_mean_young          0.985     0.981       0.789  -0.948   -0.835    -0.952
## est_mean_middle         0.991     0.980       0.799  -0.948   -0.835    -0.965
## est_mean_old            1.000     0.988       0.800  -0.953   -0.855    -0.947
## pref_mean               0.988     1.000       0.826  -0.960   -0.865    -0.957
## pref_median             0.800     0.826       1.000  -0.780   -0.882    -0.860
## pref_sd                -0.953    -0.960      -0.780   1.000    0.836     0.928
## pref_mad               -0.855    -0.865      -0.882   0.836    1.000     0.863
## pref_skew              -0.947    -0.957      -0.860   0.928    0.863     1.000
## pref_kurtosis           0.902     0.927       0.740  -0.913   -0.835    -0.813
## pref_prop_1            -0.947    -0.946      -0.790   0.929    0.794     0.978
## pref_prop_4             0.875     0.896       0.656  -0.827   -0.740    -0.739
## pref_mean_young        -0.957    -0.979      -0.862   0.952    0.875     0.971
## pref_mean_middle       -0.982    -0.996      -0.840   0.953    0.874     0.962
## pref_mean_old          -0.979    -0.989      -0.840   0.951    0.897     0.935
## muslim                 -0.695    -0.715      -0.588   0.724    0.575     0.791
## net_fiscal              0.779     0.760       0.599  -0.815   -0.627    -0.796
##                  pref_kurtosis pref_prop_1 pref_prop_4 pref_mean_young
## est_mean                 0.883      -0.960       0.856          -0.967
## est_median               0.818      -0.930       0.830          -0.934
## est_sd                  -0.634       0.189      -0.610           0.354
## est_mad                   -Inf         Inf         Inf             NaN
## est_skew                -0.812       0.942      -0.804           0.936
## est_kurtosis             0.827      -0.460       0.880          -0.618
## est_prop_1              -0.575       0.943      -0.521           0.839
## est_prop_7               0.872      -0.723       0.946          -0.807
## est_mean_young           0.878      -0.952       0.855          -0.969
## est_mean_middle          0.859      -0.970       0.829          -0.965
## est_mean_old             0.902      -0.947       0.875          -0.957
## pref_mean                0.927      -0.946       0.896          -0.979
## pref_median              0.740      -0.790       0.656          -0.862
## pref_sd                 -0.913       0.929      -0.827           0.952
## pref_mad                -0.835       0.794      -0.740           0.875
## pref_skew               -0.813       0.978      -0.739           0.971
## pref_kurtosis            1.000      -0.777       0.947          -0.889
## pref_prop_1             -0.777       1.000      -0.717           0.946
## pref_prop_4              0.947      -0.717       1.000          -0.834
## pref_mean_young         -0.889       0.946      -0.834           1.000
## pref_mean_middle        -0.923       0.945      -0.876           0.977
## pref_mean_old           -0.948       0.909      -0.906           0.958
## muslim                  -0.522       0.835      -0.445           0.731
## net_fiscal               0.612      -0.830       0.547          -0.795
##                  pref_mean_middle pref_mean_old    muslim net_fiscal
## est_mean                   -0.980        -0.970 -0.722599     0.8064
## est_median                 -0.948        -0.931 -0.709851     0.7753
## est_sd                      0.409         0.469  0.000769    -0.0409
## est_mad                      -Inf           Inf      -Inf       -Inf
## est_skew                    0.931         0.915  0.703297    -0.8038
## est_kurtosis               -0.673        -0.731 -0.259524     0.3096
## est_prop_1                  0.815         0.759  0.769041    -0.8342
## est_prop_7                 -0.843        -0.870 -0.478799     0.5717
## est_mean_young             -0.974        -0.961 -0.717043     0.8147
## est_mean_middle            -0.974        -0.958 -0.747877     0.8176
## est_mean_old               -0.982        -0.979 -0.695430     0.7795
## pref_mean                  -0.996        -0.989 -0.714579     0.7601
## pref_median                -0.840        -0.840 -0.588171     0.5990
## pref_sd                     0.953         0.951  0.724072    -0.8148
## pref_mad                    0.874         0.897  0.574825    -0.6275
## pref_skew                   0.962         0.935  0.790525    -0.7962
## pref_kurtosis              -0.923        -0.948 -0.522021     0.6116
## pref_prop_1                 0.945         0.909  0.834709    -0.8300
## pref_prop_4                -0.876        -0.906 -0.444788     0.5475
## pref_mean_young             0.977         0.958  0.731489    -0.7954
## pref_mean_middle            1.000         0.989  0.719394    -0.7459
## pref_mean_old               0.989         1.000  0.657093    -0.7103
## muslim                      0.719         0.657  1.000000    -0.7295
## net_fiscal                 -0.746        -0.710 -0.729479     1.0000
#relation of SD to Muslim?
GG_scatter(d_reviewer, "muslim", "est_sd", text_pos = "tr")
## `geom_smooth()` using formula 'y ~ x'

#fiscal?
GG_scatter(d_reviewer, "net_fiscal", "est_sd", text_pos = "tr")
## `geom_smooth()` using formula 'y ~ x'

#regressions
#predict fiscal from estimates
list(
  ols(net_fiscal ~ est_mean, data = d_reviewer %>% df_standardize(messages = F)),
  ols(net_fiscal ~ est_mean + est_sd, data = d_reviewer %>% df_standardize(messages = F)),
  ols(net_fiscal ~ est_prop_1 + est_prop_7, data = d_reviewer %>% df_standardize(messages = F)),
  ols(net_fiscal ~ est_mean + est_sd + est_prop_1 + est_prop_7, data = d_reviewer %>% df_standardize(messages = F))
) %>% summarize_models()
#predict fiscal from preferences
list(
  ols(net_fiscal ~ pref_mean, data = d_reviewer %>% df_standardize(messages = F)),
  ols(net_fiscal ~ pref_mean + pref_sd, data = d_reviewer %>% df_standardize(messages = F)),
  ols(net_fiscal ~ pref_prop_1 + pref_prop_4, data = d_reviewer %>% df_standardize(messages = F)),
  ols(net_fiscal ~ pref_mean + pref_sd + pref_prop_1 + pref_prop_4, data = d_reviewer %>% df_standardize(messages = F))
) %>% summarize_models()
#age to predict stereotype accuracy
ols(stereotype_accuracy ~ age + gender + education_num + party_vote, data = d) %>% 
  ggpredict("age") %>% 
  plot()

GG_save("figures/age_prediction_model.png")

ols(stereotype_accuracy ~ rcs(age) + gender + education_num + party_vote, data = d) %>% 
  ggpredict("age") %>% 
  plot()

GG_save("figures/age_prediction_model_spline.png")

ols(stereotype_accuracy ~ rcs(age), data = d) %>% 
  ggpredict("age") %>% 
  plot()

GG_save("figures/age_prediction_model_spline_alone.png")

Meta

write_sessioninfo()
## R version 3.6.2 (2019-12-12)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Linux Mint 19.3
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] ggeffects_0.14.1    dkstat_0.08         readxl_1.3.1       
##  [4] rms_5.1-4           SparseM_1.78        polycor_0.7-10     
##  [7] lavaan_0.6-5        googlesheets4_0.1.0 kirkegaard_2018.05 
## [10] metafor_2.1-0       psych_1.9.12.31     magrittr_1.5       
## [13] assertthat_0.2.1    weights_1.0.1       mice_3.8.0         
## [16] gdata_2.18.0        Hmisc_4.3-1         Formula_1.2-3      
## [19] survival_3.1-11     lattice_0.20-40     forcats_0.5.0      
## [22] stringr_1.4.0       dplyr_0.8.5         purrr_0.3.3        
## [25] readr_1.3.1         tidyr_1.0.2         tibble_2.1.3       
## [28] tidyverse_1.3.0     mediation_4.5.0     sandwich_2.5-1     
## [31] mvtnorm_1.1-0       Matrix_1.2-18       MASS_7.3-51.5      
## [34] lavaanPlot_0.5.1    ggrepel_0.8.2       ggplot2_3.3.0      
## [37] pacman_0.5.1       
## 
## loaded via a namespace (and not attached):
##   [1] backports_1.1.5     plyr_1.8.6          splines_3.6.2      
##   [4] crosstalk_1.0.0     TH.data_1.0-10      digest_0.6.25      
##   [7] htmltools_0.4.0     fansi_0.4.1         checkmate_2.0.0    
##  [10] cluster_2.1.0       modelr_0.1.6        lpSolve_5.6.15     
##  [13] jpeg_0.1-8.1        colorspace_1.4-1    rvest_0.3.5        
##  [16] haven_2.2.0         xfun_0.12           crayon_1.3.4       
##  [19] jsonlite_1.6.1      lme4_1.1-21         zoo_1.8-7          
##  [22] glue_1.3.2          gtable_0.3.0        MatrixModels_0.4-1 
##  [25] scales_1.1.0        DBI_1.1.0           Rcpp_1.0.3         
##  [28] xtable_1.8-4        htmlTable_1.13.3    foreign_0.8-76     
##  [31] stats4_3.6.2        DT_0.12             htmlwidgets_1.5.1  
##  [34] httr_1.4.1          DiagrammeR_1.0.5    RColorBrewer_1.1-2 
##  [37] acepack_1.4.1       ellipsis_0.3.0      pkgconfig_2.0.3    
##  [40] farver_2.0.3        nnet_7.3-13         dbplyr_1.4.2       
##  [43] utf8_1.1.4          tidyselect_1.0.0    labeling_0.3       
##  [46] rlang_0.4.5         later_1.0.0         munsell_0.5.0      
##  [49] multilevel_2.6      cellranger_1.1.0    tools_3.6.2        
##  [52] visNetwork_2.0.9    cli_2.0.2           generics_0.0.2     
##  [55] sjlabelled_1.1.3    broom_0.5.5         evaluate_0.14      
##  [58] fastmap_1.0.1       yaml_2.2.1          knitr_1.28         
##  [61] fs_1.3.2            nlme_3.1-145        mime_0.9           
##  [64] quantreg_5.54       xml2_1.2.5          psychometric_2.2   
##  [67] compiler_3.6.2      rstudioapi_0.11     png_0.1-7          
##  [70] reprex_0.3.0        pbivnorm_0.6.0      stringi_1.4.6      
##  [73] nloptr_1.2.2        vctrs_0.2.4         stringdist_0.9.5.5 
##  [76] pillar_1.4.3        lifecycle_0.2.0     data.table_1.12.8  
##  [79] insight_0.8.2       httpuv_1.5.2        R6_2.4.1           
##  [82] latticeExtra_0.6-29 promises_1.1.0      gridExtra_2.3      
##  [85] codetools_0.2-16    polspline_1.1.17    boot_1.3-24        
##  [88] gtools_3.8.1        withr_2.1.2         mnormt_1.5-6       
##  [91] multcomp_1.4-12     mgcv_1.8-31         parallel_3.6.2     
##  [94] hms_0.5.3           grid_3.6.2          rpart_4.1-15       
##  [97] minqa_1.2.4         rmarkdown_2.1       lsr_0.5            
## [100] shiny_1.4.0         lubridate_1.7.4     base64enc_0.1-3    
## [103] rematch_1.0.1
#save the global environment
write_rds(as.list(globalenv()), "data/global_envir.rds")