Skip to content

Instantly share code, notes, and snippets.

@encima
Last active August 10, 2016 10:48
Show Gist options
  • Save encima/9fd3a1c72b4f931cf01405c94f1d0c93 to your computer and use it in GitHub Desktop.
Save encima/9fd3a1c72b4f931cf01405c94f1d0c93 to your computer and use it in GitHub Desktop.
Multivariate Analysis and MDS Scratchpad
library(MASS)
library(car)
makeProfilePlot <- function(list,words) {
# find out the minimum and maximum values of the variables:
mymin <- 1e+20
mymax <- 1e-20
for (i in 1:length(list)) {
vectori <- mylist[[i]]
mini <- min(vectori)
maxi <- max(vectori)
if (mini < mymin) { mymin <- mini }
if (maxi > mymax) { mymax <- maxi }
}
# plot the variables
for (i in 1:length(list)) {
vectori <- list[[i]]
wordi <- words[i]
if (i == 1) {
plot(vectori, col="green", type="l",ylim=c(mymin,mymax))
} else {
points(vectori,col="green", type="l")
}
lastxval <- length(vectori)
lastyval <- vectori[length(vectori)]
#poor colour choices, I know!
text((lastxval-10),(lastyval),wordi,col="red",cex=0.6)
}
}
#update for your working directory
setwd("~/development/js/susan/")
data <- read.csv('data.csv')
#sample profile plot to show variance of
names <- c("Very","Really","So","Like","Sort of", "Kind of")
sample_comp_list<-list(data$very, data$really, data$so, data$like, data$sortof, data$kindof)
makeProfilePlot(sample_comp_list, names)
#remove categorical data, extract sample variables
numData <- data[11:length(data)]
scatterplotMatrix(numData)
#non metric mds, not including word count
data[11:length(data)]
d <- dist(data[11:length(data)])
fit <- isoMDS(d, k=2)
fit
x <- fit$points[,1]
y <- fit$points[,2]
plot(x, y, xlab="x", ylab="y", main="Nonmetric MDS", type="n")
text(x, y, labels = data$character, cex=.7)
#metric mds with no reliance on external libs
(mds <- cmdscale(d))
plot(mds, type = 'n')
text(mds[, 1], mds[, 2], data$character)
#TODO: visualisation of categorical variables (age, show type etc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment