diff --git a/ccn2019.rev2.Rmd b/ccn2019.rev2.Rmd index 16bd274..119fd43 100644 --- a/ccn2019.rev2.Rmd +++ b/ccn2019.rev2.Rmd @@ -1,6 +1,8 @@ --- title: "Statistical Properties of the N-Back Sequences" -output: html_notebook +output: + pdf_document: default + html_notebook: default --- # Problems @@ -19,10 +21,96 @@ ```{r libraries, message=FALSE, include=FALSE, paged.print=FALSE} library(ggplot2) library(tidyverse) - +library(stringi) +library(plsRglm) ``` -```{r datasets} +```{r params} load('./data/CL2015.RData') + +window_size <- 8 ``` + +$P=\{V,D,C,W\}$ + +$V=\{x_N,x_{T},x_{T,local},x_L,x_{L,local},x_V,x_U,x_S,x_{S,local},x_G\}$ + +$D=\{\}$ + + + +```{r history} + +with_lures <- function(stimulus, stimulus_type, n) { + sapply(1:length(stimulus), function(i) { + lures <- c(as.character(stimulus[i-n-1]), as.character(stimulus[i-n+1])) + are_valid_trials <- i>n && all(!is.na(c(lures,stimulus[i]))) + ifelse(are_valid_trials && stimulus[i] %in% lures, + "lure", + as.character(stimulus_type[i])) + }) +} + +with_history <- function(stimuli, length=16, fixed=F) { + seq <- paste(stimuli, collapse = '') + + sapply(1:length(stimuli), function(i) { + stri_reverse(str_sub(seq, max(1,i-length+1), i)) + }) + #ifelse(fixed, h[str_length(h)==size], h) +} + +# $x_{s,local}$ +with_skewness <- function(history) { + sapply(history, function(h) { + freqs <- table(unlist(str_split(h,""))) + sum(sort(freqs, decreasing = T)[1:2]) - 1 + }) +} + +# $x_{u,local}$ +with_lumpiness <- function(history) { + sapply(history, function(h) { + freqs <- table(unlist(str_split(h,""))) + max(freqs) - 1 + }) +} + + +with_targets_ratio <- function(stimulus_type, length=16) { + sapply(1:length(stimulus_type), function(i) { + trials <- stimulus_type[max(1,i-length):i] + length(trials[trials=="target"]) / length(trials) + }) +} + +with_lures_ratio <- function(stimulus_type, length=16) { + sapply(1:length(stimulus_type), function(i) { + trials <- stimulus_type[max(1,i-length):i] + length(trials[trials=="lure"]) / length(trials) + }) +} + +NB2 <- NB %>% + group_by(participant, condition, block) %>% + mutate(n = ifelse(condition=='2-back',2,3)) %>% + mutate(stimulus_type = with_lures(stimulus, stimulus_type, n)) %>% + mutate(history = with_history(stimulus, window_size)) %>% + mutate(x_sl = with_skewness(history)) %>% + mutate(x_ul = with_lumpiness(history)) %>% + mutate(x_t = with_targets_ratio(stimulus_type, window_size)) %>% + mutate(x_l = with_lures_ratio(stimulus_type, window_size)) %>% + ungroup() + +pca <- prcomp(~x_sl+x_ul+x_t+x_l, NB2, center = TRUE,scale. = TRUE, na.action=na.exclude) +NB2 <- NB2 %>% mutate(pc1=pca$x[,'PC1'], pc2=pca$x[,'PC2']) + +rt <- unlist(NB2[,"rt"]) +dm <- NB2[,c("x_sl","x_ul","x_t","x_l")] +set.seed(250) +plsR(rt,dm,3) + + + +``` \ No newline at end of file