@@ -126,6 +126,8 @@ library(effects)
126126library(xml2 ) # for saving tables to disk
127127library(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,
575603plot_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(" \u 03b2 = " , digit_display(b1 ),
659+ " \n SE = " , digit_display(se ),
660+ " \n CI = [" , digit_display(cis [5 ,1 ]), " - " , digit_display(cis [5 ,2 ]), " ]" ,
661+ " \n p " , 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(" \u 03b2 = " , digit_display(b1 ),
1007+ " \n SE = " , digit_display(se ),
1008+ " \n CI = [" , digit_display(cis [5 ,1 ]), " - " , digit_display(cis [5 ,2 ]), " ]" ,
1009+ " \n p " , 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:
0 commit comments