---
title: "Create Free Classification (Dis)similarity Matrices"
author: "Original by Aaron Albin, modified and expanded by Danielle Daidone"
date: "3/8/2022"
output: html_document
---

Note: The comments in this R script pertain to the Finnish length free classification data from the following open-access article:

Daidone, D., Lidster, R., & Kruger, F. (2023). Free classification as a method for investigating the perception of non-native sounds. Studies in Second Language Acquisition, 45(4), pp. 1104-1130. https://doi.org/10.1017/S0272263123000050

The files "FNL_Lookup_Matrix.txt" and "FNL_AE_FC.txt" are available at https://osf.io/xf5ju/ as well as within the explanatory blog post at https://www.ddaidone.com/blog/analyzing-free-classification-results-using-an-r-script-to-obtain-dissimilarity-matrices.  This blog post also explains how to format your data for use with this script.

***Make sure your stimuli across contexts are labeled such that alphabetical order will not differ across the contexts.  Otherwise, the combined contexts results will be inaccurate***

***Because of the length of the script, I recommend collapsing all the code chunks for easy of readability.  Go to Edit --> Folding --> Collapse All.  Alternately, the short cut is Alt + O (the letter).***

Download and load relevant packages
```{r}
if(!require(dplyr)){install.packages("dplyr")}
library(dplyr)
if(!require(tidyverse)){install.packages("tidyverse")}
library(tidyverse)
```



PART 1: OPEN AND CHECK DATASET

Open dataset - Change names to match your filepath and files
```{r}
# Set your working directory to where your look-up matrix and coded PPTs text file are saved
setwd("C:/Users/Danielle/OneDrive - UNC-Wilmington/CV/Website/FC analysis info")

# Note that the look-up matrix and coded PPTs text file are tab delimited files with headers
Lookup=read.table("FNL_Lookup_Matrix.txt",sep="\t",header=TRUE)
Dataset=read.table("FNL_AE_FC.txt",sep="\t",header=TRUE)
```
Look at the first few rows of the dataset
```{r}
head(Dataset)
#   Subject Version   Slide   Context     Group     Tokens  
#1	B08	    B	        1	       upu         1	      19,4,8,6,15
#2	B08	    B	        1	       upu         2	      1,3,22,7,18
#3	B08	    B	        1	       upu         3	      5,2,16
#4	B08	    B	        1	       upu         4	      20,11,17,10,21
#5	B08	    B	        1	       upu         5	      24,23,13,14,9,12
#6	B08	    B	        2	       iki         1	      3,11,5,10,15
```
Check number of participants
```{r}
length(unique(Dataset$Subject))
# 26 participants' data included in the dataset
```
Check total number of groups per participant across all slides
```{r}
sort(table(Dataset$Subject))
# Only 9 groups for B52, and as many as 23 for D207
```
Check on the data for an individual participant
```{r}
Dataset[Dataset$Subject=="B08",]
# Participant B08 made 5 groups for each of the 3 slides
```
Check number of slides
```{r}
# Because different participants made different total numbers of groups, count the number of "Group=1" cases as an indication of the structure of the data as a whole
Group1=Dataset[Dataset$Group==1,]
table(table(Group1$Subject))
# All 'number of slides' is 3 for the 26 participants
```
Check how many people did each version of the task (i.e. which slide order)
```{r}
x=unique(Dataset[,c("Subject","Version"),])
table(x$Version)
# How many people did each version of each experiment:
#   A   B
#   13  13
```
Check how many participants' worth of data we have for each slide
```{r}
xx=unique(Dataset[,c("Subject","Slide"),])
table(xx$Slide)
#     1  2  3
#    26 26 26
# Just as you would expect. No one is missing a slide.
```
Check number of groups each subject made for each slide
```{r}
xxx=unique(Dataset[,c("Subject","Slide","Group"),])
table(xxx$Subject, xxx$Slide)
#        1 2 3
#  B08   5 5 5
#  B13   8 7 5
#  B14   3 5 4
#  B16   8 7 4
#  B50   5 4 5
#  B51   7 7 6
```
Check how many tokens of each icon you have
```{r}
table(strsplit(paste(as.character(Dataset$Tokens),collapse=","),split=","))
# Everything is 78. This is 26*3=78.
```
Check how many icons there are for each participant
```{r}
xyz=aggregate(as.character(Dataset$Tokens),by=list(paste(as.character(Dataset$Subject))),FUN=function(x){ paste(x,collapse=",")})[,2]
unlist(lapply(strsplit(xyz,split=","),FUN=function(x){table(table(x))}))
# Every participant has 3 slides with 24 icons each
```


Create variables for your contexts (up to 4)
```{r}
Contexts = unique(Dataset$Context)
Contexts

Context1 = Contexts[1]
if (length(Contexts) > 1) {
  Context2 = Contexts[2]
}
if (length(Contexts) > 2) {
  Context3 = Contexts[3]
}
if (length(Contexts) > 3) {
  Context4 = Contexts[4]
}

# "upu" "iki" "ata"

```




PART 2: CREATE SIMILARITY MATRICES



*****Context 1*****


Subset data by first context
```{r}
Dataset1 <- select(filter(Dataset, Context == Context1), c("Subject","Version","Slide","Context","Group","Tokens"))

head(Dataset1)

# Subject Version Slide Context Group   Tokens
#1	B08	    B	      1	    upu	    1	    19,4,8,6,15
#2	B08	    B	      1	    upu	    2	    1,3,22,7,18
#3	B08	    B	      1	    upu	    3	    5,2,16
#4	B08	    B	      1	    upu	    4	    20,11,17,10,21
#5	B08	    B	      1	    upu	    5	    24,23,13,14,9,12
#6	B13	    B	      1	    upu	    1	    1,3,16,18

```

Subset look-up matrix by first context
```{r}
Lookup1 <- select(filter(Lookup, Context == Context1), c("ASlide","BSlide","IconNumber","Condition","Context","Speaker","Code"))

head(Lookup1)

# ASlide  BSlide IconNumber Condition Context Speaker Code
#1	3	      1	      1	        cVcVV	    upu	    M	    cVcVV_upu_M
#2	3	      1	      2	        cVccV	    upu	    N	    cVccV_upu_N
#3	3	      1	      3	        cVcVV	    upu	    L	    cVcVV_upu_L
#4	3	      1	      4	        cVVcVV	  upu	    N	    cVVcVV_upu_N
#5	3	      1	      5	        cVVccV	  upu	    L	    cVVccV_upu_L
#6	3	      1	      6	        cVVccV	  upu	    N	    cVVccV_upu_N

```


Context 1: Create similarity matrix by counts (symmetrical)

```{r}
nRow = nrow(Dataset1)

AllCodes = as.character(Lookup1$Code)
AllCodes = sort(AllCodes)

OutputMatrix1 = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

for(EachRow in 1:nRow){ # EachRow=1

CurrentRow=Dataset1[EachRow,]

Version=as.character(CurrentRow[,"Version"])
Slide=CurrentRow[,"Slide"]
Group=as.character(CurrentRow[,"Group"])
Tokens=as.character(CurrentRow[,"Tokens"])
SplitTokens = as.integer(strsplit(Tokens,split=",")[[1]])

nTokens=length(SplitTokens)
Recoded = rep(NA,times=nTokens)
for(EachToken in 1:nTokens){ # EachToken=1

CurrentToken=SplitTokens[EachToken]

if(Version=="A"){ColumnName="ASlide"}else{ColumnName="BSlide"}

Target= Lookup[,ColumnName]==Slide & 
        Lookup$IconNumber==CurrentToken

Recoded[EachToken] <- as.character(Lookup[Target,"Code"])

} # End 'EachToken' loop

Combinations = t(combn(Recoded,m=2))
Columns=Combinations[,1]
Rows=Combinations[,2]

nCombinations = nrow(Combinations)
for(EachCombination in 1:nCombinations){ # EachCombination=1
Sorted = sort(c(Columns[EachCombination],Rows[EachCombination]))
ThisRow=Sorted[1]
ThisColumn=Sorted[2]

OutputMatrix1[ThisRow,ThisColumn] <- OutputMatrix1[ThisRow,ThisColumn] + 1
} # End 'each combination' row

} # End 'each row' loop

# Transpose matrix and add to existing matrix to create symmetrical matrix
TransMatrix1 <- t(OutputMatrix1)
SymMatrix1 <- OutputMatrix1 + TransMatrix1

# Write this out to your working directory
write.table(SymMatrix1,file="Output_Matrix_Context1_counts.txt",sep="\t",quote=FALSE)

```

Context 1: Create similarity matrix by percentages (symmetrical)

```{r}
nRow = nrow(Dataset1)

AllCodes = as.character(Lookup1$Code)
AllCodes = sort(AllCodes)

OutputMatrix1 = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

for(EachRow in 1:nRow){ # EachRow=1

CurrentRow=Dataset1[EachRow,]

Version=as.character(CurrentRow[,"Version"])
Slide=CurrentRow[,"Slide"]
Group=as.character(CurrentRow[,"Group"])
Tokens=as.character(CurrentRow[,"Tokens"])
SplitTokens = as.integer(strsplit(Tokens,split=",")[[1]])

nTokens=length(SplitTokens)
Recoded = rep(NA,times=nTokens)
for(EachToken in 1:nTokens){ # EachToken=1

CurrentToken=SplitTokens[EachToken]

if(Version=="A"){ColumnName="ASlide"}else{ColumnName="BSlide"}

Target= Lookup[,ColumnName]==Slide & 
        Lookup$IconNumber==CurrentToken

Recoded[EachToken] <- as.character(Lookup[Target,"Code"])

} # End 'EachToken' loop

Combinations = t(combn(Recoded,m=2))
Columns=Combinations[,1]
Rows=Combinations[,2]

nCombinations = nrow(Combinations)
for(EachCombination in 1:nCombinations){ # EachCombination=1
Sorted = sort(c(Columns[EachCombination],Rows[EachCombination]))
ThisRow=Sorted[1]
ThisColumn=Sorted[2]

OutputMatrix1[ThisRow,ThisColumn] <- OutputMatrix1[ThisRow,ThisColumn] + 1
} # End 'each combination' row

} # End 'each row' loop

# Transpose matrix and add to existing matrix to create symmetrical matrix
TransMatrix1 <- t(OutputMatrix1)
SymMatrix1 <- OutputMatrix1 + TransMatrix1

# Divide matrix by number of participants
NbParticipants1 = length(unique(Dataset1$Subject))
SymMatrix1_percent <- SymMatrix1/NbParticipants1

# Set diagonal to 1 (stimuli are maximally similar to themselves)
diag(SymMatrix1_percent) = 1

# Write this out to your working directory
write.table(SymMatrix1_percent,file="Output_Matrix_Context1_percent.txt",sep="\t",quote=FALSE)

```

Context 1: Create DIS-similarity matrix by percentages (symmetrical) (For MDS)

```{r}
nRow = nrow(Dataset1)

AllCodes = as.character(Lookup1$Code)
AllCodes = sort(AllCodes)

OutputMatrix1 = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

for(EachRow in 1:nRow){ # EachRow=1

CurrentRow=Dataset1[EachRow,]

Version=as.character(CurrentRow[,"Version"])
Slide=CurrentRow[,"Slide"]
Group=as.character(CurrentRow[,"Group"])
Tokens=as.character(CurrentRow[,"Tokens"])
SplitTokens = as.integer(strsplit(Tokens,split=",")[[1]])

nTokens=length(SplitTokens)
Recoded = rep(NA,times=nTokens)
for(EachToken in 1:nTokens){ # EachToken=1

CurrentToken=SplitTokens[EachToken]

if(Version=="A"){ColumnName="ASlide"}else{ColumnName="BSlide"}

Target= Lookup[,ColumnName]==Slide & 
        Lookup$IconNumber==CurrentToken

Recoded[EachToken] <- as.character(Lookup[Target,"Code"])

} # End 'EachToken' loop

Combinations = t(combn(Recoded,m=2))
Columns=Combinations[,1]
Rows=Combinations[,2]

nCombinations = nrow(Combinations)
for(EachCombination in 1:nCombinations){ # EachCombination=1
Sorted = sort(c(Columns[EachCombination],Rows[EachCombination]))
ThisRow=Sorted[1]
ThisColumn=Sorted[2]

OutputMatrix1[ThisRow,ThisColumn] <- OutputMatrix1[ThisRow,ThisColumn] + 1
} # End 'each combination' row

} # End 'each row' loop

# Transpose matrix and add to existing matrix to create symmetrical matrix
TransMatrix1 <- t(OutputMatrix1)
SymMatrix1 <- OutputMatrix1 + TransMatrix1

# Divide matrix by number of participants
NbParticipants1 = length(unique(Dataset1$Subject))
DisSymMatrix1_percent <- 1 - (SymMatrix1/NbParticipants1)

# Set diagonal to 0 (stimuli are minimally dissimilar to themselves)
diag(DisSymMatrix1_percent) = 0

# Write this out to your working directory
write.table(DisSymMatrix1_percent,file="Output_Matrix_Context1_percent_dis.txt",sep="\t",quote=FALSE)

```

Context 1: Create a column for Condition + Context codes to use with analyses for all speakers combined

```{r}
# Create a column for Condition + Context codes
Lookup1 = Lookup1 %>%
  unite ("ConditionContext", c(Condition,Context), remove = FALSE)
```

Context 1: Create similarity matrix by counts with all speakers combined (symmetrical)

```{r}
nRow = nrow(Dataset1)

# Make these codes the names for the rows and columns
AllCodes = as.character(unique(Lookup1$ConditionContext))
AllCodes = sort(AllCodes)

#AllCodes = as.character(unique(Lookup1$Condition))
#AllCodes = sort(AllCodes)

OutputMatrix1 = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

for(EachRow in 1:nRow){ # EachRow=1

CurrentRow=Dataset1[EachRow,]

Version=as.character(CurrentRow[,"Version"])
Slide=CurrentRow[,"Slide"]
Group=as.character(CurrentRow[,"Group"])
Tokens=as.character(CurrentRow[,"Tokens"])
SplitTokens = as.integer(strsplit(Tokens,split=",")[[1]])

nTokens=length(SplitTokens)
Recoded = rep(NA,times=nTokens)
for(EachToken in 1:nTokens){ # EachToken=1

CurrentToken=SplitTokens[EachToken]

if(Version=="A"){ColumnName="ASlide"}else{ColumnName="BSlide"}

Target= Lookup1[,ColumnName]==Slide & 
        Lookup1$IconNumber==CurrentToken

Recoded[EachToken] <- as.character(Lookup1[Target,"ConditionContext"])

} # End 'EachToken' loop

Combinations = t(combn(Recoded,m=2))
Columns=Combinations[,1]
Rows=Combinations[,2]

nCombinations = nrow(Combinations)
for(EachCombination in 1:nCombinations){ # EachCombination=1
Sorted = sort(c(Columns[EachCombination],Rows[EachCombination]))
ThisRow=Sorted[1]
ThisColumn=Sorted[2]

OutputMatrix1[ThisRow,ThisColumn] <- OutputMatrix1[ThisRow,ThisColumn] + 1
} # End 'each combination' row

} # End 'each row' loop

# Transpose matrix so numbers are in bottom right
TransMatrix1 <- t(OutputMatrix1)

# Set diagonals to 0 for original matrix so the numbers aren't doubled when the matrices are added
diag(TransMatrix1) = 0

# Add matrices together
SymMatrix1_all <- OutputMatrix1 + TransMatrix1

# Write this out to your working directory
write.table(SymMatrix1_all,file="Output_Matrix_Context1_Allspeakers_counts.txt",sep="\t",quote=FALSE)

```

Context 1: Create similarity matrix by percentages with all speakers combined (symmetrical)

```{r}
nRow = nrow(Dataset1)

AllCodes = as.character(unique(Lookup1$ConditionContext))
AllCodes = sort(AllCodes)

OutputMatrix1 = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

for(EachRow in 1:nRow){ # EachRow=1

CurrentRow=Dataset1[EachRow,]

Version=as.character(CurrentRow[,"Version"])
Slide=CurrentRow[,"Slide"]
Group=as.character(CurrentRow[,"Group"])
Tokens=as.character(CurrentRow[,"Tokens"])
SplitTokens = as.integer(strsplit(Tokens,split=",")[[1]])

nTokens=length(SplitTokens)
Recoded = rep(NA,times=nTokens)
for(EachToken in 1:nTokens){ # EachToken=1

CurrentToken=SplitTokens[EachToken]

if(Version=="A"){ColumnName="ASlide"}else{ColumnName="BSlide"}

Target= Lookup1[,ColumnName]==Slide & 
        Lookup1$IconNumber==CurrentToken

Recoded[EachToken] <- as.character(Lookup1[Target,"ConditionContext"])

} # End 'EachToken' loop

Combinations = t(combn(Recoded,m=2))
Columns=Combinations[,1]
Rows=Combinations[,2]

nCombinations = nrow(Combinations)
for(EachCombination in 1:nCombinations){ # EachCombination=1
Sorted = sort(c(Columns[EachCombination],Rows[EachCombination]))
ThisRow=Sorted[1]
ThisColumn=Sorted[2]

OutputMatrix1[ThisRow,ThisColumn] <- OutputMatrix1[ThisRow,ThisColumn] + 1
} # End 'each combination' row

} # End 'each row' loop

# Transpose matrix so numbers are in bottom right
TransMatrix1 <- t(OutputMatrix1)

# Get values along diagonal (i.e. condition with itself)
TransMatrix1_diag <- diag(diag(TransMatrix1))

# Set diagonals to 0 for original matrix so the numbers aren't doubled when the matrices are added
diag(TransMatrix1) = 0

# Get number of participants and speakers
NbParticipants1 = length(unique(Dataset1$Subject))
NbSpeakers1 = length(unique(Lookup1$Speaker))

# Divide diagonal by number of (participants x number of speakers x (number of speakers - 1))/2 to get percentages (because stimuli can't be paired with themselves, so need to remove the number of diagonal cells, and divided by 2 because cells are identical across the diagonal)
TransMatrix1_diag_percent <- TransMatrix1_diag/((NbParticipants1*NbSpeakers1*(NbSpeakers1-1))/2)

# Add original matrices together
SymMatrix1_all <- OutputMatrix1 + TransMatrix1

# Divide resulting matrix by number of participants x (number of speakers x number of speakers) (i.e. the number of cells being combined) to get percentages
SymMatrix1_all_percent <- SymMatrix1_all/(NbParticipants1*NbSpeakers1*NbSpeakers1)

# Set diagonals to 0 for matrix
diag(SymMatrix1_all_percent) = 0

# Replace diagonal with correct numbers
SymMatrix1_all_percent <- SymMatrix1_all_percent + TransMatrix1_diag_percent

# Write this out to your working directory
write.table(SymMatrix1_all_percent,file="Output_Matrix_Context1_Allspeakers_percent.txt",sep="\t",quote=FALSE)

```

Context 1: Create DIS-similarity matrix by percentages with all speakers combined (symmetrical)

```{r}
nRow = nrow(Dataset1)

AllCodes = as.character(unique(Lookup1$ConditionContext))
AllCodes = sort(AllCodes)

OutputMatrix1 = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

for(EachRow in 1:nRow){ # EachRow=1

CurrentRow=Dataset1[EachRow,]

Version=as.character(CurrentRow[,"Version"])
Slide=CurrentRow[,"Slide"]
Group=as.character(CurrentRow[,"Group"])
Tokens=as.character(CurrentRow[,"Tokens"])
SplitTokens = as.integer(strsplit(Tokens,split=",")[[1]])

nTokens=length(SplitTokens)
Recoded = rep(NA,times=nTokens)
for(EachToken in 1:nTokens){ # EachToken=1

CurrentToken=SplitTokens[EachToken]

if(Version=="A"){ColumnName="ASlide"}else{ColumnName="BSlide"}

Target= Lookup1[,ColumnName]==Slide & 
        Lookup1$IconNumber==CurrentToken

Recoded[EachToken] <- as.character(Lookup1[Target,"ConditionContext"])

} # End 'EachToken' loop

Combinations = t(combn(Recoded,m=2))
Columns=Combinations[,1]
Rows=Combinations[,2]

nCombinations = nrow(Combinations)
for(EachCombination in 1:nCombinations){ # EachCombination=1
Sorted = sort(c(Columns[EachCombination],Rows[EachCombination]))
ThisRow=Sorted[1]
ThisColumn=Sorted[2]

OutputMatrix1[ThisRow,ThisColumn] <- OutputMatrix1[ThisRow,ThisColumn] + 1
} # End 'each combination' row

} # End 'each row' loop

# Transpose matrix so numbers are in bottom right
TransMatrix1 <- t(OutputMatrix1)

# Get values along diagonal (i.e. condition with itself)
TransMatrix1_diag <- diag(diag(TransMatrix1))

# Set diagonals to 0 for original matrix so the numbers aren't doubled when the matrices are added
diag(TransMatrix1) = 0

# Get number of participants and speakers
NbParticipants1 = length(unique(Dataset1$Subject))
NbSpeakers1 = length(unique(Lookup1$Speaker))

# Divide diagonal by number of (participants x number of speakers x (number of speakers - 1))/2 to get percentages (because stimuli can't be paired with themselves, so need to remove the number of diagonal cells, and divided by 2 because cells are identical across the diagonal)
TransMatrix1_diag_percent <- TransMatrix1_diag/((NbParticipants1*NbSpeakers1*(NbSpeakers1-1))/2)

# Add original matrices together
SymMatrix1_all <- OutputMatrix1 + TransMatrix1

# Divide resulting matrix by number of participants x (number of speakers x number of speakers) (i.e. the number of cells being combined) to get percentages
SymMatrix1_all_percent <- SymMatrix1_all/(NbParticipants1*NbSpeakers1*NbSpeakers1)

# Set diagonals to 0 for matrix
diag(SymMatrix1_all_percent) = 0

# Replace diagonal with correct numbers
SymMatrix1_all_percent <- SymMatrix1_all_percent + TransMatrix1_diag_percent

# Make dissimilarity matrix
DisSymMatrix1_all_percent <- 1-SymMatrix1_all_percent

# Write this out to your working directory
write.table(DisSymMatrix1_all_percent,file="Output_Matrix_Context1_Allspeakers_percent_dis.txt",sep="\t",quote=FALSE)

```


*****Context 2*****


Subset data by second context
```{r}
Dataset2 <- select(filter(Dataset, Context == Context2), c("Subject","Version","Slide","Context","Group","Tokens"))

head(Dataset2)

# Subject Version Slide Context Group   Tokens
#1	B08	    B	      2	    iki	    1	    3,11,5,10,15
#2	B08	    B	      2	    iki	    2	    2,4,14
#3	B08	    B	      2	    iki	    3	    1,12,21,24,19,23
#4	B08	    B	      2	    iki	    4	    17,20,22
#5	B08	    B	      2	    iki	    5	    9,8,13,6,7,18,16
#6	B13	    B	      2	    iki	    1	    1,3,19

```

Subset look-up matrix by second context
```{r}
Lookup2 <- select(filter(Lookup, Context == Context2), c("ASlide","BSlide","IconNumber","Condition","Context","Speaker","Code"))

head(Lookup2)

# ASlide  BSlide IconNumber Condition Context Speaker Code
#1	2	      2	      1	        cVcV	    iki	    L	    cVcV_iki_L
#2	2	      2	      2	        cVVccV	  iki	    N	    cVVccV_iki_N
#3	2	      2	      3	        cVcVV	    iki	    M	    cVcVV_iki_M
#4	2	      2	      4	        cVccV	    iki	    N	    cVccV_iki_N
#5	2	      2	      5	        cVVcV	    iki	    M	    cVVcV_iki_M
#6	2	      2	      6	        cVVccVV	  iki	    L	    cVVccVV_iki_L

```


Context 2: Create similarity matrix by counts (symmetrical)

```{r}
nRow = nrow(Dataset2)

AllCodes = as.character(Lookup2$Code)
AllCodes = sort(AllCodes)

OutputMatrix2 = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

for(EachRow in 1:nRow){ # EachRow=1

CurrentRow=Dataset2[EachRow,]

Version=as.character(CurrentRow[,"Version"])
Slide=CurrentRow[,"Slide"]
Group=as.character(CurrentRow[,"Group"])
Tokens=as.character(CurrentRow[,"Tokens"])
SplitTokens = as.integer(strsplit(Tokens,split=",")[[1]])

nTokens=length(SplitTokens)
Recoded = rep(NA,times=nTokens)
for(EachToken in 1:nTokens){ # EachToken=1

CurrentToken=SplitTokens[EachToken]

if(Version=="A"){ColumnName="ASlide"}else{ColumnName="BSlide"}

Target= Lookup[,ColumnName]==Slide & 
        Lookup$IconNumber==CurrentToken

Recoded[EachToken] <- as.character(Lookup[Target,"Code"])

} # End 'EachToken' loop

Combinations = t(combn(Recoded,m=2))
Columns=Combinations[,1]
Rows=Combinations[,2]

nCombinations = nrow(Combinations)
for(EachCombination in 1:nCombinations){ # EachCombination=1
Sorted = sort(c(Columns[EachCombination],Rows[EachCombination]))
ThisRow=Sorted[1]
ThisColumn=Sorted[2]

OutputMatrix2[ThisRow,ThisColumn] <- OutputMatrix2[ThisRow,ThisColumn] + 1
} # End 'each combination' row

} # End 'each row' loop

# Transpose matrix and add to existing matrix to create symmetrical matrix
TransMatrix2 <- t(OutputMatrix2)
SymMatrix2 <- OutputMatrix2 + TransMatrix2

# Write this out to your working directory
write.table(SymMatrix2,file="Output_Matrix_Context2_counts.txt",sep="\t",quote=FALSE)

```

Context 2: Create similarity matrix by percentages (symmetrical)

```{r}
nRow = nrow(Dataset2)

AllCodes = as.character(Lookup2$Code)
AllCodes = sort(AllCodes)

OutputMatrix2 = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

for(EachRow in 1:nRow){ # EachRow=1

CurrentRow=Dataset2[EachRow,]

Version=as.character(CurrentRow[,"Version"])
Slide=CurrentRow[,"Slide"]
Group=as.character(CurrentRow[,"Group"])
Tokens=as.character(CurrentRow[,"Tokens"])
SplitTokens = as.integer(strsplit(Tokens,split=",")[[1]])

nTokens=length(SplitTokens)
Recoded = rep(NA,times=nTokens)
for(EachToken in 1:nTokens){ # EachToken=1

CurrentToken=SplitTokens[EachToken]

if(Version=="A"){ColumnName="ASlide"}else{ColumnName="BSlide"}

Target= Lookup[,ColumnName]==Slide & 
        Lookup$IconNumber==CurrentToken

Recoded[EachToken] <- as.character(Lookup[Target,"Code"])

} # End 'EachToken' loop

Combinations = t(combn(Recoded,m=2))
Columns=Combinations[,1]
Rows=Combinations[,2]

nCombinations = nrow(Combinations)
for(EachCombination in 1:nCombinations){ # EachCombination=1
Sorted = sort(c(Columns[EachCombination],Rows[EachCombination]))
ThisRow=Sorted[1]
ThisColumn=Sorted[2]

OutputMatrix2[ThisRow,ThisColumn] <- OutputMatrix2[ThisRow,ThisColumn] + 1
} # End 'each combination' row

} # End 'each row' loop

# Transpose matrix and add to existing matrix to create symmetrical matrix
TransMatrix2 <- t(OutputMatrix2)
SymMatrix2 <- OutputMatrix2 + TransMatrix2

# Divide matrix by number of participants
NbParticipants2 = length(unique(Dataset2$Subject))
SymMatrix2_percent <- SymMatrix2/NbParticipants2

# Set diagonal to 1 (stimuli are maximally similar to themselves)
diag(SymMatrix2_percent) = 1

# Write this out to your working directory
write.table(SymMatrix2_percent,file="Output_Matrix_Context2_percent.txt",sep="\t",quote=FALSE)

```

Context 2: Create DIS-similarity matrix by percentages (symmetrical) (For MDS)

```{r}
nRow = nrow(Dataset2)

AllCodes = as.character(Lookup2$Code)
AllCodes = sort(AllCodes)

OutputMatrix2 = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

for(EachRow in 1:nRow){ # EachRow=1

CurrentRow=Dataset2[EachRow,]

Version=as.character(CurrentRow[,"Version"])
Slide=CurrentRow[,"Slide"]
Group=as.character(CurrentRow[,"Group"])
Tokens=as.character(CurrentRow[,"Tokens"])
SplitTokens = as.integer(strsplit(Tokens,split=",")[[1]])

nTokens=length(SplitTokens)
Recoded = rep(NA,times=nTokens)
for(EachToken in 1:nTokens){ # EachToken=1

CurrentToken=SplitTokens[EachToken]

if(Version=="A"){ColumnName="ASlide"}else{ColumnName="BSlide"}

Target= Lookup[,ColumnName]==Slide & 
        Lookup$IconNumber==CurrentToken

Recoded[EachToken] <- as.character(Lookup[Target,"Code"])

} # End 'EachToken' loop

Combinations = t(combn(Recoded,m=2))
Columns=Combinations[,1]
Rows=Combinations[,2]

nCombinations = nrow(Combinations)
for(EachCombination in 1:nCombinations){ # EachCombination=1
Sorted = sort(c(Columns[EachCombination],Rows[EachCombination]))
ThisRow=Sorted[1]
ThisColumn=Sorted[2]

OutputMatrix2[ThisRow,ThisColumn] <- OutputMatrix2[ThisRow,ThisColumn] + 1
} # End 'each combination' row

} # End 'each row' loop

# Transpose matrix and add to existing matrix to create symmetrical matrix
TransMatrix2 <- t(OutputMatrix2)
SymMatrix2 <- OutputMatrix2 + TransMatrix2

# Divide matrix by number of participants
NbParticipants2 = length(unique(Dataset2$Subject))
DisSymMatrix2_percent <- 1 - (SymMatrix2/NbParticipants2)

# Set diagonal to 0 (stimuli are minimally dissimilar to themselves)
diag(DisSymMatrix2_percent) = 0

# Write this out to your working directory
write.table(DisSymMatrix2_percent,file="Output_Matrix_Context2_percent_dis.txt",sep="\t",quote=FALSE)

```

Context 2: Create a column for Condition + Context codes to use with analyses for all speakers combined

```{r}
# Create a column for Condition + Context codes
Lookup2 = Lookup2 %>%
  unite ("ConditionContext", c(Condition,Context), remove = FALSE)
```

Context 2: Create similarity matrix by counts with all speakers combined (symmetrical)

```{r}
nRow = nrow(Dataset2)

AllCodes = as.character(unique(Lookup2$ConditionContext))
AllCodes = sort(AllCodes)

OutputMatrix2 = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

for(EachRow in 1:nRow){ # EachRow=1

CurrentRow=Dataset2[EachRow,]

Version=as.character(CurrentRow[,"Version"])
Slide=CurrentRow[,"Slide"]
Group=as.character(CurrentRow[,"Group"])
Tokens=as.character(CurrentRow[,"Tokens"])
SplitTokens = as.integer(strsplit(Tokens,split=",")[[1]])

nTokens=length(SplitTokens)
Recoded = rep(NA,times=nTokens)
for(EachToken in 1:nTokens){ # EachToken=1

CurrentToken=SplitTokens[EachToken]

if(Version=="A"){ColumnName="ASlide"}else{ColumnName="BSlide"}

Target= Lookup2[,ColumnName]==Slide & 
        Lookup2$IconNumber==CurrentToken

Recoded[EachToken] <- as.character(Lookup2[Target,"ConditionContext"])

} # End 'EachToken' loop

Combinations = t(combn(Recoded,m=2))
Columns=Combinations[,1]
Rows=Combinations[,2]

nCombinations = nrow(Combinations)
for(EachCombination in 1:nCombinations){ # EachCombination=1
Sorted = sort(c(Columns[EachCombination],Rows[EachCombination]))
ThisRow=Sorted[1]
ThisColumn=Sorted[2]

OutputMatrix2[ThisRow,ThisColumn] <- OutputMatrix2[ThisRow,ThisColumn] + 1
} # End 'each combination' row

} # End 'each row' loop

# Transpose matrix so numbers are in bottom right
TransMatrix2 <- t(OutputMatrix2)

# Set diagonals to 0 for original matrix so the numbers aren't doubled when the matrices are added
diag(TransMatrix2) = 0

# Add matrices together
SymMatrix2_all <- OutputMatrix2 + TransMatrix2

# Write this out to your working directory
write.table(SymMatrix2_all,file="Output_Matrix_Context2_Allspeakers_counts.txt",sep="\t",quote=FALSE)

```

Context 2: Create similarity matrix by percentages with all speakers combined (symmetrical)

```{r}
nRow = nrow(Dataset2)

AllCodes = as.character(unique(Lookup2$ConditionContext))
AllCodes = sort(AllCodes)

OutputMatrix2 = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

for(EachRow in 1:nRow){ # EachRow=1

CurrentRow=Dataset2[EachRow,]

Version=as.character(CurrentRow[,"Version"])
Slide=CurrentRow[,"Slide"]
Group=as.character(CurrentRow[,"Group"])
Tokens=as.character(CurrentRow[,"Tokens"])
SplitTokens = as.integer(strsplit(Tokens,split=",")[[1]])

nTokens=length(SplitTokens)
Recoded = rep(NA,times=nTokens)
for(EachToken in 1:nTokens){ # EachToken=1

CurrentToken=SplitTokens[EachToken]

if(Version=="A"){ColumnName="ASlide"}else{ColumnName="BSlide"}

Target= Lookup2[,ColumnName]==Slide & 
        Lookup2$IconNumber==CurrentToken

Recoded[EachToken] <- as.character(Lookup2[Target,"ConditionContext"])

} # End 'EachToken' loop

Combinations = t(combn(Recoded,m=2))
Columns=Combinations[,1]
Rows=Combinations[,2]

nCombinations = nrow(Combinations)
for(EachCombination in 1:nCombinations){ # EachCombination=1
Sorted = sort(c(Columns[EachCombination],Rows[EachCombination]))
ThisRow=Sorted[1]
ThisColumn=Sorted[2]

OutputMatrix2[ThisRow,ThisColumn] <- OutputMatrix2[ThisRow,ThisColumn] + 1
} # End 'each combination' row

} # End 'each row' loop

# Transpose matrix so numbers are in bottom right
TransMatrix2 <- t(OutputMatrix2)

# Get values along diagonal (i.e. condition with itself)
TransMatrix2_diag <- diag(diag(TransMatrix2))

# Set diagonals to 0 for original matrix so the numbers aren't doubled when the matrices are added
diag(TransMatrix2) = 0

# Get number of participants and speakers
NbParticipants2 = length(unique(Dataset2$Subject))
NbSpeakers2 = length(unique(Lookup2$Speaker))

# Divide diagonal by number of (participants x number of speakers x (number of speakers - 1))/2 to get percentages (because stimuli can't be paired with themselves, so need to remove the number of diagonal cells, and divided by 2 because cells are identical across the diagonal)
TransMatrix2_diag_percent <- TransMatrix2_diag/((NbParticipants2*NbSpeakers2*(NbSpeakers2-1))/2)

# Add original matrices together
SymMatrix2_all <- OutputMatrix2 + TransMatrix2

# Divide resulting matrix by number of participants x (number of speakers x number of speakers) (i.e. the number of cells being combined) to get percentages
SymMatrix2_all_percent <- SymMatrix2_all/(NbParticipants2*NbSpeakers2*NbSpeakers2)

# Set diagonals to 0 for matrix
diag(SymMatrix2_all_percent) = 0

# Replace diagonal with correct numbers
SymMatrix2_all_percent <- SymMatrix2_all_percent + TransMatrix2_diag_percent

# Write this out to your working directory
write.table(SymMatrix2_all_percent,file="Output_Matrix_Context2_Allspeakers_percent.txt",sep="\t",quote=FALSE)

```

Context 2: Create DIS-similarity matrix by percentages with all speakers combined (symmetrical)

```{r}
nRow = nrow(Dataset2)

AllCodes = as.character(unique(Lookup2$ConditionContext))
AllCodes = sort(AllCodes)

OutputMatrix2 = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

for(EachRow in 1:nRow){ # EachRow=1

CurrentRow=Dataset2[EachRow,]

Version=as.character(CurrentRow[,"Version"])
Slide=CurrentRow[,"Slide"]
Group=as.character(CurrentRow[,"Group"])
Tokens=as.character(CurrentRow[,"Tokens"])
SplitTokens = as.integer(strsplit(Tokens,split=",")[[1]])

nTokens=length(SplitTokens)
Recoded = rep(NA,times=nTokens)
for(EachToken in 1:nTokens){ # EachToken=1

CurrentToken=SplitTokens[EachToken]

if(Version=="A"){ColumnName="ASlide"}else{ColumnName="BSlide"}

Target= Lookup2[,ColumnName]==Slide & 
        Lookup2$IconNumber==CurrentToken

Recoded[EachToken] <- as.character(Lookup2[Target,"ConditionContext"])

} # End 'EachToken' loop

Combinations = t(combn(Recoded,m=2))
Columns=Combinations[,1]
Rows=Combinations[,2]

nCombinations = nrow(Combinations)
for(EachCombination in 1:nCombinations){ # EachCombination=1
Sorted = sort(c(Columns[EachCombination],Rows[EachCombination]))
ThisRow=Sorted[1]
ThisColumn=Sorted[2]

OutputMatrix2[ThisRow,ThisColumn] <- OutputMatrix2[ThisRow,ThisColumn] + 1
} # End 'each combination' row

} # End 'each row' loop

# Transpose matrix so numbers are in bottom right
TransMatrix2 <- t(OutputMatrix2)

# Get values along diagonal (i.e. condition with itself)
TransMatrix2_diag <- diag(diag(TransMatrix2))

# Set diagonals to 0 for original matrix so the numbers aren't doubled when the matrices are added
diag(TransMatrix2) = 0

# Get number of participants and speakers
NbParticipants2 = length(unique(Dataset2$Subject))
NbSpeakers2 = length(unique(Lookup2$Speaker))

# Divide diagonal by number of (participants x number of speakers x (number of speakers - 1))/2 to get percentages (because stimuli can't be paired with themselves, so need to remove the number of diagonal cells, and divided by 2 because cells are identical across the diagonal)
TransMatrix2_diag_percent <- TransMatrix2_diag/((NbParticipants2*NbSpeakers2*(NbSpeakers2-1))/2)

# Add original matrices together
SymMatrix2_all <- OutputMatrix2 + TransMatrix2

# Divide resulting matrix by number of participants x (number of speakers x number of speakers) (i.e. the number of cells being combined) to get percentages
SymMatrix2_all_percent <- SymMatrix2_all/(NbParticipants2*NbSpeakers2*NbSpeakers2)

# Set diagonals to 0 for matrix
diag(SymMatrix2_all_percent) = 0

# Replace diagonal with correct numbers
SymMatrix2_all_percent <- SymMatrix2_all_percent + TransMatrix2_diag_percent

# Make dissimilarity matrix
DisSymMatrix2_all_percent <- 1-SymMatrix2_all_percent

# Write this out to your working directory
write.table(DisSymMatrix2_all_percent,file="Output_Matrix_Context2_Allspeakers_percent_dis.txt",sep="\t",quote=FALSE)

```



*****Context 3*****


Subset data by third context
```{r}
Dataset3 <- select(filter(Dataset, Context == Context3), c("Subject","Version","Slide","Context","Group","Tokens"))

head(Dataset3)

# Subject Version Slide Context Group   Tokens
#1	B08	    B	      3	    ata	    1	    3,11,20
#2	B08	    B	      3	    ata	    2	    1,4,14,10,5,12,19
#3	B08	    B	      3	    ata	    3	    16,22
#4	B08	    B	      3	    ata	    4	    24,18,21,8,9,13
#5	B08	    B	      3	    ata	    5	    2,6,7,15,17,23
#6	B13	    B	      3	    ata	    1	    1,3,5,21
```

Subset look-up matrix by third context
```{r}
Lookup3 <- select(filter(Lookup, Context == Context3), c("ASlide","BSlide","IconNumber","Condition","Context","Speaker","Code"))

head(Lookup3)

# ASlide  BSlide IconNumber Condition Context Speaker Code
#1	1	      3	      1	        cVccV	    ata	    M	    cVccV_ata_M
#2	1	      3	      2	        cVVccVV	  ata	    M	    cVVccVV_ata_M
#3	1	      3	      3	        cVccVV	  ata	    M	    cVccVV_ata_M
#4	1	      3	      4	        cVcV	    ata	    N	    cVcV_ata_N
#5	1	      3	      5	        cVcVV	    ata	    M	    cVcVV_ata_M
#6	1	      3	      6	        cVVccV	  ata	    N	    cVVccV_ata_N
```


Context 3: Create similarity matrix by counts (symmetrical)

```{r}
nRow = nrow(Dataset3)

AllCodes = as.character(Lookup3$Code)
AllCodes = sort(AllCodes)

OutputMatrix3 = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

for(EachRow in 1:nRow){ # EachRow=1

CurrentRow=Dataset3[EachRow,]

Version=as.character(CurrentRow[,"Version"])
Slide=CurrentRow[,"Slide"]
Group=as.character(CurrentRow[,"Group"])
Tokens=as.character(CurrentRow[,"Tokens"])
SplitTokens = as.integer(strsplit(Tokens,split=",")[[1]])

nTokens=length(SplitTokens)
Recoded = rep(NA,times=nTokens)
for(EachToken in 1:nTokens){ # EachToken=1

CurrentToken=SplitTokens[EachToken]

if(Version=="A"){ColumnName="ASlide"}else{ColumnName="BSlide"}

Target= Lookup[,ColumnName]==Slide & 
        Lookup$IconNumber==CurrentToken

Recoded[EachToken] <- as.character(Lookup[Target,"Code"])

} # End 'EachToken' loop

Combinations = t(combn(Recoded,m=2))
Columns=Combinations[,1]
Rows=Combinations[,2]

nCombinations = nrow(Combinations)
for(EachCombination in 1:nCombinations){ # EachCombination=1
Sorted = sort(c(Columns[EachCombination],Rows[EachCombination]))
ThisRow=Sorted[1]
ThisColumn=Sorted[2]

OutputMatrix3[ThisRow,ThisColumn] <- OutputMatrix3[ThisRow,ThisColumn] + 1
} # End 'each combination' row

} # End 'each row' loop

# Transpose matrix and add to existing matrix to create symmetrical matrix
TransMatrix3 <- t(OutputMatrix3)
SymMatrix3 <- OutputMatrix3 + TransMatrix3

# Write this out to your working directory
write.table(SymMatrix3,file="Output_Matrix_Context3_counts.txt",sep="\t",quote=FALSE)

```

Context 3: Create similarity matrix by percentages (symmetrical)

```{r}
nRow = nrow(Dataset3)

AllCodes = as.character(Lookup3$Code)
AllCodes = sort(AllCodes)

OutputMatrix3 = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

for(EachRow in 1:nRow){ # EachRow=1

CurrentRow=Dataset3[EachRow,]

Version=as.character(CurrentRow[,"Version"])
Slide=CurrentRow[,"Slide"]
Group=as.character(CurrentRow[,"Group"])
Tokens=as.character(CurrentRow[,"Tokens"])
SplitTokens = as.integer(strsplit(Tokens,split=",")[[1]])

nTokens=length(SplitTokens)
Recoded = rep(NA,times=nTokens)
for(EachToken in 1:nTokens){ # EachToken=1

CurrentToken=SplitTokens[EachToken]

if(Version=="A"){ColumnName="ASlide"}else{ColumnName="BSlide"}

Target= Lookup[,ColumnName]==Slide & 
        Lookup$IconNumber==CurrentToken

Recoded[EachToken] <- as.character(Lookup[Target,"Code"])

} # End 'EachToken' loop

Combinations = t(combn(Recoded,m=2))
Columns=Combinations[,1]
Rows=Combinations[,2]

nCombinations = nrow(Combinations)
for(EachCombination in 1:nCombinations){ # EachCombination=1
Sorted = sort(c(Columns[EachCombination],Rows[EachCombination]))
ThisRow=Sorted[1]
ThisColumn=Sorted[2]

OutputMatrix3[ThisRow,ThisColumn] <- OutputMatrix3[ThisRow,ThisColumn] + 1
} # End 'each combination' row

} # End 'each row' loop

# Transpose matrix and add to existing matrix, to create symmetrical matrix
TransMatrix3 <- t(OutputMatrix3)
SymMatrix3 <- OutputMatrix3 + TransMatrix3

# Divide matrix by number of participants
NbParticipants3 = length(unique(Dataset3$Subject))
SymMatrix3_percent <- SymMatrix3/NbParticipants3

# Set diagonal to 1 (stimuli are maximally similar to themselves)
diag(SymMatrix3_percent) = 1

# Write this out to your working directory
write.table(SymMatrix3_percent,file="Output_Matrix_Context3_percent.txt",sep="\t",quote=FALSE)

```

Context 3: Create DIS-similarity matrix by percentages (symmetrical) (For MDS)

```{r}
nRow = nrow(Dataset3)

AllCodes = as.character(Lookup3$Code)
AllCodes = sort(AllCodes)

OutputMatrix3 = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

for(EachRow in 1:nRow){ # EachRow=1

CurrentRow=Dataset3[EachRow,]

Version=as.character(CurrentRow[,"Version"])
Slide=CurrentRow[,"Slide"]
Group=as.character(CurrentRow[,"Group"])
Tokens=as.character(CurrentRow[,"Tokens"])
SplitTokens = as.integer(strsplit(Tokens,split=",")[[1]])

nTokens=length(SplitTokens)
Recoded = rep(NA,times=nTokens)
for(EachToken in 1:nTokens){ # EachToken=1

CurrentToken=SplitTokens[EachToken]

if(Version=="A"){ColumnName="ASlide"}else{ColumnName="BSlide"}

Target= Lookup[,ColumnName]==Slide & 
        Lookup$IconNumber==CurrentToken

Recoded[EachToken] <- as.character(Lookup[Target,"Code"])

} # End 'EachToken' loop

Combinations = t(combn(Recoded,m=2))
Columns=Combinations[,1]
Rows=Combinations[,2]

nCombinations = nrow(Combinations)
for(EachCombination in 1:nCombinations){ # EachCombination=1
Sorted = sort(c(Columns[EachCombination],Rows[EachCombination]))
ThisRow=Sorted[1]
ThisColumn=Sorted[2]

OutputMatrix3[ThisRow,ThisColumn] <- OutputMatrix3[ThisRow,ThisColumn] + 1
} # End 'each combination' row

} # End 'each row' loop

# Transpose matrix and add to existing matrix to create symmetrical matrix
TransMatrix3 <- t(OutputMatrix3)
SymMatrix3 <- OutputMatrix3 + TransMatrix3

# Divide matrix by number of participants
NbParticipants3 = length(unique(Dataset3$Subject))
DisSymMatrix3_percent <- 1 - (SymMatrix3/NbParticipants3)

# Set diagonal to 0 (stimuli are minimally dissimilar to themselves)
diag(DisSymMatrix3_percent) = 0

# Write this out to your working directory
write.table(DisSymMatrix3_percent,file="Output_Matrix_Context3_percent_dis.txt",sep="\t",quote=FALSE)

```

Context 3: Create a column for Condition + Context codes to use with analyses for all speakers combined

```{r}
# Create a column for Condition + Context codes
Lookup3 = Lookup3 %>%
  unite ("ConditionContext", c(Condition,Context), remove = FALSE)
```

Context 3: Create similarity matrix by counts with all speakers combined (symmetrical)

```{r}
nRow = nrow(Dataset3)

AllCodes = as.character(unique(Lookup3$ConditionContext))
AllCodes = sort(AllCodes)

OutputMatrix3 = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

for(EachRow in 1:nRow){ # EachRow=1

CurrentRow=Dataset3[EachRow,]

Version=as.character(CurrentRow[,"Version"])
Slide=CurrentRow[,"Slide"]
Group=as.character(CurrentRow[,"Group"])
Tokens=as.character(CurrentRow[,"Tokens"])
SplitTokens = as.integer(strsplit(Tokens,split=",")[[1]])

nTokens=length(SplitTokens)
Recoded = rep(NA,times=nTokens)
for(EachToken in 1:nTokens){ # EachToken=1

CurrentToken=SplitTokens[EachToken]

if(Version=="A"){ColumnName="ASlide"}else{ColumnName="BSlide"}

Target= Lookup3[,ColumnName]==Slide & 
        Lookup3$IconNumber==CurrentToken

Recoded[EachToken] <- as.character(Lookup3[Target,"ConditionContext"])

} # End 'EachToken' loop

Combinations = t(combn(Recoded,m=2))
Columns=Combinations[,1]
Rows=Combinations[,2]

nCombinations = nrow(Combinations)
for(EachCombination in 1:nCombinations){ # EachCombination=1
Sorted = sort(c(Columns[EachCombination],Rows[EachCombination]))
ThisRow=Sorted[1]
ThisColumn=Sorted[2]

OutputMatrix3[ThisRow,ThisColumn] <- OutputMatrix3[ThisRow,ThisColumn] + 1
} # End 'each combination' row

} # End 'each row' loop

# Transpose matrix so numbers are in bottom right
TransMatrix3 <- t(OutputMatrix3)

# Set diagonals to 0 for original matrix so the numbers aren't doubled when the matrices are added
diag(TransMatrix3) = 0

# Add matrices together
SymMatrix3_all <- OutputMatrix3 + TransMatrix3

# Write this out to your working directory
write.table(SymMatrix3_all,file="Output_Matrix_Context3_Allspeakers_counts.txt",sep="\t",quote=FALSE)

```

Context 3: Create similarity matrix by percentages with all speakers combined (symmetrical)

```{r}
nRow = nrow(Dataset3)

AllCodes = as.character(unique(Lookup3$ConditionContext))
AllCodes = sort(AllCodes)

OutputMatrix3 = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

for(EachRow in 1:nRow){ # EachRow=1

CurrentRow=Dataset3[EachRow,]

Version=as.character(CurrentRow[,"Version"])
Slide=CurrentRow[,"Slide"]
Group=as.character(CurrentRow[,"Group"])
Tokens=as.character(CurrentRow[,"Tokens"])
SplitTokens = as.integer(strsplit(Tokens,split=",")[[1]])

nTokens=length(SplitTokens)
Recoded = rep(NA,times=nTokens)
for(EachToken in 1:nTokens){ # EachToken=1

CurrentToken=SplitTokens[EachToken]

if(Version=="A"){ColumnName="ASlide"}else{ColumnName="BSlide"}

Target= Lookup3[,ColumnName]==Slide & 
        Lookup3$IconNumber==CurrentToken

Recoded[EachToken] <- as.character(Lookup3[Target,"ConditionContext"])

} # End 'EachToken' loop

Combinations = t(combn(Recoded,m=2))
Columns=Combinations[,1]
Rows=Combinations[,2]

nCombinations = nrow(Combinations)
for(EachCombination in 1:nCombinations){ # EachCombination=1
Sorted = sort(c(Columns[EachCombination],Rows[EachCombination]))
ThisRow=Sorted[1]
ThisColumn=Sorted[2]

OutputMatrix3[ThisRow,ThisColumn] <- OutputMatrix3[ThisRow,ThisColumn] + 1
} # End 'each combination' row

} # End 'each row' loop

# Transpose matrix so numbers are in bottom right
TransMatrix3 <- t(OutputMatrix3)

# Get values along diagonal (i.e. condition with itself)
TransMatrix3_diag <- diag(diag(TransMatrix3))

# Set diagonals to 0 for original matrix so the numbers aren't doubled when the matrices are added
diag(TransMatrix3) = 0

# Get number of participants and speakers
NbParticipants3 = length(unique(Dataset3$Subject))
NbSpeakers3 = length(unique(Lookup3$Speaker))

# Divide diagonal by number of (participants x number of speakers x (number of speakers - 1))/2 to get percentages (because stimuli can't be paired with themselves, so need to remove the number of diagonal cells, and divided by 2 because cells are identical across the diagonal)
TransMatrix3_diag_percent <- TransMatrix3_diag/((NbParticipants3*NbSpeakers3*(NbSpeakers3-1))/2)

# Add original matrices together
SymMatrix3_all <- OutputMatrix3 + TransMatrix3

# Divide resulting matrix by number of participants x (number of speakers x number of speakers) (i.e. the number of cells being combined) to get percentages
SymMatrix3_all_percent <- SymMatrix3_all/(NbParticipants3*NbSpeakers3*NbSpeakers3)

# Set diagonals to 0 for matrix
diag(SymMatrix3_all_percent) = 0

# Replace diagonal with correct numbers
SymMatrix3_all_percent <- SymMatrix3_all_percent + TransMatrix3_diag_percent

# Write this out to your working directory
write.table(SymMatrix3_all_percent,file="Output_Matrix_Context3_Allspeakers_percent.txt",sep="\t",quote=FALSE)

```

Context 3: Create DIS-similarity matrix by percentages with all speakers combined (symmetrical)

```{r}
nRow = nrow(Dataset3)

AllCodes = as.character(unique(Lookup3$ConditionContext))
AllCodes = sort(AllCodes)

OutputMatrix3 = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

for(EachRow in 1:nRow){ # EachRow=1

CurrentRow=Dataset3[EachRow,]

Version=as.character(CurrentRow[,"Version"])
Slide=CurrentRow[,"Slide"]
Group=as.character(CurrentRow[,"Group"])
Tokens=as.character(CurrentRow[,"Tokens"])
SplitTokens = as.integer(strsplit(Tokens,split=",")[[1]])

nTokens=length(SplitTokens)
Recoded = rep(NA,times=nTokens)
for(EachToken in 1:nTokens){ # EachToken=1

CurrentToken=SplitTokens[EachToken]

if(Version=="A"){ColumnName="ASlide"}else{ColumnName="BSlide"}

Target= Lookup3[,ColumnName]==Slide & 
        Lookup3$IconNumber==CurrentToken

Recoded[EachToken] <- as.character(Lookup3[Target,"ConditionContext"])

} # End 'EachToken' loop

Combinations = t(combn(Recoded,m=2))
Columns=Combinations[,1]
Rows=Combinations[,2]

nCombinations = nrow(Combinations)
for(EachCombination in 1:nCombinations){ # EachCombination=1
Sorted = sort(c(Columns[EachCombination],Rows[EachCombination]))
ThisRow=Sorted[1]
ThisColumn=Sorted[2]

OutputMatrix3[ThisRow,ThisColumn] <- OutputMatrix3[ThisRow,ThisColumn] + 1
} # End 'each combination' row

} # End 'each row' loop

# Transpose matrix so numbers are in bottom right
TransMatrix3 <- t(OutputMatrix3)

# Get values along diagonal (i.e. condition with itself)
TransMatrix3_diag <- diag(diag(TransMatrix3))

# Set diagonals to 0 for original matrix so the numbers aren't doubled when the matrices are added
diag(TransMatrix3) = 0

# Get number of participants and speakers
NbParticipants3 = length(unique(Dataset3$Subject))
NbSpeakers3 = length(unique(Lookup3$Speaker))

# Divide diagonal by number of (participants x number of speakers x (number of speakers - 1))/2 to get percentages (because stimuli can't be paired with themselves, so need to remove the number of diagonal cells, and divided by 2 because cells are identical across the diagonal)
TransMatrix3_diag_percent <- TransMatrix3_diag/((NbParticipants3*NbSpeakers3*(NbSpeakers3-1))/2)

# Add original matrices together
SymMatrix3_all <- OutputMatrix3 + TransMatrix3

# Divide resulting matrix by number of participants x (number of speakers x number of speakers) (i.e. the number of cells being combined) to get percentages
SymMatrix3_all_percent <- SymMatrix3_all/(NbParticipants3*NbSpeakers3*NbSpeakers3)

# Set diagonals to 0 for matrix
diag(SymMatrix3_all_percent) = 0

# Replace diagonal with correct numbers
SymMatrix3_all_percent <- SymMatrix3_all_percent + TransMatrix3_diag_percent

# Make dissimilarity matrix
DisSymMatrix3_all_percent <- 1-SymMatrix3_all_percent

# Write this out to your working directory
write.table(DisSymMatrix3_all_percent,file="Output_Matrix_Context3_Allspeakers_percent_dis.txt",sep="\t",quote=FALSE)

```



*****Context 4*****


Subset data by fourth context
```{r}
Dataset4 <- select(filter(Dataset, Context == Context4), c("Subject","Version","Slide","Context","Group","Tokens"))

head(Dataset4)
```

Subset look-up matrix by fourth context
```{r}
Lookup4 <- select(filter(Lookup, Context == Context4), c("ASlide","BSlide","IconNumber","Condition","Context","Speaker","Code"))

head(Lookup4)
```


Context 4: Create similarity matrix by counts (symmetrical)

```{r}
nRow = nrow(Dataset4)

AllCodes = as.character(Lookup4$Code)
AllCodes = sort(AllCodes)

OutputMatrix4 = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

for(EachRow in 1:nRow){ # EachRow=1

CurrentRow=Dataset4[EachRow,]

Version=as.character(CurrentRow[,"Version"])
Slide=CurrentRow[,"Slide"]
Group=as.character(CurrentRow[,"Group"])
Tokens=as.character(CurrentRow[,"Tokens"])
SplitTokens = as.integer(strsplit(Tokens,split=",")[[1]])

nTokens=length(SplitTokens)
Recoded = rep(NA,times=nTokens)
for(EachToken in 1:nTokens){ # EachToken=1

CurrentToken=SplitTokens[EachToken]

if(Version=="A"){ColumnName="ASlide"}else{ColumnName="BSlide"}

Target= Lookup[,ColumnName]==Slide & 
        Lookup$IconNumber==CurrentToken

Recoded[EachToken] <- as.character(Lookup[Target,"Code"])

} # End 'EachToken' loop

Combinations = t(combn(Recoded,m=2))
Columns=Combinations[,1]
Rows=Combinations[,2]

nCombinations = nrow(Combinations)
for(EachCombination in 1:nCombinations){ # EachCombination=1
Sorted = sort(c(Columns[EachCombination],Rows[EachCombination]))
ThisRow=Sorted[1]
ThisColumn=Sorted[2]

OutputMatrix4[ThisRow,ThisColumn] <- OutputMatrix4[ThisRow,ThisColumn] + 1
} # End 'each combination' row

} # End 'each row' loop

# Transpose matrix and add to existing matrix to create symmetrical matrix
TransMatrix4 <- t(OutputMatrix4)
SymMatrix4 <- OutputMatrix4 + TransMatrix4

# Write this out to your working directory
write.table(SymMatrix4,file="Output_Matrix_Context4_counts.txt",sep="\t",quote=FALSE)

```

Context 4: Create similarity matrix by percentages (symmetrical)

```{r}
nRow = nrow(Dataset4)

AllCodes = as.character(Lookup4$Code)
AllCodes = sort(AllCodes)

OutputMatrix4 = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

for(EachRow in 1:nRow){ # EachRow=1

CurrentRow=Dataset4[EachRow,]

Version=as.character(CurrentRow[,"Version"])
Slide=CurrentRow[,"Slide"]
Group=as.character(CurrentRow[,"Group"])
Tokens=as.character(CurrentRow[,"Tokens"])
SplitTokens = as.integer(strsplit(Tokens,split=",")[[1]])

nTokens=length(SplitTokens)
Recoded = rep(NA,times=nTokens)
for(EachToken in 1:nTokens){ # EachToken=1

CurrentToken=SplitTokens[EachToken]

if(Version=="A"){ColumnName="ASlide"}else{ColumnName="BSlide"}

Target= Lookup[,ColumnName]==Slide & 
        Lookup$IconNumber==CurrentToken

Recoded[EachToken] <- as.character(Lookup[Target,"Code"])

} # End 'EachToken' loop

Combinations = t(combn(Recoded,m=2))
Columns=Combinations[,1]
Rows=Combinations[,2]

nCombinations = nrow(Combinations)
for(EachCombination in 1:nCombinations){ # EachCombination=1
Sorted = sort(c(Columns[EachCombination],Rows[EachCombination]))
ThisRow=Sorted[1]
ThisColumn=Sorted[2]

OutputMatrix4[ThisRow,ThisColumn] <- OutputMatrix4[ThisRow,ThisColumn] + 1
} # End 'each combination' row

} # End 'each row' loop

# Transpose matrix and add to existing matrix, to create symmetrical matrix
TransMatrix4 <- t(OutputMatrix4)
SymMatrix4 <- OutputMatrix4 + TransMatrix4

# Divide matrix by number of participants
NbParticipants4 = length(unique(Dataset4$Subject))
SymMatrix4_percent <- SymMatrix4/NbParticipants4

# Set diagonal to 1 (stimuli are maximally similar to themselves)
diag(SymMatrix4_percent) = 1

# Write this out to your working directory
write.table(SymMatrix4_percent,file="Output_Matrix_Context4_percent.txt",sep="\t",quote=FALSE)

```

Context 4: Create DIS-similarity matrix by percentages (symmetrical) (For MDS)

```{r}
nRow = nrow(Dataset4)

AllCodes = as.character(Lookup4$Code)
AllCodes = sort(AllCodes)

OutputMatrix4 = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

for(EachRow in 1:nRow){ # EachRow=1

CurrentRow=Dataset4[EachRow,]

Version=as.character(CurrentRow[,"Version"])
Slide=CurrentRow[,"Slide"]
Group=as.character(CurrentRow[,"Group"])
Tokens=as.character(CurrentRow[,"Tokens"])
SplitTokens = as.integer(strsplit(Tokens,split=",")[[1]])

nTokens=length(SplitTokens)
Recoded = rep(NA,times=nTokens)
for(EachToken in 1:nTokens){ # EachToken=1

CurrentToken=SplitTokens[EachToken]

if(Version=="A"){ColumnName="ASlide"}else{ColumnName="BSlide"}

Target= Lookup[,ColumnName]==Slide & 
        Lookup$IconNumber==CurrentToken

Recoded[EachToken] <- as.character(Lookup[Target,"Code"])

} # End 'EachToken' loop

Combinations = t(combn(Recoded,m=2))
Columns=Combinations[,1]
Rows=Combinations[,2]

nCombinations = nrow(Combinations)
for(EachCombination in 1:nCombinations){ # EachCombination=1
Sorted = sort(c(Columns[EachCombination],Rows[EachCombination]))
ThisRow=Sorted[1]
ThisColumn=Sorted[2]

OutputMatrix4[ThisRow,ThisColumn] <- OutputMatrix4[ThisRow,ThisColumn] + 1
} # End 'each combination' row

} # End 'each row' loop

# Transpose matrix and add to existing matrix to create symmetrical matrix
TransMatrix4 <- t(OutputMatrix4)
SymMatrix4 <- OutputMatrix4 + TransMatrix4

# Divide matrix by number of participants
NbParticipants4 = length(unique(Dataset4$Subject))
DisSymMatrix4_percent <- 1 - (SymMatrix4/NbParticipants4)

# Set diagonal to 0 (stimuli are minimally dissimilar to themselves)
diag(DisSymMatrix4_percent) = 0

# Write this out to your working directory
write.table(DisSymMatrix4_percent,file="Output_Matrix_Context4_percent_dis.txt",sep="\t",quote=FALSE)

```

Context 4: Create a column for Condition + Context codes to use with analyses for all speakers combined

```{r}
# Create a column for Condition + Context codes
Lookup4 = Lookup4 %>%
  unite ("ConditionContext", c(Condition,Context), remove = FALSE)
```

Context 4: Create similarity matrix by counts with all speakers combined (symmetrical)

```{r}
nRow = nrow(Dataset4)

AllCodes = as.character(unique(Lookup4$ConditionContext))
AllCodes = sort(AllCodes)

OutputMatrix4 = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

for(EachRow in 1:nRow){ # EachRow=1

CurrentRow=Dataset4[EachRow,]

Version=as.character(CurrentRow[,"Version"])
Slide=CurrentRow[,"Slide"]
Group=as.character(CurrentRow[,"Group"])
Tokens=as.character(CurrentRow[,"Tokens"])
SplitTokens = as.integer(strsplit(Tokens,split=",")[[1]])

nTokens=length(SplitTokens)
Recoded = rep(NA,times=nTokens)
for(EachToken in 1:nTokens){ # EachToken=1

CurrentToken=SplitTokens[EachToken]

if(Version=="A"){ColumnName="ASlide"}else{ColumnName="BSlide"}

Target= Lookup4[,ColumnName]==Slide & 
        Lookup4$IconNumber==CurrentToken

Recoded[EachToken] <- as.character(Lookup4[Target,"ConditionContext"])

} # End 'EachToken' loop

Combinations = t(combn(Recoded,m=2))
Columns=Combinations[,1]
Rows=Combinations[,2]

nCombinations = nrow(Combinations)
for(EachCombination in 1:nCombinations){ # EachCombination=1
Sorted = sort(c(Columns[EachCombination],Rows[EachCombination]))
ThisRow=Sorted[1]
ThisColumn=Sorted[2]

OutputMatrix4[ThisRow,ThisColumn] <- OutputMatrix4[ThisRow,ThisColumn] + 1
} # End 'each combination' row

} # End 'each row' loop

# Transpose matrix so numbers are in bottom right
TransMatrix4 <- t(OutputMatrix4)

# Set diagonals to 0 for original matrix so the numbers aren't doubled when the matrices are added
diag(TransMatrix4) = 0

# Add matrices together
SymMatrix4_all <- OutputMatrix4 + TransMatrix4

# Write this out to your working directory
write.table(SymMatrix4_all,file="Output_Matrix_Context4_Allspeakers_counts.txt",sep="\t",quote=FALSE)

```

Context 4: Create similarity matrix by percentages with all speakers combined (symmetrical)

```{r}
nRow = nrow(Dataset4)

AllCodes = as.character(unique(Lookup4$ConditionContext))
AllCodes = sort(AllCodes)

OutputMatrix4 = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

for(EachRow in 1:nRow){ # EachRow=1

CurrentRow=Dataset4[EachRow,]

Version=as.character(CurrentRow[,"Version"])
Slide=CurrentRow[,"Slide"]
Group=as.character(CurrentRow[,"Group"])
Tokens=as.character(CurrentRow[,"Tokens"])
SplitTokens = as.integer(strsplit(Tokens,split=",")[[1]])

nTokens=length(SplitTokens)
Recoded = rep(NA,times=nTokens)
for(EachToken in 1:nTokens){ # EachToken=1

CurrentToken=SplitTokens[EachToken]

if(Version=="A"){ColumnName="ASlide"}else{ColumnName="BSlide"}

Target= Lookup4[,ColumnName]==Slide & 
        Lookup4$IconNumber==CurrentToken

Recoded[EachToken] <- as.character(Lookup4[Target,"ConditionContext"])

} # End 'EachToken' loop

Combinations = t(combn(Recoded,m=2))
Columns=Combinations[,1]
Rows=Combinations[,2]

nCombinations = nrow(Combinations)
for(EachCombination in 1:nCombinations){ # EachCombination=1
Sorted = sort(c(Columns[EachCombination],Rows[EachCombination]))
ThisRow=Sorted[1]
ThisColumn=Sorted[2]

OutputMatrix4[ThisRow,ThisColumn] <- OutputMatrix4[ThisRow,ThisColumn] + 1
} # End 'each combination' row

} # End 'each row' loop

# Transpose matrix so numbers are in bottom right
TransMatrix4 <- t(OutputMatrix4)

# Get values along diagonal (i.e. condition with itself)
TransMatrix4_diag <- diag(diag(TransMatrix4))

# Set diagonals to 0 for original matrix so the numbers aren't doubled when the matrices are added
diag(TransMatrix4) = 0

# Get number of participants and speakers
NbParticipants4 = length(unique(Dataset4$Subject))
NbSpeakers4 = length(unique(Lookup4$Speaker))

# Divide diagonal by number of (participants x number of speakers x (number of speakers - 1))/2 to get percentages (because stimuli can't be paired with themselves, so need to remove the number of diagonal cells, and divided by 2 because cells are identical across the diagonal)
TransMatrix4_diag_percent <- TransMatrix4_diag/((NbParticipants4*NbSpeakers4*(NbSpeakers4-1))/2)

# Add original matrices together
SymMatrix4_all <- OutputMatrix4 + TransMatrix4

# Divide resulting matrix by number of participants x (number of speakers x number of speakers) (i.e. the number of cells being combined) to get percentages
SymMatrix4_all_percent <- SymMatrix4_all/(NbParticipants4*NbSpeakers4*NbSpeakers4)

# Set diagonals to 0 for matrix
diag(SymMatrix4_all_percent) = 0

# Replace diagonal with correct numbers
SymMatrix4_all_percent <- SymMatrix4_all_percent + TransMatrix4_diag_percent

# Write this out to your working directory
write.table(SymMatrix4_all_percent,file="Output_Matrix_Context4_Allspeakers_percent.txt",sep="\t",quote=FALSE)

```

Context 4: Create DIS-similarity matrix by percentages with all speakers combined (symmetrical)

```{r}
nRow = nrow(Dataset4)

AllCodes = as.character(unique(Lookup4$ConditionContext))
AllCodes = sort(AllCodes)

OutputMatrix4 = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

for(EachRow in 1:nRow){ # EachRow=1

CurrentRow=Dataset4[EachRow,]

Version=as.character(CurrentRow[,"Version"])
Slide=CurrentRow[,"Slide"]
Group=as.character(CurrentRow[,"Group"])
Tokens=as.character(CurrentRow[,"Tokens"])
SplitTokens = as.integer(strsplit(Tokens,split=",")[[1]])

nTokens=length(SplitTokens)
Recoded = rep(NA,times=nTokens)
for(EachToken in 1:nTokens){ # EachToken=1

CurrentToken=SplitTokens[EachToken]

if(Version=="A"){ColumnName="ASlide"}else{ColumnName="BSlide"}

Target= Lookup4[,ColumnName]==Slide & 
        Lookup4$IconNumber==CurrentToken

Recoded[EachToken] <- as.character(Lookup4[Target,"ConditionContext"])

} # End 'EachToken' loop

Combinations = t(combn(Recoded,m=2))
Columns=Combinations[,1]
Rows=Combinations[,2]

nCombinations = nrow(Combinations)
for(EachCombination in 1:nCombinations){ # EachCombination=1
Sorted = sort(c(Columns[EachCombination],Rows[EachCombination]))
ThisRow=Sorted[1]
ThisColumn=Sorted[2]

OutputMatrix4[ThisRow,ThisColumn] <- OutputMatrix4[ThisRow,ThisColumn] + 1
} # End 'each combination' row

} # End 'each row' loop

# Transpose matrix so numbers are in bottom right
TransMatrix4 <- t(OutputMatrix4)

# Get values along diagonal (i.e. condition with itself)
TransMatrix4_diag <- diag(diag(TransMatrix4))

# Set diagonals to 0 for original matrix so the numbers aren't doubled when the matrices are added
diag(TransMatrix4) = 0

# Get number of participants and speakers
NbParticipants4 = length(unique(Dataset4$Subject))
NbSpeakers4 = length(unique(Lookup4$Speaker))

# Divide diagonal by number of (participants x number of speakers x (number of speakers - 1))/2 to get percentages (because stimuli can't be paired with themselves, so need to remove the number of diagonal cells, and divided by 2 because cells are identical across the diagonal)
TransMatrix4_diag_percent <- TransMatrix4_diag/((NbParticipants4*NbSpeakers4*(NbSpeakers4-1))/2)

# Add original matrices together
SymMatrix4_all <- OutputMatrix4 + TransMatrix4

# Divide resulting matrix by number of participants x (number of speakers x number of speakers) (i.e. the number of cells being combined) to get percentages
SymMatrix4_all_percent <- SymMatrix4_all/(NbParticipants4*NbSpeakers4*NbSpeakers4)

# Set diagonals to 0 for matrix
diag(SymMatrix4_all_percent) = 0

# Replace diagonal with correct numbers
SymMatrix4_all_percent <- SymMatrix4_all_percent + TransMatrix4_diag_percent

# Make dissimilarity matrix
DisSymMatrix4_all_percent <- 1-SymMatrix4_all_percent

# Write this out to your working directory
write.table(DisSymMatrix4_all_percent,file="Output_Matrix_Context4_Allspeakers_percent_dis.txt",sep="\t",quote=FALSE)

```


***Combined Contexts***


Combined Contexts: Create similarity matrix by counts with all contexts combined (symmetrical)
NOTE: Must have run code blocks for creating similarity matrices by counts for individual contexts first
```{r}
# Create a column for Condition + Speaker codes
Lookup1 = Lookup1 %>%
  unite ("CombinedCode", c(Condition,Speaker), remove = FALSE)

# Make these codes the names for the rows and columns
AllCodes = as.character(Lookup1$CombinedCode)
AllCodes = sort(AllCodes)

CombOutputMatrix = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

# Add all contexts together
CombOutputMatrix = CombOutputMatrix + SymMatrix1 + SymMatrix2
CombOutputMatrix = if(exists("SymMatrix3")) {CombOutputMatrix + SymMatrix3} else {CombOutputMatrix}
CombOutputMatrix = if(exists("SymMatrix4")) {CombOutputMatrix + SymMatrix4} else {CombOutputMatrix}

# Write this out to your working directory
write.table(CombOutputMatrix,file="Output_Matrix_AllContexts_counts.txt",sep="\t",quote=FALSE)
```

Combined Contexts: Create similarity matrix by percentages with all contexts combined (symmetrical)
NOTE: Must have run code blocks for creating similarity matrices by percentages for individual contexts first
```{r}
# Create a column for Condition + Speaker codes
Lookup1 = Lookup1 %>%
  unite ("CombinedCode", c(Condition,Speaker), remove = FALSE)

# Make these codes the names for the rows and columns
AllCodes = as.character(Lookup1$CombinedCode)
AllCodes = sort(AllCodes)

CombOutputMatrix = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

# Add all contexts together
CombOutputMatrix = CombOutputMatrix + SymMatrix1 + SymMatrix2
CombOutputMatrix = if(exists("SymMatrix3")) {CombOutputMatrix + SymMatrix3} else {CombOutputMatrix}
CombOutputMatrix = if(exists("SymMatrix4")) {CombOutputMatrix + SymMatrix4} else {CombOutputMatrix}

# Divide by number of participants in each context totaled to get percentage
NbParticipants3 = if(exists("NbParticipants3")) {NbParticipants3} else {0}
NbParticipants4 = if(exists("NbParticipants4")) {NbParticipants4} else {0}
CombOutputMatrix_percent = CombOutputMatrix/(NbParticipants1 + NbParticipants2 + NbParticipants3 + NbParticipants4)

# Set diagonal to 1 (stimuli are maximally similar to themselves)
diag(CombOutputMatrix_percent) = 1

# Write this out to your working directory
write.table(CombOutputMatrix_percent,file="Output_Matrix_AllContexts_percent.txt",sep="\t",quote=FALSE)

```

Combined Contexts: Create DIS-similarity matrix by percentages with all contexts combined (symmetrical) (For MDS in SPSS)
NOTE: Must have run code blocks for creating similarity matrices by percentages for individual contexts first
```{r}
# Create a column for Condition + Speaker codes
Lookup1 = Lookup1 %>%
  unite ("CombinedCode", c(Condition,Speaker), remove = FALSE)

# Make these codes the names for the rows and columns
AllCodes = as.character(Lookup1$CombinedCode)
AllCodes = sort(AllCodes)

CombOutputMatrix = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

# Add all contexts together
CombOutputMatrix = CombOutputMatrix + SymMatrix1 + SymMatrix2
CombOutputMatrix = if(exists("SymMatrix3")) {CombOutputMatrix + SymMatrix3} else {CombOutputMatrix}
CombOutputMatrix = if(exists("SymMatrix4")) {CombOutputMatrix + SymMatrix4} else {CombOutputMatrix}

# Divide by number of participants in each context totaled to get percentage
NbParticipants3 = if(exists("NbParticipants3")) {NbParticipants3} else {0}
NbParticipants4 = if(exists("NbParticipants4")) {NbParticipants4} else {0}
CombOutputMatrix_percent = CombOutputMatrix/(NbParticipants1 + NbParticipants2 + NbParticipants3 + NbParticipants4)

# Make dissimilarity matrix
DisCombOutputMatrix_percent = 1 - CombOutputMatrix_percent

# Set diagonal to 0 (stimuli are minimally dissimilar to themselves)
diag(DisCombOutputMatrix_percent) = 0

# Write this out to your working directory
write.table(DisCombOutputMatrix_percent,file="Output_Matrix_AllContexts_percent_dis.txt",sep="\t",quote=FALSE)

```


***Combined Contexts and Speakers***


Combined Contexts and Speakers: Create similarity matrix by counts with all contexts and speakers combined (symmetrical)
NOTE: Must have run code blocks for creating similarity matrices by counts for individual contexts with all speakers combined first
```{r}
# Make conditions the names for the rows and columns
AllCodes = as.character(unique(Lookup1$Condition))
AllCodes = sort(AllCodes)

CombOutputMatrix_all = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

# Add all contexts together
CombOutputMatrix_all = CombOutputMatrix_all + SymMatrix1_all + SymMatrix2_all
CombOutputMatrix_all = if(exists("SymMatrix3_all")) {CombOutputMatrix_all + SymMatrix3_all} else {CombOutputMatrix_all}
CombOutputMatrix_all = if(exists("SymMatrix4_all")) {CombOutputMatrix_all + SymMatrix4_all} else {CombOutputMatrix_all}

# Write this out to your working directory
write.table(CombOutputMatrix_all,file="Output_Matrix_AllContexts_AllSpeakers_counts.txt",sep="\t",quote=FALSE)
```

Combined Contexts and Speakers: Create similarity matrix by percentages with all contexts and speakers combined (symmetrical)
NOTE: Must have run code blocks for creating similarity matrices by percentages for individual contexts with all speakers combined first
```{r}
# Make conditions the names for the rows and columns
AllCodes = as.character(unique(Lookup1$Condition))
AllCodes = sort(AllCodes)

CombOutputMatrix_all = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

# Add all contexts together
CombOutputMatrix_all = CombOutputMatrix_all + SymMatrix1_all + SymMatrix2_all
CombOutputMatrix_all = if(exists("SymMatrix3_all")) {CombOutputMatrix_all + SymMatrix3_all} else {CombOutputMatrix_all}
CombOutputMatrix_all = if(exists("SymMatrix4_all")) {CombOutputMatrix_all + SymMatrix4_all} else {CombOutputMatrix_all}

# Get values along diagonal (i.e. condition with itself)
CombOutputMatrix_all_diag <- diag(diag(CombOutputMatrix_all))

# Divide diagonal by number of participants x number of speakers x (number of speakers - 1)/2 to get percentages (because stimuli can't be paired with themselves, so need to remove the number of diagonal cells, divide by 2 because numbers are symmetrical across diagonal) (assumes same number of speakers across all contexts)
NbParticipants3 = if(exists("NbParticipants3")) {NbParticipants3} else {0}
NbParticipants4 = if(exists("NbParticipants4")) {NbParticipants4} else {0}
CombOutputMatrix_all_diag_percent <- CombOutputMatrix_all_diag/(((NbParticipants1 + NbParticipants2 + NbParticipants3 + NbParticipants4) * NbSpeakers1 * (NbSpeakers1-1))/2)

# Divide original combined matrix by number of participants x (number of speakers x number of speakers) (i.e. the number of cells being combined) to get percentages (assumes same number of speakers across all contexts)
CombOutputMatrix_percent_all = CombOutputMatrix_all/((NbParticipants1 + NbParticipants2 + NbParticipants3 + NbParticipants4) *NbSpeakers1* NbSpeakers1)

# Set diagonals to 0 for matrix
diag(CombOutputMatrix_percent_all) = 0

# Replace diagonal with correct numbers
CombOutputMatrix_percent_all <- CombOutputMatrix_percent_all + CombOutputMatrix_all_diag_percent

# Write this out to your working directory
write.table(CombOutputMatrix_percent_all,file="Output_Matrix_AllContexts_AllSpeakers_percent.txt",sep="\t",quote=FALSE)

```

Combined Contexts and Speakers: Create DIS-similarity matrix by percentages with all contexts and speakers combined (symmetrical)
NOTE: Must have run code blocks for creating similarity matrices by percentages for individual contexts with all speakers combined first
```{r}
# Make conditions the names for the rows and columns
AllCodes = as.character(unique(Lookup1$Condition))
AllCodes = sort(AllCodes)

CombOutputMatrix_all = matrix(0, nrow=length(AllCodes), ncol=length(AllCodes), dimnames=list(AllCodes,AllCodes))

# Add all contexts together
CombOutputMatrix_all = CombOutputMatrix_all + SymMatrix1_all + SymMatrix2_all
CombOutputMatrix_all = if(exists("SymMatrix3_all")) {CombOutputMatrix_all + SymMatrix3_all} else {CombOutputMatrix_all}
CombOutputMatrix_all = if(exists("SymMatrix4_all")) {CombOutputMatrix_all + SymMatrix4_all} else {CombOutputMatrix_all}

# Get values along diagonal (i.e. condition with itself)
CombOutputMatrix_all_diag <- diag(diag(CombOutputMatrix_all))

# Divide diagonal by number of participants x number of speakers x (number of speakers - 1)/2 to get percentages (because stimuli can't be paired with themselves, so need to remove the number of diagonal cells, divide by 2 because numbers are symmetrical across diagonal) (assumes same number of speakers across all contexts)
NbParticipants3 = if(exists("NbParticipants3")) {NbParticipants3} else {0}
NbParticipants4 = if(exists("NbParticipants4")) {NbParticipants4} else {0}
CombOutputMatrix_all_diag_percent <- CombOutputMatrix_all_diag/(((NbParticipants1 + NbParticipants2 + NbParticipants3 + NbParticipants4) * NbSpeakers1 * (NbSpeakers1-1))/2)

# Divide original combined matrix by number of participants x (number of speakers x number of speakers) (i.e. the number of cells being combined) to get percentages (assumes same number of speakers across all contexts)
CombOutputMatrix_percent_all = CombOutputMatrix_all/((NbParticipants1 + NbParticipants2 + NbParticipants3 + NbParticipants4) *NbSpeakers1*NbSpeakers1)

# Set diagonals to 0 for matrix
diag(CombOutputMatrix_percent_all) = 0

# Replace diagonal with correct numbers
CombOutputMatrix_percent_all <- CombOutputMatrix_percent_all + CombOutputMatrix_all_diag_percent

# Make dissimilarity matrix
DisCombOutputMatrix_percent_all = 1 - CombOutputMatrix_percent_all

# Write this out to your working directory
write.table(DisCombOutputMatrix_percent_all,file="Output_Matrix_AllContexts_AllSpeakers_percent_dis.txt",sep="\t",quote=FALSE)

```
