Created
March 7, 2013 19:59
-
-
Save IronistM/5111257 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# *-------------------------------------------------------------------- | |
# | FUNCTION: visCorrel | |
# | Creates an MDS plot where the distance between variables represents | |
# | correlation between the variables (closer=more correlated) | |
# *-------------------------------------------------------------------- | |
# | Version |Date |Programmer |Details of Change | |
# | 01 |05/01/2012|Simon Raper |first version. | |
# *-------------------------------------------------------------------- | |
# | INPUTS: dataset A dataframe containing only the explanatory | |
# | variables. It should not contain missing | |
# | values or variables where there is no | |
# | variation | |
# | abr The number of characters used when | |
# | abbreviating the variables. If set to zero | |
# | there is no abbreviation | |
# | method The options are 'metric' or 'ordinal'. The | |
# | default is metric | |
# *-------------------------------------------------------------------- | |
# | OUTPUTS: graph An MDS plot where the similarity measure is | |
# | correlation between the variables. | |
# | | |
# *-------------------------------------------------------------------- | |
# | USAGE: vis_correl(dataset, | |
# | abr) | |
# | | |
# *-------------------------------------------------------------------- | |
# | DEPENDS: ggplot2, directlabels | |
# | | |
# *-------------------------------------------------------------------- | |
# | NOTES: For more information about MDS please see | |
# | http://en.wikipedia.org/wiki/Multidimensional_scaling | |
# | | |
# *-------------------------------------------------------------------- | |
visCorrel<-function(dataset, abr, method="metric"){ | |
#Create correlation matrix | |
cor_ts<-cor(dataset) | |
n<-dim(cor_ts)[2] | |
# Create dissimilarities | |
ones<-matrix(rep(1,n^2), nrow=n) | |
dis_ts<-ones-abs(cor_ts) | |
# Do MDS | |
if ( method=="ordinal"){ | |
fit <- isoMDS(dis_ts, k=2)$points | |
} else { | |
cmd.res <- cmdscale(dis_ts, k=2, eig=TRUE) | |
eig<-cmd.res$eig | |
fit<-cmd.res$points | |
prop<-sum(abs(eig[1:2]))/sum(abs(eig)) | |
print(paste("Proportion of squared distances represented:", round(prop*100))) | |
if(prop<0.5){print("Less than 50% of squared distance is represented. Consider using ordinal scaling instead")} | |
} | |
x <- fit[,1] | |
y <- fit[,2] | |
labels<-row.names(cor_ts) | |
if (abr>0){labels<-substr(labels,1,abr)} | |
mds_plot<-data.frame(labels, x, y) | |
#Plot the results | |
g<-ggplot(mds_plot, aes(x=x, y=y, colour=labels, main="MDS Plot of Correlations"))+geom_point() + coord_fixed()+ opts(title ="MDS Plot of Correlations") | |
direct.label(g, first.qp) | |
} | |
# *-------------------------------------------------------------------- | |
# * Examples | |
# *-------------------------------------------------------------------- | |
# visCorrel(midwest[,4:27],10, method="classical") | |
# visCorrel(midwest[,4:27],10, method="ordinal") | |
# visCorrel(Crime[,-c(1,2,11,12)],10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment