[R] Hypercube in R

William Dunlap wdunlap at tibco.com
Wed Feb 10 20:59:41 CET 2010

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Andrej Kastrin
> Sent: Monday, February 08, 2010 12:37 PM
> To: r-help at r-project.org
> Subject: [R] Hypercube in R
> Dear all,
> Does anybody have an idea or suggestion how to construct (plot)
> 4-dimensional hypercube in R.
> Thanks in advance for any pointers.

Try the following, which plots a projection of
a `dimension`-cube to a random 2 dimensional
subspace.  It uses lm() to do the projection
since that is how I think of linear models, but
you could use more direct matrix manipulations.
It has too many calls to t() in it and it should
let you specify the subspace.  Try it with
   par(mfrow=c(3,3), mar=rep(0,4))
   for(i in 1:9) f(dimension=4)
to get a 9 random views.

f <- function(dimension) {
  rorthogonal <- function(n = 2, dimension) {
      # generate `n` random orthonormal vectors
      # in `dimension` dimensional space
      x <- matrix(rnorm(n*dimension), nrow=n, ncol=dimension)
  cube <- as.matrix(do.call(expand.grid, rep(list(c(-1,1)),dimension)))
  # the points pairs with distance 2 are connected by edges
  ij <- which(abs(as.matrix(dist(cube))-2)<1e-7, arr.ind=TRUE)
  # only need edge in one direction
  ij <- ij[ij[,1]<ij[,2],]
  basis <- rorthogonal(n=2, dim=dimension)
  # p is projection of vectices to subspace of basis
  p <- t(lm.fit(x=t(basis), y=t(cube))$coef)
  # plot vertices
  # plot edges
  segments(p[,1][ij[,1]], p[,2][ij[,1]], p[,1][ij[,2]], p[,2][ij[,2]])

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> Regards, Andrej
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list