--- title: "MDS Analysis" author: "Danielle Daidone" date: "3/31/26" output: html_document --- This script runs multidimensional scaling (MDS) analyses on matrix outputs from create_FC_similarity_matrices.Rmd. There are chunks for running MDS on all contexts combined as well as up to 4 contexts separately. It outputs 1,2,3,4 and 5 dimensional solutions as text files and produces a stress plot to help you decide on the most appropriate number of dimensions. It also produces basic plots of the solutions, but note that the solutions are unrotated and thus may not be easily interpretable. This script also computes Euclidean distances between points for 2 and 3 dimensional solutions (i.e. the number of dimensions most likely to be interpretable) with all speakers combined so that MDS distances between sounds can be compared to discrimination accuracy. It outputs these distance matrices as csv files. The smacof package automatically calculates distances as part of the output for every matrix, so you can easily get distances for any other MDS solution as desired. ***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 See documentation: smacof (for MDS): https://rdrr.io/cran/smacof/man/smacofSym.html ```{r} if(!require(rmarkdown)){install.packages("rmarkdown")} library(rmarkdown) if(!require(smacof)){install.packages("smacof")} library(smacof) ``` Set working directory - *Change to match your filepath* ```{r setup, include=FALSE} knitr::opts_knit$set(root.dir = "C:/Users/ddaid/OneDrive - UNC-Wilmington/CV/Website/FC analysis info/Output Matrices") ``` **SECTION 1: CALCULATE MDS SOLUTIONS** ***PART 1: GET MDS SOLUTIONS FOR 1-5 DIMENSIONS FOR CONTEXTS COMBINED*** Open dataset - Contexts combined *dis*similarity matrix ```{r} # Note that the dissimilarity text files are tab delimited files with headers # If you've changed the default file names from the previous script, you'll need to adjust this FCmatrixAll=read.table("Output_Matrix_AllContexts_percent_dis.txt",sep="\t",header=TRUE) ``` Contexts combined: Look at the first few rows of the dataset ```{r} head(FCmatrixAll) ``` Contexts combined: Run ordinal multi-dimensional scaling (MDS) - 1 dimensional solution ```{r} # ndim is the number of dimensions MDS1Dim_all = mds(FCmatrixAll, ndim = 1, type = "ordinal") # view Kruskal stress MDS1Dim_all # get dim scores dim1scores_all = MDS1Dim_all$conf # write dim scores out to a text file in your working directory write.table(dim1scores_all,file="MDS_1D_AllContexts.txt",sep="\t",quote=FALSE) ``` Contexts combined: Run ordinal multi-dimensional scaling (MDS) - 2 dimensional solution ```{r} # ndim is the number of dimensions MDS2Dim_all = mds(FCmatrixAll, ndim = 2, type = "ordinal") # view Kruskal stress MDS2Dim_all # get dim scores dim2scores_all = MDS2Dim_all$conf # write dim scores out to a text file in your working directory write.table(dim2scores_all,file="MDS_2D_AllContexts.txt",sep="\t",quote=FALSE) #plot dim 1 by dim 2 plot(MDS2Dim_all, plot.type = "confplot", plot.dim = c(1,2)) ``` Contexts combined: Run ordinal multi-dimensional scaling (MDS) - 3 dimensional solution ```{r} # ndim is the number of dimensions MDS3Dim_all = mds(FCmatrixAll, ndim = 3, type = "ordinal") # view Kruskal stress MDS3Dim_all # get dim scores dim3scores_all = MDS3Dim_all$conf # write dim scores out to a text file in your working directory write.table(dim3scores_all,file="MDS_3D_AllContexts.txt",sep="\t",quote=FALSE) #plot dim 1 by dim 2 plot(MDS3Dim_all, plot.type = "confplot", plot.dim = c(1,2)) #plot dim 1 by dim 3 (as if looking into previous plot from the top) plot(MDS3Dim_all, plot.type = "confplot", plot.dim = c(1,3)) ``` Contexts combined: Run ordinal multi-dimensional scaling (MDS) - 4 dimensional solution ```{r} # ndim is the number of dimensions MDS4Dim_all = mds(FCmatrixAll, ndim = 4, type = "ordinal") # view Kruskal stress MDS4Dim_all # get dim scores dim4scores_all = MDS4Dim_all$conf # write dim scores out to a text file in your working directory write.table(dim4scores_all,file="MDS_4D_AllContexts.txt",sep="\t",quote=FALSE) #plot dim 1 by dim 2 plot(MDS4Dim_all, plot.type = "confplot", plot.dim = c(1,2)) #plot dim 1 by dim 4 (as if looking into previous plot from the top) plot(MDS4Dim_all, plot.type = "confplot", plot.dim = c(1,3)) ``` Contexts combined: Run ordinal multi-dimensional scaling (MDS) - 5 dimensional solution ```{r} # ndim is the number of dimensions MDS5Dim_all = mds(FCmatrixAll, ndim = 5, type = "ordinal") # view Kruskal stress MDS5Dim_all # get dim scores dim5scores_all = MDS5Dim_all$conf # write dim scores out to a text file in your working directory write.table(dim5scores_all,file="MDS_5D_AllContexts.txt",sep="\t",quote=FALSE) #plot dim 1 by dim 2 plot(MDS5Dim_all, plot.type = "confplot", plot.dim = c(1,2)) #plot dim 1 by dim 5 (as if looking into previous plot from the top) plot(MDS5Dim_all, plot.type = "confplot", plot.dim = c(1,3)) ``` Contexts combined: Create stress plot ```{r} stressvalues = c(MDS1Dim_all$stress,MDS2Dim_all$stress,MDS3Dim_all$stress,MDS4Dim_all$stress,MDS5Dim_all$stress) dimensions = c(1,2,3,4,5) plot(dimensions,stressvalues) ``` Contexts combined: Create plot of residual sum of squares ```{r} rssvalues = c(MDS1Dim_all$rss,MDS2Dim_all$rss,MDS3Dim_all$rss,MDS4Dim_all$rss,MDS5Dim_all$rss) dimensions = c(1,2,3,4,5) plot(dimensions,rssvalues) ``` ***PART 2: GET MDS SOLUTIONS FOR 1-5 DIMENSIONS FOR CONTEXT 1*** Open dataset - Context 1 *dis*similarity matrix ```{r} # Note that the dissimilarity text files are tab delimited files with headers # If you've changed the default file names from the previous script, you'll need to adjust this FCmatrixcontext1=read.table("Output_Matrix_Context1_percent_dis.txt",sep="\t",header=TRUE) ``` Context 1: Look at the first few rows of the dataset ```{r} head(FCmatrixcontext1) ``` Context 1: Run ordinal multi-dimensional scaling (MDS) - 1 dimensional solution ```{r} # ndim is the number of dimensions MDS1Dim_context1 = mds(FCmatrixcontext1, ndim = 1, type = "ordinal") # view Kruskal stress MDS1Dim_context1 # get dim scores dim1scores_context1 = MDS1Dim_context1$conf # write dim scores out to a text file in your working directory write.table(dim1scores_context1,file="MDS_1D_Context1.txt",sep="\t",quote=FALSE) ``` Context 1: Run ordinal multi-dimensional scaling (MDS) - 2 dimensional solution ```{r} # ndim is the number of dimensions MDS2Dim_context1 = mds(FCmatrixcontext1, ndim = 2, type = "ordinal") # view Kruskal stress MDS2Dim_context1 # get dim scores dim2scores_context1 = MDS2Dim_context1$conf # write dim scores out to a text file in your working directory write.table(dim2scores_context1,file="MDS_2D_Context1.txt",sep="\t",quote=FALSE) #plot dim 1 by dim 2 plot(MDS2Dim_context1, plot.type = "confplot", plot.dim = c(1,2)) ``` Contexts combined: Run ordinal multi-dimensional scaling (MDS) - 3 dimensional solution ```{r} # ndim is the number of dimensions MDS3Dim_context1 = mds(FCmatrixcontext1, ndim = 3, type = "ordinal") # view Kruskal stress MDS3Dim_context1 # get dim scores dim3scores_context1 = MDS3Dim_context1$conf # write dim scores out to a text file in your working directory write.table(dim3scores_context1,file="MDS_3D_Context1.txt",sep="\t",quote=FALSE) #plot dim 1 by dim 2 plot(MDS3Dim_context1, plot.type = "confplot", plot.dim = c(1,2)) #plot dim 1 by dim 3 (as if looking into previous plot from the top) plot(MDS3Dim_context1, plot.type = "confplot", plot.dim = c(1,3)) ``` Context 1: Run ordinal multi-dimensional scaling (MDS) - 4 dimensional solution ```{r} # ndim is the number of dimensions MDS4Dim_context1 = mds(FCmatrixcontext1, ndim = 4, type = "ordinal") # view Kruskal stress MDS4Dim_context1 # get dim scores dim4scores_context1 = MDS4Dim_context1$conf # write dim scores out to a text file in your working directory write.table(dim4scores_context1,file="MDS_4D_Context1.txt",sep="\t",quote=FALSE) #plot dim 1 by dim 2 plot(MDS4Dim_context1, plot.type = "confplot", plot.dim = c(1,2)) #plot dim 1 by dim 4 (as if looking into previous plot from the top) plot(MDS4Dim_context1, plot.type = "confplot", plot.dim = c(1,3)) ``` Context 1: Run ordinal multi-dimensional scaling (MDS) - 5 dimensional solution ```{r} # ndim is the number of dimensions MDS5Dim_context1 = mds(FCmatrixcontext1, ndim = 5, type = "ordinal") # view Kruskal stress MDS5Dim_context1 # get dim scores dim5scores_context1 = MDS5Dim_context1$conf # write dim scores out to a text file in your working directory write.table(dim5scores_context1,file="MDS_5D_Context1.txt",sep="\t",quote=FALSE) #plot dim 1 by dim 2 plot(MDS5Dim_context1, plot.type = "confplot", plot.dim = c(1,2)) #plot dim 1 by dim 5 (as if looking into previous plot from the top) plot(MDS5Dim_context1, plot.type = "confplot", plot.dim = c(1,3)) ``` Context 1: Create stress plot ```{r} stressvalues = c(MDS1Dim_context1$stress,MDS2Dim_context1$stress,MDS3Dim_context1$stress,MDS4Dim_context1$stress,MDS5Dim_context1$stress) dimensions = c(1,2,3,4,5) plot(dimensions,stressvalues) ``` ***PART 3: GET MDS SOLUTIONS FOR 1-5 DIMENSIONS FOR CONTEXT 2*** Open dataset - Context 2 *dis*similarity matrix ```{r} # Note that the dissimilarity text files are tab delimited files with headers # If you've changed the default file names from the previous script, you'll need to adjust this FCmatrixcontext2=read.table("Output_Matrix_Context2_percent_dis.txt",sep="\t",header=TRUE) ``` Context 2: Look at the first few rows of the dataset ```{r} head(FCmatrixcontext2) ``` Context 2: Run ordinal multi-dimensional scaling (MDS) - 1 dimensional solution ```{r} # ndim is the number of dimensions MDS1Dim_context2 = mds(FCmatrixcontext2, ndim = 1, type = "ordinal") # view Kruskal stress MDS1Dim_context2 # get dim scores dim1scores_context2 = MDS1Dim_context2$conf # write dim scores out to a text file in your working directory write.table(dim1scores_context2,file="MDS_1D_Context2.txt",sep="\t",quote=FALSE) ``` Context 2: Run ordinal multi-dimensional scaling (MDS) - 2 dimensional solution ```{r} # ndim is the number of dimensions MDS2Dim_context2 = mds(FCmatrixcontext2, ndim = 2, type = "ordinal") # view Kruskal stress MDS2Dim_context2 # get dim scores dim2scores_context2 = MDS2Dim_context2$conf # write dim scores out to a text file in your working directory write.table(dim2scores_context2,file="MDS_2D_Context2.txt",sep="\t",quote=FALSE) #plot dim 1 by dim 2 plot(MDS2Dim_context2, plot.type = "confplot", plot.dim = c(1,2)) ``` Contexts combined: Run ordinal multi-dimensional scaling (MDS) - 3 dimensional solution ```{r} # ndim is the number of dimensions MDS3Dim_context2 = mds(FCmatrixcontext2, ndim = 3, type = "ordinal") # view Kruskal stress MDS3Dim_context2 # get dim scores dim3scores_context2 = MDS3Dim_context2$conf # write dim scores out to a text file in your working directory write.table(dim3scores_context2,file="MDS_3D_Context2.txt",sep="\t",quote=FALSE) #plot dim 1 by dim 2 plot(MDS3Dim_context2, plot.type = "confplot", plot.dim = c(1,2)) #plot dim 1 by dim 3 (as if looking into previous plot from the top) plot(MDS3Dim_context2, plot.type = "confplot", plot.dim = c(1,3)) ``` Context 2: Run ordinal multi-dimensional scaling (MDS) - 4 dimensional solution ```{r} # ndim is the number of dimensions MDS4Dim_context2 = mds(FCmatrixcontext2, ndim = 4, type = "ordinal") # view Kruskal stress MDS4Dim_context2 # get dim scores dim4scores_context2 = MDS4Dim_context2$conf # write dim scores out to a text file in your working directory write.table(dim4scores_context2,file="MDS_4D_Context2.txt",sep="\t",quote=FALSE) #plot dim 1 by dim 2 plot(MDS4Dim_context2, plot.type = "confplot", plot.dim = c(1,2)) #plot dim 1 by dim 4 (as if looking into previous plot from the top) plot(MDS4Dim_context2, plot.type = "confplot", plot.dim = c(1,3)) ``` Context 2: Run ordinal multi-dimensional scaling (MDS) - 5 dimensional solution ```{r} # ndim is the number of dimensions MDS5Dim_context2 = mds(FCmatrixcontext2, ndim = 5, type = "ordinal") # view Kruskal stress MDS5Dim_context2 # get dim scores dim5scores_context2 = MDS5Dim_context2$conf # write dim scores out to a text file in your working directory write.table(dim5scores_context2,file="MDS_5D_Context2.txt",sep="\t",quote=FALSE) #plot dim 1 by dim 2 plot(MDS5Dim_context2, plot.type = "confplot", plot.dim = c(1,2)) #plot dim 1 by dim 5 (as if looking into previous plot from the top) plot(MDS5Dim_context2, plot.type = "confplot", plot.dim = c(1,3)) ``` Context 2: Create stress plot ```{r} stressvalues = c(MDS1Dim_context2$stress,MDS2Dim_context2$stress,MDS3Dim_context2$stress,MDS4Dim_context2$stress,MDS5Dim_context2$stress) dimensions = c(1,2,3,4,5) plot(dimensions,stressvalues) ``` ***PART 4: GET MDS SOLUTIONS FOR 1-5 DIMENSIONS FOR CONTEXT 3*** Open dataset - Context 3 *dis*similarity matrix ```{r} # Note that the dissimilarity text files are tab delimited files with headers # If you've changed the default file names from the previous script, you'll need to adjust this FCmatrixcontext3=read.table("Output_Matrix_Context3_percent_dis.txt",sep="\t",header=TRUE) ``` Context 3: Look at the first few rows of the dataset ```{r} head(FCmatrixcontext3) ``` Context 3: Run ordinal multi-dimensional scaling (MDS) - 1 dimensional solution ```{r} # ndim is the number of dimensions MDS1Dim_context3 = mds(FCmatrixcontext3, ndim = 1, type = "ordinal") # view Kruskal stress MDS1Dim_context3 # get dim scores dim1scores_context3 = MDS1Dim_context3$conf # write dim scores out to a text file in your working directory write.table(dim1scores_context3,file="MDS_1D_Context3.txt",sep="\t",quote=FALSE) ``` Context 3: Run ordinal multi-dimensional scaling (MDS) - 2 dimensional solution ```{r} # ndim is the number of dimensions MDS2Dim_context3 = mds(FCmatrixcontext3, ndim = 2, type = "ordinal") # view Kruskal stress MDS2Dim_context3 # get dim scores dim2scores_context3 = MDS2Dim_context3$conf # write dim scores out to a text file in your working directory write.table(dim2scores_context3,file="MDS_2D_Context3.txt",sep="\t",quote=FALSE) #plot dim 1 by dim 2 plot(MDS2Dim_context3, plot.type = "confplot", plot.dim = c(1,2)) ``` Contexts combined: Run ordinal multi-dimensional scaling (MDS) - 3 dimensional solution ```{r} # ndim is the number of dimensions MDS3Dim_context3 = mds(FCmatrixcontext3, ndim = 3, type = "ordinal") # view Kruskal stress MDS3Dim_context3 # get dim scores dim3scores_context3 = MDS3Dim_context3$conf # write dim scores out to a text file in your working directory write.table(dim3scores_context3,file="MDS_3D_Context3.txt",sep="\t",quote=FALSE) #plot dim 1 by dim 2 plot(MDS3Dim_context3, plot.type = "confplot", plot.dim = c(1,2)) #plot dim 1 by dim 3 (as if looking into previous plot from the top) plot(MDS3Dim_context3, plot.type = "confplot", plot.dim = c(1,3)) ``` Context 3: Run ordinal multi-dimensional scaling (MDS) - 4 dimensional solution ```{r} # ndim is the number of dimensions MDS4Dim_context3 = mds(FCmatrixcontext3, ndim = 4, type = "ordinal") # view Kruskal stress MDS4Dim_context3 # get dim scores dim4scores_context3 = MDS4Dim_context3$conf # write dim scores out to a text file in your working directory write.table(dim4scores_context3,file="MDS_4D_Context3.txt",sep="\t",quote=FALSE) #plot dim 1 by dim 2 plot(MDS4Dim_context3, plot.type = "confplot", plot.dim = c(1,2)) #plot dim 1 by dim 4 (as if looking into previous plot from the top) plot(MDS4Dim_context3, plot.type = "confplot", plot.dim = c(1,3)) ``` Context 3: Run ordinal multi-dimensional scaling (MDS) - 5 dimensional solution ```{r} # ndim is the number of dimensions MDS5Dim_context3 = mds(FCmatrixcontext3, ndim = 5, type = "ordinal") # view Kruskal stress MDS5Dim_context3 # get dim scores dim5scores_context3 = MDS5Dim_context3$conf # write dim scores out to a text file in your working directory write.table(dim5scores_context3,file="MDS_5D_Context3.txt",sep="\t",quote=FALSE) #plot dim 1 by dim 2 plot(MDS5Dim_context3, plot.type = "confplot", plot.dim = c(1,2)) #plot dim 1 by dim 5 (as if looking into previous plot from the top) plot(MDS5Dim_context3, plot.type = "confplot", plot.dim = c(1,3)) ``` Context 3: Create stress plot ```{r} stressvalues = c(MDS1Dim_context3$stress,MDS2Dim_context3$stress,MDS3Dim_context3$stress,MDS4Dim_context3$stress,MDS5Dim_context3$stress) dimensions = c(1,2,3,4,5) plot(dimensions,stressvalues) ``` ***PART 5: GET MDS SOLUTIONS FOR 1-5 DIMENSIONS FOR CONTEXT 4*** Open dataset - Context 4 *dis*similarity matrix ```{r} # Note that the dissimilarity text files are tab delimited files with headers # If you've changed the default file names from the previous script, you'll need to adjust this FCmatrixcontext4=read.table("Output_Matrix_Context4_percent_dis.txt",sep="\t",header=TRUE) ``` Context 4: Look at the first few rows of the dataset ```{r} head(FCmatrixcontext4) ``` Context 4: Run ordinal multi-dimensional scaling (MDS) - 1 dimensional solution ```{r} # ndim is the number of dimensions MDS1Dim_context4 = mds(FCmatrixcontext4, ndim = 1, type = "ordinal") # view Kruskal stress MDS1Dim_context4 # get dim scores dim1scores_context4 = MDS1Dim_context4$conf # write dim scores out to a text file in your working directory write.table(dim1scores_context4,file="MDS_1D_Context4.txt",sep="\t",quote=FALSE) ``` Context 4: Run ordinal multi-dimensional scaling (MDS) - 2 dimensional solution ```{r} # ndim is the number of dimensions MDS2Dim_context4 = mds(FCmatrixcontext4, ndim = 2, type = "ordinal") # view Kruskal stress MDS2Dim_context4 # get dim scores dim2scores_context4 = MDS2Dim_context4$conf # write dim scores out to a text file in your working directory write.table(dim2scores_context4,file="MDS_2D_Context4.txt",sep="\t",quote=FALSE) #plot dim 1 by dim 2 plot(MDS2Dim_context4, plot.type = "confplot", plot.dim = c(1,2)) ``` Context 4: Run ordinal multi-dimensional scaling (MDS) - 3 dimensional solution ```{r} # ndim is the number of dimensions MDS3Dim_context4 = mds(FCmatrixcontext4, ndim = 3, type = "ordinal") # view Kruskal stress MDS3Dim_context4 # get dim scores dim3scores_context4 = MDS3Dim_context4$conf # write dim scores out to a text file in your working directory write.table(dim3scores_context4,file="MDS_3D_Context4.txt",sep="\t",quote=FALSE) #plot dim 1 by dim 2 plot(MDS3Dim_context4, plot.type = "confplot", plot.dim = c(1,2)) #plot dim 1 by dim 3 (as if looking into previous plot from the top) plot(MDS3Dim_context4, plot.type = "confplot", plot.dim = c(1,3)) ``` Context 4: Run ordinal multi-dimensional scaling (MDS) - 4 dimensional solution ```{r} # ndim is the number of dimensions MDS4Dim_context4 = mds(FCmatrixcontext4, ndim = 4, type = "ordinal") # view Kruskal stress MDS4Dim_context4 # get dim scores dim4scores_context4 = MDS4Dim_context4$conf # write dim scores out to a text file in your working directory write.table(dim4scores_context4,file="MDS_4D_Context4.txt",sep="\t",quote=FALSE) #plot dim 1 by dim 2 plot(MDS4Dim_context4, plot.type = "confplot", plot.dim = c(1,2)) #plot dim 1 by dim 4 (as if looking into previous plot from the top) plot(MDS4Dim_context4, plot.type = "confplot", plot.dim = c(1,3)) ``` Context 4: Run ordinal multi-dimensional scaling (MDS) - 5 dimensional solution ```{r} # ndim is the number of dimensions MDS5Dim_context4 = mds(FCmatrixcontext4, ndim = 5, type = "ordinal") # view Kruskal stress MDS5Dim_context4 # get dim scores dim5scores_context4 = MDS5Dim_context4$conf # write dim scores out to a text file in your working directory write.table(dim5scores_context4,file="MDS_5D_Context4.txt",sep="\t",quote=FALSE) #plot dim 1 by dim 2 plot(MDS5Dim_context4, plot.type = "confplot", plot.dim = c(1,2)) #plot dim 1 by dim 5 (as if looking into previous plot from the top) plot(MDS5Dim_context4, plot.type = "confplot", plot.dim = c(1,3)) ``` Context 4: Create stress plot ```{r} stressvalues = c(MDS1Dim_context4$stress,MDS2Dim_context4$stress,MDS3Dim_context4$stress,MDS4Dim_context4$stress,MDS5Dim_context4$stress) dimensions = c(1,2,3,4,5) plot(dimensions,stressvalues) ``` **SECTION 2: CALCULATE EUCLIDEAN DISTANCES BETWEEN DIMENSION SCORES** Note that all speakers are combined in these analyses in order to get an average distance between categories. ***Part 1: CALCULATE EUCLIDEAN DISTANCES FOR 2D & 3D SOLUTIONS FOR CONTEXTS COMBINED*** Contexts Combined: Calculate Euclidean distances between dimension scores for 2-dimensional solution ```{r} # read in file for all contexts and all speakers combined FCmatrixAll_all = read.table("Output_Matrix_AllContexts_AllSpeakers_percent_dis.txt",sep="\t",header=TRUE) # run MDS analysis MDS2Dim_all_all = mds(FCmatrixAll_all, ndim = 2, type = "ordinal") # save distances calculated with mds() and convert to a symmetrical matrix for saving to file dist2D_all_all = as.matrix(MDS2Dim_all_all$confdist) # write distance matrix to a file in your working directory write.csv(dist2D_all_all, "Dist2D_AllContexts.csv", row.names = TRUE) ``` Contexts Combined: Calculate Euclidean distances between dimension scores for 3-dimensional solution ```{r} # read in file for all contexts and all speakers combined FCmatrixAll_all = read.table("Output_Matrix_AllContexts_AllSpeakers_percent_dis.txt",sep="\t",header=TRUE) # run MDS analysis MDS3Dim_all_all = mds(FCmatrixAll_all, ndim = 3, type = "ordinal") # save distances calculated with mds() and convert to a symmetrical matrix for saving to file dist3D_all_all = as.matrix(MDS2Dim_all_all$confdist) # write distance matrix to a file in your working directory write.csv(dist3D_all_all, "Dist3D_AllContexts.csv", row.names = TRUE) ``` ***Part 2: CALCULATE EUCLIDEAN DISTANCES FOR 2D & 3D SOLUTIONS FOR CONTEXT 1*** Context 1: Calculate Euclidean distances between dimension scores for 2-dimensional solution ```{r} # read in file for Context 1 and all speakers combined FCmatrixcontext1_all = read.table("Output_Matrix_Context1_AllSpeakers_percent_dis.txt",sep="\t",header=TRUE) # run MDS analysis MDS2Dim_context1_all = mds(FCmatrixcontext1_all, ndim = 2, type = "ordinal") # save distances calculated with mds() and convert to a symmetrical matrix for saving to file dist2D_context1_all = as.matrix(MDS2Dim_context1_all$confdist) # write distance matrix to a file in your working directory write.csv(dist2D_context1_all, "Dist2D_Context1.csv", row.names = TRUE) ``` Context 1: Calculate Euclidean distances between dimension scores for 3-dimensional solution ```{r} # read in file for Context 1 and all speakers combined FCmatrixcontext1_all = read.table("Output_Matrix_Context1_AllSpeakers_percent_dis.txt",sep="\t",header=TRUE) # run MDS analysis MDS3Dim_context1_all = mds(FCmatrixcontext1_all, ndim = 3, type = "ordinal") # save distances calculated with mds() and convert to a symmetrical matrix for saving to file dist3D_context1_all = as.matrix(MDS3Dim_context1_all$confdist) # write distance matrix to a file in your working directory write.csv(dist3D_context1_all, "Dist3D_Context1.csv", row.names = TRUE) ``` ***Part 3: CALCULATE EUCLIDEAN DISTANCES FOR 2D & 3D SOLUTIONS FOR CONTEXT 2*** Context 2: Calculate Euclidean distances between dimension scores for 2-dimensional solution ```{r} # read in file for Context 2 and all speakers combined FCmatrixcontext2_all = read.table("Output_Matrix_Context2_AllSpeakers_percent_dis.txt",sep="\t",header=TRUE) # run MDS analysis MDS2Dim_context2_all = mds(FCmatrixcontext2_all, ndim = 2, type = "ordinal") # save distances calculated with mds() and convert to a symmetrical matrix for saving to file dist2D_context2_all = as.matrix(MDS2Dim_context2_all$confdist) # write distance matrix to a file in your working directory write.csv(dist2D_context2_all, "Dist2D_Context2.csv", row.names = TRUE) ``` Context 2: Calculate Euclidean distances between dimension scores for 3-dimensional solution ```{r} # read in file for Context 2 and all speakers combined FCmatrixcontext2_all = read.table("Output_Matrix_Context2_AllSpeakers_percent_dis.txt",sep="\t",header=TRUE) # run MDS analysis MDS3Dim_context2_all = mds(FCmatrixcontext2_all, ndim = 3, type = "ordinal") # save distances calculated with mds() and convert to a symmetrical matrix for saving to file dist3D_context2_all = as.matrix(MDS3Dim_context2_all$confdist) # write distance matrix to a file in your working directory write.csv(dist3D_context2_all, "Dist3D_Context2.csv", row.names = TRUE) ``` ***Part 4: CALCULATE EUCLIDEAN DISTANCES FOR 2D & 3D SOLUTIONS FOR CONTEXT 3*** Context 3: Calculate Euclidean distances between dimension scores for 2-dimensional solution ```{r} # read in file for Context 3 and all speakers combined FCmatrixcontext3_all = read.table("Output_Matrix_Context3_AllSpeakers_percent_dis.txt",sep="\t",header=TRUE) # run MDS analysis MDS2Dim_context3_all = mds(FCmatrixcontext3_all, ndim = 2, type = "ordinal") # save distances calculated with mds() and convert to a symmetrical matrix for saving to file dist2D_context3_all = as.matrix(MDS2Dim_context3_all$confdist) # write distance matrix to a file in your working directory write.csv(dist2D_context3_all, "Dist2D_Context3.csv", row.names = TRUE) ``` Context 3: Calculate Euclidean distances between dimension scores for 3-dimensional solution ```{r} # read in file for Context 3 and all speakers combined FCmatrixcontext3_all = read.table("Output_Matrix_Context3_AllSpeakers_percent_dis.txt",sep="\t",header=TRUE) # run MDS analysis MDS3Dim_context3_all = mds(FCmatrixcontext3_all, ndim = 3, type = "ordinal") # save distances calculated with mds() and convert to a symmetrical matrix for saving to file dist3D_context3_all = as.matrix(MDS3Dim_context3_all$confdist) # write distance matrix to a file in your working directory write.csv(dist3D_context3_all, "Dist3D_Context3.csv", row.names = TRUE) ``` ***Part 5: CALCULATE EUCLIDEAN DISTANCES FOR 2D & 3D SOLUTIONS FOR CONTEXT 4*** Context 4: Calculate Euclidean distances between dimension scores for 2-dimensional solution ```{r} # read in file for Context 4 and all speakers combined FCmatrixcontext4_all = read.table("Output_Matrix_Context4_AllSpeakers_percent_dis.txt",sep="\t",header=TRUE) # run MDS analysis MDS2Dim_context4_all = mds(FCmatrixcontext4_all, ndim = 2, type = "ordinal") # save distances calculated with mds() and convert to a symmetrical matrix for saving to file dist2D_context4_all = as.matrix(MDS2Dim_context4_all$confdist) # write distance matrix to a file in your working directory write.csv(dist2D_context4_all, "Dist2D_Context4.csv", row.names = TRUE) ``` Context 4: Calculate Euclidean distances between dimension scores for 3-dimensional solution ```{r} # read in file for Context 4 and all speakers combined FCmatrixcontext4_all = read.table("Output_Matrix_Context4_AllSpeakers_percent_dis.txt",sep="\t",header=TRUE) # run MDS analysis MDS3Dim_context4_all = mds(FCmatrixcontext4_all, ndim = 3, type = "ordinal") # save distances calculated with mds() and convert to a symmetrical matrix for saving to file dist3D_context4_all = as.matrix(MDS3Dim_context4_all$confdist) # write distance matrix to a file in your working directory write.csv(dist3D_context4_all, "Dist3D_Context4.csv", row.names = TRUE) ```