Init
library(pacman)
p_load(kirkegaard, haven, rms, ggeffects)
theme_set(theme_bw())
Data
d = read_sav("data/Political+diversity--philosophy--Philo-L_RAW.sav")
d_vars = df_var_table(d)
#fix encoding of areas
area = d %>% select(Q105_1:Q105_11) %>% names()
area_nice_names = d_vars %>% filter(var_name %in% area) %>% pull(label) %>% str_match("Selected Choice (.*)") %>% .[, 2]
area_nice_names_legal = area_nice_names %>% str_legalize()
for (v in area) {
#fill 0's
d[[v]][is.na(d[[v]])] = 0
#nice name
d[[area_nice_names_legal[which(area == v)]]] = d[[v]] %>% as.factor()
}
#others
d$Age = d$Age %>% as.numeric()
d$Sex = d$Sex %>% as_factor()
d$ethnicity = d$ethnicity %>% as_factor() %>% fct_relevel("White")
d$PolViews = d$PolViews %>% as_factor() %>% as.ordered()
d$PolViews_num = d$PolViews %>% as.numeric()
Analyze
#plot simple comparison
logic_compare = table(d$Logic, d$PolViews)
d %>%
filter(!is.na(Logic), !is.na(PolViews)) %>%
ggplot(aes(Logic, fill = PolViews)) +
geom_bar(position = "fill") +
scale_x_discrete("Specialiaty includes logic?") +
scale_y_continuous("Percentage", labels = scales::percent) +
scale_fill_ordinal("Self-rated political ideology") +
ggtitle("Political distribution of logic speciality philosophers vs. other specialities",
str_glue("Sample sizes: {logic_compare[1, ] %>% sum()} vs. {logic_compare[2, ] %>% sum()}"))

GG_save("figs/logic_comparison.png")
#formal comparisons
list(
ols(PolViews_num ~ Logic, data = d),
ols(PolViews_num ~ Sex + rcs(Age) + ethnicity + Logic, data = d),
ols(PolViews_num ~ Sex + rcs(Age) + ethnicity + Logic + Aesthetics + Epistemology + Ethics + History_of_philosophy + Metaphysics + Phenomenology + Philosophy_of_mind + Philosophy_of_religion + Philosophy_of_science + Political_philosophy, data = d)
) ->
model_fits
#summarize
model_fits %>%
summarize_models() %>%
DT::datatable()
#plot projections
ggpredict(model_fits[[3]], terms = c("Age", "Logic", "Sex")) %>% plot() +
scale_y_continuous("Political ideology (1 = far left, 7 = far right)", limits = c(0, 7), breaks = 1:7) +
ggtitle("Model predictions of political ideology",
"Marginal effects of age, sex and logic speciality shown")
## Scale for 'y' is already present. Adding another scale for 'y', which will
## replace the existing scale.

GG_save("figs/model_predict.png")