# --- Derive weighted networks via JRF
nclasses=2 # number of data sets / classes
n1<-n2<-50 # sample size for each data sets
p<-100 # number of variables (genes)
# --- Generate data sets
data1<-matrix(rnorm(p*n1),p,n1) # generate data1
data2<-matrix(rnorm(p*n2),p,n1) # generate data2
# --- Standardize variables to mean 0 and variance 1
data1 <- t(apply(data1, 1, function(x) { (x - mean(x)) / sd(x) } ))
data2 <- t(apply(data2, 1, function(x) { (x - mean(x)) / sd(x) } ))
# --- Initialize variables
imp1<-imp2<-matrix(0,p,p) # matrix to store importance scores
ntree=1000; # number of trees
nsample<-c(n1,n2) # vector containing sample size for each class
# --- run JRF for each target gene
for (j in 1:2){ # for loop over target genes
#--- create matrix (classes by max(n1,n2)) of response variable
y<-matrix(0,2,max(n1,n2));
y[1,seq(1,n1)]<-as.matrix(data1[j,])
y[2,seq(1,n2)]<-as.matrix(data2[j,])
x<-matrix(0,p*2-2,max(n1,n2)) #--- matrix of covariates
x[seq(1,p-1),seq(1,n1)]<-as.matrix(data1[-j,])
x[seq(p,2*p-2),seq(1,n2)]<-as.matrix(data2[-j,])
jrf.out<-JRF(x=x,y=y,mtry=round(sqrt(p-1)),importance=TRUE,
sampsize=nsample,nclasses=nclasses,ntree=ntree)
imp1[-j,j]<-importance(jrf.out,scale=FALSE)[seq(1,p-1)] #- importance for net1
imp2[-j,j]<-importance(jrf.out,scale=FALSE)[seq(p,(p-1)*2)] #- importance for net2
}Run the code above in your browser using DataLab