--- title: "MDS Analysis" author: "Danielle Daidone" date: 8/3/23 output: html_document --- This script runs multidimensional scaling analyses on any matrix output by create_FC_similarity_matrices.Rmd. 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 plots of the solutions, but note that the solutions are unrotated and thus may not be easily interpretable. 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) ``` PART 1: OPEN AND CHECK DATASET Set working directory - Change to match your filepath ```{r setup, include=FALSE} knitr::opts_knit$set(root.dir = "C:/Users/Danielle/OneDrive - UNC-Wilmington/CV/Website/FC analysis info") ``` Open dataset - Change to match your desired *dis*similarity matrix ```{r} # Note that the dissimilarity text files are tab delimited files with headers FCmatrixAll=read.table("Output_Matrix_AllContexts_percent_dis.txt",sep="\t",header=TRUE) ``` Look at the first few rows of the dataset ```{r} head(FCmatrixAll) ``` Run ordinal multi-dimensional scaling (MDS) - 1 dimensional solution ```{r} # ndim is the number of dimensions MDS1Dim = mds(FCmatrixAll, ndim = 1, type = "ordinal") # view Kruskal stress MDS1Dim # get dim scores dimscores = MDS1Dim$conf # write dim scores out to a text file in your working directory write.table(dimscores,file="MDS_1D.txt",sep="\t",quote=FALSE) ``` Run ordinal multi-dimensional scaling (MDS) - 2 dimensional solution ```{r} # ndim is the number of dimensions MDS2Dim = mds(FCmatrixAll, ndim = 2, type = "ordinal") # view Kruskal stress MDS2Dim # get dim scores dimscores = MDS2Dim$conf # write dim scores out to a text file in your working directory write.table(dimscores,file="MDS_2D.txt",sep="\t",quote=FALSE) #plot plot(MDS2Dim, plot.type = "confplot", plot.dim = c(1,2)) ``` Run ordinal multi-dimensional scaling (MDS) - 3 dimensional solution ```{r} # ndim is the number of dimensions MDS3Dim = mds(FCmatrixAll, ndim = 3, type = "ordinal") # view Kruskal stress MDS3Dim # get dim scores dimscores = MDS3Dim$conf # write dim scores out to a text file in your working directory write.table(dimscores,file="MDS_3D.txt",sep="\t",quote=FALSE) #plot dim 1 by dim 2 plot(MDS3Dim, 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, plot.type = "confplot", plot.dim = c(1,3)) ``` Run ordinal multi-dimensional scaling (MDS) - 4 dimensional solution ```{r} # ndim is the number of dimensions MDS4Dim = mds(FCmatrixAll, ndim = 4, type = "ordinal") # view Kruskal stress MDS4Dim # get dim scores dimscores = MDS4Dim$conf # write dim scores out to a text file in your working directory write.table(dimscores,file="MDS_4D.txt",sep="\t",quote=FALSE) #plot dim 1 by dim 2 plot(MDS4Dim, plot.type = "confplot", plot.dim = c(1,2)) #plot dim 1 by dim 3 (as if looking into previous plot from the top) plot(MDS4Dim, plot.type = "confplot", plot.dim = c(1,3)) ``` Run ordinal multi-dimensional scaling (MDS) - 5 dimensional solution ```{r} # ndim is the number of dimensions MDS5Dim = mds(FCmatrixAll, ndim = 5, type = "ordinal") # view Kruskal stress MDS5Dim # get dim scores dimscores = MDS5Dim$conf # write dim scores out to a text file in your working directory write.table(dimscores,file="MDS_5D.txt",sep="\t",quote=FALSE) #plot dim 1 by dim 2 plot(MDS5Dim, plot.type = "confplot", plot.dim = c(1,2)) #plot dim 1 by dim 3 (as if looking into previous plot from the top) plot(MDS5Dim, plot.type = "confplot", plot.dim = c(1,3)) ``` Create stress plot ```{r} stressvalues = c(MDS1Dim$stress,MDS2Dim$stress,MDS3Dim$stress,MDS4Dim$stress,MDS5Dim$stress) dimensions = c(1,2,3,4,5) plot(dimensions,stressvalues) ```