#manual predictions at user-specified means or representative values
# function to generate var mean
# meanvar <- function(x){
# mean(x, na.rm=TRUE)
# }
#define predictor grid for conditional avg at covariate means or rep values
# ndmo_gen <- tibble(
# gender= c("1","2"),
# nlpf = "2", #set at 2 lifetime partners (mean=1.9)
# SRintelz = 0,
# SRpopularz = 0,
# agez = 0,
# worked = "1") %>%
# expand_grid(SRPAf = c("2","14"))
# generate and save predictions at user-specified covariate values
# mod6mopred <- predictions(mod6mo, newdata = ndmo_gen) %>%
# data.frame() %>%
# mutate(
# colgrp = if_else(gender == "1" & SRPAf == "2", "M: E[italic(j)](hat(italic(p))[italic(ij)]^min)", "M: E[italic(j)](hat(italic(p))[italic(ij)]^max)"),
# colgrp = if_else(gender == "2" & SRPAf == "2", "F: E[italic(j)](hat(italic(p))[italic(ij)]^min)", colgrp),
# colgrp = if_else(gender == "2" & SRPAf == "14", "F: E[italic(j)](hat(italic(p))[italic(ij)]^max)", colgrp),
# SRPAf = ifelse(SRPAf == "2", "E[italic(j)](hat(italic(p))[italic(ij)]^min)", "E[italic(j)](hat(italic(p))[italic(ij)]^max)"))
#Let's go with automated predictions by response category at means
mod6mopred <- predictions(mod6mo, variables = list(SRPAf = c("2","14")),
newdata = "mean", by = c("SRPAf", "group","gender")) %>%
data.frame() %>%
mutate(
colgrp = if_else(gender == "1" & SRPAf == "2", "M: E[italic(j)](hat(italic(p))[italic(ij)]^min)", "M: E[italic(j)](hat(italic(p))[italic(ij)]^max)"),
colgrp = if_else(gender == "2" & SRPAf == "2", "F: E[italic(j)](hat(italic(p))[italic(ij)]^min)", colgrp),
colgrp = if_else(gender == "2" & SRPAf == "14", "F: E[italic(j)](hat(italic(p))[italic(ij)]^max)", colgrp),
SRPAf = ifelse(SRPAf == "2", "E[italic(j)](hat(italic(p))[italic(ij)]^min)", "E[italic(j)](hat(italic(p))[italic(ij)]^max)"))
#function to find & drop leading zeroes (used for x-axis label)
dropLeadingZero <- function(l){
str_replace(l, '0(?=.)', '')
}
mod6mopred_m <- mod6mopred %>% dplyr::filter(gender=="1")
mod6mopred_f <- mod6mopred %>% dplyr::filter(gender=="2")
#full range of y responses (0:4)
mod6mop1 <- ggplot(mod6mopred, aes(x = group, y = estimate, ymin = conf.low, ymax = conf.high,
color = colgrp)) +
geom_hline(yintercept = 0:10 / 10, color = "lightgrey") +
geom_linerange(data=mod6mopred_m, aes(color = colgrp), linewidth = 3/4, position = position_dodge(width = 1/3)) +
geom_point(data=mod6mopred_m, aes(shape = colgrp),
position = position_dodge(width = 1/3),
size = 2) +
geom_linerange(data=mod6mopred_f, aes(color = colgrp), linewidth = 3/4, position = position_dodge(width = 1/6)) +
geom_point(data=mod6mopred_f, aes(shape = colgrp),
position = position_dodge(width = 1/6),
size = 2) +
scale_color_viridis_d(NULL, option = "magma", begin = .2, end = .8,
labels = scales::parse_format()) +
scale_shape_manual(NULL, values = c(19, 19, 17, 17),
labels = scales::parse_format()) +
scale_y_continuous(limits = c(0, 1), breaks=seq(0, 1, by = .2),
labels = dropLeadingZero) +
labs(subtitle = "Pred. response probabilities\n(all)") +
theme(legend.background = element_blank(),
legend.position = c(0.9, 0.85),
legend.key.height= unit(.5, 'cm'),
legend.key.width= unit(.5, 'cm'),
legend.title = element_text(size=10),
legend.text=element_text(size=10),
axis.title.y = element_blank(),
axis.title.x = element_blank())
#nonzero victimization values only (1:4)
mod6mop2 <- ggplot(mod6mopred, aes(x = group, y = estimate, ymin = conf.low, ymax = conf.high,
color = colgrp)) +
geom_hline(yintercept = 0:7 / 50, color = "lightgrey") +
geom_linerange(data=mod6mopred_m, aes(color = colgrp), linewidth = 3/4, position = position_dodge(width = 1/3)) +
geom_point(data=mod6mopred_m, aes(shape = colgrp),
position = position_dodge(width = 1/3),
size = 2) +
geom_linerange(data=mod6mopred_f, aes(color = colgrp), linewidth = 3/4, position = position_dodge(width = 1/6)) +
geom_point(data=mod6mopred_f, aes(shape = colgrp),
position = position_dodge(width = 1/6),
size = 2) +
scale_color_viridis_d(NULL, option = "magma", begin = .2, end = .8,
labels = scales::parse_format()) +
scale_shape_manual(NULL, values = c(19, 19, 17, 17),
labels = scales::parse_format()) +
scale_y_continuous(limits = c(0, .14), breaks=seq(0, .14, by = .02),
labels = dropLeadingZero) +
labs(subtitle = "(y > 0)",
x = expression(Victimiz.~response~option~(italic(j)))) +
theme(legend.background = element_blank(),
legend.position = c(0.9, 0.85),
legend.key.height= unit(.5, 'cm'),
legend.key.width= unit(.5, 'cm'),
legend.title = element_text(size=10),
legend.text=element_text(size=10),
axis.title.y = element_blank()
)
# estimate contrasts at means or rep values
mod6mocon <- comparisons(mod6mo, variables = list(SRPAf = c("2","14")),
newdata = "mean", by = c("group","gender")) %>%
data.frame() %>%
mutate(
gender = if_else(gender == "1", "Males", "Females"))
mod6mop3 <- mod6mocon %>%
ggplot(aes(x = group, y = estimate, ymin = conf.low, ymax = conf.high, color = gender)) +
geom_hline(yintercept = -4:4 / 20, color = "lightgrey") +
geom_hline(yintercept = 0, color = "black") +
geom_pointrange(aes(color = gender), linewidth = 3/4, size = 1/3, position = position_dodge(width = 1/3)) +
scale_color_viridis_d(NULL, option = "magma", begin = .3, end = .7,
labels = scales::parse_format()) +
scale_y_continuous(expression(italic(p[j])^max-italic(p[j])^min),
limits = c(-0.2, 0.2), breaks=seq(-.2, .2, by = .05),
labels = dropLeadingZero) +
labs(subtitle = expression(Delta[italic(j)]^{MEM}~(max~contrasts)),
x = expression(Victimiz.~response~option~(italic(j)))) +
theme(legend.background = element_blank(),
legend.position = c(0.9, 0.85),
legend.key.height= unit(.5, 'cm'),
legend.key.width= unit(.5, 'cm'),
legend.title = element_text(size=10),
legend.text=element_text(size=10)
)
# combine and entitle
((mod6mop3 | mod6mop1 / mod6mop2)) +
plot_annotation(title = "Cumulative probit y ~ mo(SRPAf):Gender + Controls",
subtitle = "Maximum monotonic MEM of SRPA on Victimization by Gender")