Skip to content

Commit d42b91b

Browse files
committed
WIP: beautify plots for publication
1 parent 4ee91cc commit d42b91b

File tree

3 files changed

+199
-3
lines changed

3 files changed

+199
-3
lines changed

code/analysisReadAloudBeta.R

Lines changed: 165 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ library(effects)
126126
library(xml2) # for saving tables to disk
127127
library(htmlTable) # for descriptive table
128128
# library(colorblindr)
129+
library(MetBrewer)
130+
library(RColorBrewer)
129131

130132
# ```
131133
# Warning in install.packages :
@@ -490,6 +492,32 @@ plot_lmer <- function(model, predictor, outcome, xlab = predictor, ...) {
490492
...)
491493
}
492494

495+
# visual details
496+
rwe_palette <- brewer.pal(4, "Purples")
497+
rwe_palette <- colorRampPalette(rwe_palette)(17)
498+
rwe_palette <- rwe_palette[4:17]
499+
500+
# helpers
501+
502+
#helper functions
503+
digit_display <- function(number){
504+
if(abs(number)<0.001){
505+
x <- sprintf("%.4f", number)
506+
}else{
507+
x <- sprintf("%.3f", number)
508+
}
509+
return (x) #this is a string
510+
}
511+
512+
tinyps <- function(pval){
513+
if(pval < 0.001){
514+
x = "< 0.001"
515+
}else {
516+
tmp <- round(pval,3)
517+
x = paste("= ", as.character(tmp), sep="")
518+
}
519+
return (x) #this is a string
520+
}
493521

494522

495523
#misprod_rate x bfne
@@ -575,13 +603,15 @@ plot_lmer(model11_z_scored,
575603
plot_model(model11_z_scored,
576604
type = "pred",
577605
terms = "scaaredSoc_z",
578-
colors = "#4b9bc7"
606+
colors = "mediumpurple2"
607+
# colors = rwe_palette
579608
) + theme(
580609
plot.title = element_text(size = 18),
581610
text = element_text(size = 16),
582611
) + # geom_line(size = 2) +
583-
theme_bw() +
584-
scale_color_manual(values = c("blue")) +
612+
theme_bw() +
613+
# scale_color_manual(values = c("plum3")) +
614+
# scale_color_manual(values = rwe_palette)
585615
theme(plot.title = element_text(size = 18),
586616
text = element_text(size = 16),
587617
panel.border = element_blank(),
@@ -601,6 +631,69 @@ plot_model(model11_z_scored,
601631
theme(plot.title = element_text(hjust = 0.05))
602632

603633

634+
# Jess' version
635+
plot_fig_2 <- function() {
636+
coefsmodel11z <- summary(model11_z_scored)$coef
637+
cis <- confint(model11_z_scored)
638+
b0 <- coefsmodel11z[1]
639+
b1 <- coefsmodel11z[2]
640+
se <- coefsmodel11z[4]
641+
642+
#bootstrap ci ribbon
643+
iterations = 1000
644+
a <- tibble(i=rep(1:iterations,))
645+
a <- mutate(a, intercept=NA, beta=NA)
646+
for(i in 1:nrow(a)){
647+
rows <- sample(1:nrow(errorDat), nrow(errorDat), replace=TRUE)
648+
df <- errorDat[rows, c('id', 'passage', 'scaaredSoc_z', 'words_with_hes_rate_z')]
649+
mdl <- lme4::lmer(words_with_hes_rate_z ~ scaaredSoc_z + (1|id) + (1|passage),
650+
data=df, REML=TRUE, control=lmerControl(optimizer="bobyqa", optCtrl=list(maxfun=2e5)))
651+
a[i,2] <- lme4::fixef(mdl)[1]
652+
a[i,3] <- lme4::fixef(mdl)[2]
653+
}
654+
655+
656+
#create df for annotation
657+
label_text <- data.frame(
658+
label = c(paste("\u03b2 = ", digit_display(b1),
659+
"\nSE = ", digit_display(se),
660+
"\nCI = [", digit_display(cis[5,1]), " - ", digit_display(cis[5,2]), "]",
661+
"\np ", tinyps(coefsmodel11z[10]), sep="")),
662+
scaaredSoc_z = c(-1.1),
663+
#words_with_hes_rate_z = c(4.5)) #location for plot with all datapoints
664+
words_with_hes_rate_z = c(0.75)) #location for plot with limited y-axis
665+
666+
#plot
667+
p <- ggplot(errorDat, aes(x=scaaredSoc_z, y=words_with_hes_rate_z)) +
668+
geom_jitter(aes(color=factor(scaaredSoc)), alpha=0.5, width=0.05, show.legend=FALSE) +
669+
scale_color_manual(values=rwe_palette)
670+
671+
for(i in 1:nrow(a)){ #add bootstrapped lines to show confidence interval
672+
p <- p + geom_abline(intercept=as.numeric(a[i,2]), slope=as.numeric(a[i,3]), color=rwe_palette[3], alpha=0.1)
673+
}
674+
675+
p <- p + geom_abline(intercept=b0, slope=b1, color=rwe_palette[14], linewidth=1) +
676+
guides(color=FALSE, shape=FALSE) +
677+
geom_label(data=label_text, aes(x=scaaredSoc_z, y=words_with_hes_rate_z, label=label), size=3) +
678+
ylim(-0.9, 0.9) + #remove this line for plot with all datapoints
679+
theme_bw() +
680+
theme(plot.title = element_text(size=18, hjust=0.05, face='bold'),
681+
text = element_text(size=16),
682+
panel.border = element_blank(),
683+
panel.grid = element_line(linewidth=0.6, linetype='dashed'),
684+
panel.grid.minor = element_blank(),
685+
axis.line.x = element_line(linewidth=0.6, linetype='dashed', color='#bbbbbb60'),
686+
axis.ticks.x = element_blank()) +
687+
labs(title="Social Anxiety Symptoms × Hesitation Rate",
688+
x="SCAARED-Social Score\n(z-scored)",
689+
y="Rate of Hesitations\n(per word, z-scored)")
690+
return(p)
691+
}
692+
693+
#save file (adjust width/height as needed)
694+
ggsave(file.path(outpath, "fig2.jpg"), plot=plot_fig_2(), width=8, height=5, units="in")
695+
696+
604697

605698

606699
# same, controlling for sex:
@@ -876,6 +969,75 @@ plot_model(f_model24_z_scored,
876969
scale_y_continuous(breaks = c(0, 0.5, 1, 1.5, 2)) +
877970
theme(plot.title = element_text(hjust = 0.05))
878971

972+
# Jess' version, wip
973+
plot_fig_3 <- function() {
974+
# determine degrees of purple needed for this variable
975+
rwe_palette_custom <- brewer.pal(4, "Purples")
976+
number_of_values <-
977+
pull(errorDat, words_with_hes_rate_z) %>%
978+
unique %>%
979+
length
980+
981+
rwe_palette_custom <- colorRampPalette(rwe_palette_custom)(number_of_values+3)
982+
rwe_palette_custom <- rwe_palette_custom[4:(number_of_values+3)]
983+
984+
coefsmodel11z <- summary(f_model24_z_scored)$coef
985+
cis <- confint(f_model24_z_scored)
986+
b0 <- coefsmodel11z[1]
987+
b1 <- coefsmodel11z[2]
988+
se <- coefsmodel11z[4]
989+
990+
#bootstrap ci ribbon
991+
iterations = 1000
992+
a <- tibble(i=rep(1:iterations,))
993+
a <- mutate(a, intercept=NA, beta=NA)
994+
for(i in 1:nrow(a)){
995+
rows <- sample(1:nrow(errorDat), nrow(errorDat), replace=TRUE)
996+
df <- errorDat[rows, c('id', 'passage', 'words_with_hes_rate_z', 'words_with_misprod_rate_z')]
997+
mdl <- lme4::lmer(words_with_misprod_rate_z ~ words_with_hes_rate_z + (1|id) + (1|passage),
998+
data=df, REML=TRUE, control=lmerControl(optimizer="bobyqa", optCtrl=list(maxfun=2e5)))
999+
a[i,2] <- lme4::fixef(mdl)[1]
1000+
a[i,3] <- lme4::fixef(mdl)[2]
1001+
}
1002+
1003+
1004+
#create df for annotation
1005+
label_text <- data.frame(
1006+
label = c(paste("\u03b2 = ", digit_display(b1),
1007+
"\nSE = ", digit_display(se),
1008+
"\nCI = [", digit_display(cis[5,1]), " - ", digit_display(cis[5,2]), "]",
1009+
"\np ", tinyps(coefsmodel11z[10]), sep="")),
1010+
words_with_hes_rate_z = c(-1.1),
1011+
words_with_misprod_rate_z = c(0.75)) #location for plot with limited y-axis
1012+
1013+
#plot
1014+
p <- ggplot(errorDat, aes(x=words_with_hes_rate_z, y=words_with_misprod_rate_z)) +
1015+
geom_jitter(aes(color=factor(words_with_hes_rate_z)), alpha=0.5, width=0.05, show.legend=FALSE) +
1016+
scale_color_manual(values=rwe_palette_custom)
1017+
1018+
for(i in 1:nrow(a)){ #add bootstrapped lines to show confidence interval
1019+
p <- p + geom_abline(intercept=as.numeric(a[i,2]), slope=as.numeric(a[i,3]), color=rwe_palette_custom[3], alpha=0.1)
1020+
}
1021+
1022+
p <- p + geom_abline(intercept=b0, slope=b1, color=rwe_palette_custom[number_of_values], linewidth=1) +
1023+
guides(color=FALSE, shape=FALSE) +
1024+
geom_label(data=label_text, aes(x=words_with_hes_rate_z, y=words_with_misprod_rate_z, label=label), size=3) +
1025+
ylim(-0.9, 0.9) + #remove this line for plot with all datapoints
1026+
theme_bw() +
1027+
theme(plot.title = element_text(size=18, hjust=0.05, face='bold'),
1028+
text = element_text(size=16),
1029+
panel.border = element_blank(),
1030+
panel.grid = element_line(linewidth=0.6, linetype='dashed'),
1031+
panel.grid.minor = element_blank(),
1032+
axis.line.x = element_line(linewidth=0.6, linetype='dashed', color='#bbbbbb60'),
1033+
axis.ticks.x = element_blank()) +
1034+
labs(title="Hesitation Rate × Misproduction Rate",
1035+
x="Rate of Hesitations\n(per word, z-scored)",
1036+
y="Rate of Misproductions\n(per word, z-scored)")
1037+
return(p)
1038+
}
1039+
ggsave(file.path(outpath, "fig3.jpg"), plot=plot_fig_3(), width=8, height=5, units="in")
1040+
8791041

8801042

8811043
# same, controlling for sex:

code/analysisWordLevelPrePostRevisedReadAloudBeta.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ interact_plot(model = hesitation_adjacent_misproduction_model_5_logistic_wordfre
303303
modx = "adjacent_hesitation_present_in_direction_looked",
304304
pred = "direction_searched_for_potential_hesitation_predictor",
305305
interval = TRUE,
306+
colors = "Qual2",
306307
#fixme x.label = "SCAARED-Social score\n(z-scored)",
307308
modx.labels = c('Hesitation present', 'Hesitation absent'),
308309
modx.values = factor(c(1, -1)),
@@ -423,6 +424,28 @@ write_table_html_to_disk(
423424
)
424425

425426

427+
# control analyses
428+
sex_hesitation_adjacent_misproduction_model_5_logistic_wordfreq_with_absents_as_median_no_psg <- # starting sans passage because prior model with fewer predictors still did not initially converge
429+
glmer(misprod_outcome ~ adjacent_hesitation_present_in_direction_looked * direction_searched_for_potential_hesitation_predictor * log10frequency_with_absents_as_median_z * scaaredSoc_z + sex + (1|id) + (1|word),
430+
data=revisedDatWithPositionAltTesting, family = "binomial") # does not converge
431+
432+
# raise # of iterations
433+
sex_hesitation_adjacent_misproduction_model_5_logistic_wordfreq_with_absents_as_median_no_psg_bobyqa <-
434+
update(sex_hesitation_adjacent_misproduction_model_5_logistic_wordfreq_with_absents_as_median_no_psg,
435+
control = glmerControl(optimizer="bobyqa", optCtrl = list(maxfun = 1e9)))
436+
437+
## to run
438+
age_hesitation_adjacent_misproduction_model_5_logistic_wordfreq_with_absents_as_median_no_psg <- # starting sans passage because prior model with fewer predictors still did not initially converge
439+
glmer(misprod_outcome ~ adjacent_hesitation_present_in_direction_looked * direction_searched_for_potential_hesitation_predictor * log10frequency_with_absents_as_median_z * scaaredSoc_z + age + (1|id) + (1|word),
440+
data=revisedDatWithPositionAltTesting, family = "binomial") # does not converge
441+
442+
443+
age_hesitation_adjacent_misproduction_model_5_logistic_wordfreq_with_absents_as_median_no_psg_bobyqa <-
444+
update(age_hesitation_adjacent_misproduction_model_5_logistic_wordfreq_with_absents_as_median_no_psg,
445+
control = glmerControl(optimizer="bobyqa", optCtrl = list(maxfun = 1e9)))
446+
447+
save.image("~/Documents/ndclab/rwe-analysis-sandbox/rwe-analysis/derivatives/RWE-item-level-with-zscoring-logistic-and-pre-post-and-control-analyses-may-29-25.RData")
448+
426449
# attempt to identify post-hoc contrasts for the interaction
427450
# credit to @jessb0t
428451
library(openxlsx)

code/analysisWordLevelReadAloudBeta.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ library(insight); library(sjPlot) # tables
126126
library(effects)
127127
library(xml2) # for saving tables to disk
128128
# library(colorblindr)
129+
library(MetBrewer)
130+
library(RColorBrewer)
131+
129132

130133
#set up date for output file naming
131134
today <- Sys.Date()
@@ -666,6 +669,14 @@ if(DEBUG) { # compare to how it was/would've been before splitting outcome and p
666669
summary(wrong_model2.5)
667670
}
668671

672+
#helper functions
673+
digit_display <- function(number) { # to 4 decimal places if < 0.001 else just 3
674+
ifelse(abs(number) < 0.001, "%.4f", "%.3f") %>% sprintf(number)
675+
}
676+
677+
tinyps <- function(pval) {
678+
ifelse(pval < 0.001, "< 0.001", paste0("= ", round(pval, 3)))
679+
}
669680

670681

671682
# n.s.

0 commit comments

Comments
 (0)