R/Hierarchical.sim.resampling.R

"Hierarchical.sim.resampling" <-
function(X, c = 2, nsub=100, f = 0.8, s = sFM, distance="euclidean", hmethod="ward.D") {

 n <- ncol(X);
 n.sub.ex <- ceiling(n * f);
 sim.vector <- numeric(nsub);
 
 for (i in 1:nsub)  {
   sub1 <- sample(n, n.sub.ex);
   Xsub1 <- X[,sub1];
	 colnames(Xsub1)<-sub1;
	 
	 if (distance == "euclidean")
		  d <- stats::dist(t(Xsub1))
	 else 
			d <- stats::as.dist(1 - stats::cor(Xsub1));
	 tr1 <- stats::hclust(d, method = hmethod);
	 plot(tr1, main="");
	 cl1 <- stats::rect.hclust(tr1, k = c);
	 M1 <- Do.boolean.membership.matrix(cl1, n.sub.ex, sub1);
	 
	 sub2 <- sample(n, n.sub.ex);
   Xsub2 <- X[,sub2];
	 colnames(Xsub2)<-sub2;
	 
	 if (distance == "euclidean")
		  d <- stats::dist(t(Xsub2))
	 else 
			d <- stats::as.dist(1 - stats::cor(Xsub2));
	 tr2 <- stats::hclust(d, method = hmethod);
	 plot(tr2, main="");
	 cl2 <- stats::rect.hclust(tr2, k = c);
	 M2 <- Do.boolean.membership.matrix(cl2, n.sub.ex, sub2);
	 # examples common two the two subsamples
	 sub.common <- Intersect(sub1,sub2);
	 
	 # extract from the membership matrices the rows and columns that 
	 # correspond to the examples common two the two subsamples
	 label.examples <- as.character(sub.common);
	 M1 <- M1[label.examples, label.examples];
	 M2 <- M2[label.examples, label.examples];
	 sim.vector[i] <- s(M1,M2);
 }
 return(sim.vector);
}

Try the mosclust package in your browser

Any scripts or data that you put into this service are public.

mosclust documentation built on June 8, 2025, 11:23 a.m.