[R] Reading MINE output into a matrix
Matt Considine
matt at considine.net
Sun Jan 15 16:32:05 CET 2012
I've benefited from this list with input on how to build up a
symmetrical matrix. The purpose of that query was to work with the
output from the MINE routine posted at www.exploredata.net
To the extent it helps others, here is the script that I was working on
an which turns a given MINE output column (in the case below, the third
column corresponding to "MIC") into a matrix.
Hope it helps,
Matt
#needed for MINE routine
require(rJava)
#load market data
require(PortfolioAnalytics)
data(indexes)
#write CSV file of data to current working directory
datafilename <- "indexes.csv"
write.table(indexes, datafilename, sep=",", col.names=TRUE,
row.names=FALSE, quote=FALSE, na="NA")
#read MINE R code
source.with.encoding('MINE.r', encoding='UTF-8')
pairs_method <- "all.pairs"
max_num_boxes_exponent <- 0.6
num_clumps_factor <- 15
#run MINE routine on data
MINE(datafilename,style=pairs_method,
max.num.boxes.exponent=max_num_boxes_exponent,
num.clumps.factor=num_clumps_factor)
#read output of MINE routine
#data is sorted in descending order of MIC variable
#output is half of a square symmetric matrix, excluding diagonal
#there are 9 columns, 7 of which are various stats
#calc of outputfilename could be better handled ...
# kludge included to deal with filename generated on Windows
outputfilename <- sprintf("%s,%s,cv=0.0,B=n^%g,Results.csv",datafilename,
sub(".","",pairs_method,fixed=TRUE),
max_num_boxes_exponent)
x<-read.csv(outputfilename,header=TRUE)
#isolate row/col frequencies as a matrix. we need to look at
# both to get the complete list of pairs and their respective frequencies
xtable<-table(x$X.var)
ytable<-table(x$Y.var)
#map frequencies of X & Y vars to rows
xmap<-xtable[x$X.var]
ymap<-ytable[x$Y.var]
finalmap<-order(xmap,-ymap,decreasing=TRUE)
#fill in matrix - we want the third column for MIC
z<-diag(length(levels(x$X.var))+1)
z[row(z)>col(z)]<-x[finalmap,3]
z<-z+t(z)
diag(z)<-1
#determine and set row/column names
varnames<-c(names(sort(xtable,decreasing=TRUE)),names(sort(ytable,decreasing=TRUE))[1])
rownames(z)<-varnames
colnames(z)<-varnames
z
More information about the R-help
mailing list