Chapter 19 nps_contrast ~ cue * stim
19.1 Overview
The purpose of this markdown is to plot the NPS extracted values for each contrast of interest
Steps 1. load and stack the files 2. plot per condition 3. x axis (pain vs. vicarious vs. cognitive) 4. y axis (NPS extracted values)
# contrast_name = {'P_VC_cue_high_gt_low', 'V_PC_cue_high_gt_low', 'C_PV_cue_high_gt_low', ...
# 'P_VC_stimlin_high_gt_low', 'V_PC_stimlin_high_gt_low', 'C_PV_stimlin_high_gt_low',...
# 'P_VC_stimquad_med_gt_other', 'V_PC_stimquad_med_gt_other', 'C_PV_stimquad_med_gt_other',...
# 'P_VC_cue_int_stimlin','V_PC_cue_int_stimlin', 'C_PV_cue_int_stimlin',...
# 'P_VC_cue_int_stimquad','V_PC_cue_int_stimquad','C_PV_cue_int_stimquad',...
# 'motor',...
# 'P_simple_cue_high_gt_low', 'V_simple_cue_high_gt_low', 'C_simple_cue_high_gt_low', ...
# 'P_simple_stimlin_high_gt_low', 'V_simple_stimlin_high_gt_low', 'C_simple_stimlin_high_gt_low',...
# 'P_simple_stimquad_med_gt_other', 'V_simple_stimquad_med_gt_other', 'C_simple_stimquad_med_gt_other',...
# 'P_simple_cue_int_stimlin', 'V_simple_cue_int_stimlin', 'C_simple_cue_int_stimlin',...
# 'P_simple_cue_int_stimquad','V_simple_cue_int_stimquad','C_simple_cue_int_stimquad'
19.2 For loop for all the pvc dummy codes
= dirname(dirname(getwd()))
main_dir <- c("Pain > rest" = "#941100",
color "Vicarious > rest" = "#008F51",
"Cog > rest" = "#011891")
for (contrast_name in c( "stimlin_high_gt_low", "stimquad_med_gt_other", "cue_int_stimlin", "cue_int_stimquad")) {
= data.frame()
merge_df = data.frame()
df = data.frame()
groupwise = data.frame()
subjectwise
for (conname in c('P_simple', 'V_simple', 'C_simple')) {
= data.frame()
df print(paste(contrast_name, conname))
= file.path(
npsdir
main_dir,'analysis',
'fmri',
'spm',
'univariate',
'model01_6cond',
'extract_nps'
)= read.csv(Sys.glob(file.path(
df
npsdir,paste0(
'extract-nps_model01-6cond_con_*',
conname,'_',
contrast_name,'.csv'
)
)))$conname = char(conname)
df
= rbind(merge_df, df)
merge_df
}
= as.data.frame(merge_df)
merge_df $contrast <-
merge_dfrecode_factor(
$conname,
merge_dfP_simple = "Pain > rest",
V_simple = "Vicarious > rest",
C_simple = "Cog > rest"
)$contrast <- as.factor(merge_df$contrast)
merge_df# print(summary(model.nps))
# parameters __________________________________________________________________
= 'nps'
model = "nps"
model_keyword = "mean_per_sub"
subjectwise_mean = "mean_per_sub_norm_mean"
group_mean = "contrast"
iv = c(-800, 800)
ylim = "se"
se = "subject"
subject = paste0(model_keyword,
ggtitle " :: extracted NPS value for taskwise contrast")
= "Contrasts"
legend_title = "contrasts "
xlab = "NPS dotproduct \nmetric and functionality"
ylab = "nps"
dv = "nps_dot_product"
dv_keyword
<- meanSummary(merge_df,
classwise c(subject, iv), dv)
<- summarySEwithin(data = classwise,
groupwise measurevar = "mean_per_sub",
withinvars = c(iv))
= subset(classwise, select = -c(sd))
subjectwise
<- ggplot(data = subjectwise,
p1 aes(
y = .data[[subjectwise_mean]],
x = factor(.data[[iv]]),
fill = factor(.data[[iv]])
+
))
geom_half_violin(
aes(fill = factor(.data[[iv]])),
side = 'r',
position = 'dodge',
adjust = 0.5,
trim = FALSE,
alpha = .3,
colour = NA
+
)
geom_line(
data = subjectwise,
aes(
group = .data[[subject]],
x = as.numeric(factor(.data[[iv]])) - .15 ,
y = .data[[subjectwise_mean]],
fill = factor(.data[[iv]])
),linetype = "solid",
color = "grey",
alpha = .3
+
)
geom_point(
aes(
# group = .data[[subject]],
x = as.numeric(factor(.data[[iv]])) - .15 ,
y = .data[[subjectwise_mean]],
color = factor(.data[[iv]])
),position = position_jitter(width = .05),
size = 2,
alpha = 0.7,
+
)
geom_half_boxplot(
data = subjectwise,
aes(x = .data[[iv]],
y = .data[[subjectwise_mean]],
fill = .data[[iv]]),
side = "r",
outlier.shape = NA,
alpha = 0.8,
width = .1,
notch = FALSE,
notchwidth = 0,
varwidth = FALSE,
colour = "black",
errorbar.draw = FALSE
+
)
geom_errorbar(
data = groupwise,
aes(
x = as.numeric(.data[[iv]]) + .1,
y = as.numeric(.data[[group_mean]]),
fill = factor(.data[[iv]]),
ymin = .data[[group_mean]] - .data[[se]],
ymax = .data[[group_mean]] + .data[[se]]
),width = .05
+
)
# legend stuff ________________________________________________________
guides(color = "none") +
guides(fill = guide_legend(title = legend_title)) +
scale_fill_manual(values = color) +
scale_colour_manual(values = color) +
ggtitle(ggtitle) +
# scale_x_discrete() +
xlab(xlab) +
ylab(ylab) +
ylim(ylim) +
theme_classic2() +
theme(aspect.ratio = 6 / 10) +
theme(
legend.position = "none",
text = element_text(family = "DejaVu Sans"),
plot.title = element_text(size = 12)
)
# plot description ________________________________________________________
<- p1 +
p1 labs(
title = paste("contrast: ", contrast_name),
subtitle = "Do NPS dot products differ as a function of task for the contrast?",
caption = "Blue = fitted linear slope per participant; Green: 1:1 slope"
)print(p1)
= file.path(
savedir
main_dir,'analysis',
'mixedeffect',
'model10_iv-cue-stim_dv-nps',
as.character(Sys.Date())
)dir.create(savedir, showWarnings = FALSE, recursive = TRUE)
<-
save_fname file.path(
savedir,paste0('extract-nps_iv-',iv,'_dv-',dv_keyword,'_con-',contrast_name,'.png')
)ggsave(
save_fname,plot = p1,
# width = w,
unit = "in",
dpi = 600,
scale = 2.5
) }
## [1] "stimlin_high_gt_low P_simple"
## [1] "stimlin_high_gt_low V_simple"
## [1] "stimlin_high_gt_low C_simple"
## Warning in geom_line(data = subjectwise, aes(group = .data[[subject]], x =
## as.numeric(factor(.data[[iv]])) - : Ignoring unknown aesthetics: fill
## Warning in geom_errorbar(data = groupwise, aes(x = as.numeric(.data[[iv]]) + :
## Ignoring unknown aesthetics: fill
## Saving 17.5 x 12.5 in image
## [1] "stimquad_med_gt_other P_simple"
## [1] "stimquad_med_gt_other V_simple"
## [1] "stimquad_med_gt_other C_simple"
## Warning in geom_line(data = subjectwise, aes(group = .data[[subject]], x = as.numeric(factor(.data[[iv]])) - : Ignoring unknown aesthetics: fill
## Ignoring unknown aesthetics: fill
## Saving 17.5 x 12.5 in image
## [1] "cue_int_stimlin P_simple"
## [1] "cue_int_stimlin V_simple"
## [1] "cue_int_stimlin C_simple"
## Warning in geom_line(data = subjectwise, aes(group = .data[[subject]], x = as.numeric(factor(.data[[iv]])) - : Ignoring unknown aesthetics: fill
## Ignoring unknown aesthetics: fill
## Saving 17.5 x 12.5 in image
## [1] "cue_int_stimquad P_simple"
## [1] "cue_int_stimquad V_simple"
## [1] "cue_int_stimquad C_simple"
## Warning in geom_line(data = subjectwise, aes(group = .data[[subject]], x = as.numeric(factor(.data[[iv]])) - : Ignoring unknown aesthetics: fill
## Ignoring unknown aesthetics: fill
## Saving 17.5 x 12.5 in image