[R] Hypercube in R
akastrin
andrej.kastrin at gmail.com
Wed Feb 10 21:50:45 CET 2010
Your solution seems wonderful. I'm going to uninstall ggobi :) Thanks
again.
Cheers, Andrej
On Feb 10, 8:59 pm, "William Dunlap" <wdun... at tibco.com> wrote:
> > -----Original Message-----
> > From: r-help-boun... at r-project.org
> > [mailto:r-help-boun... at r-project.org] On Behalf Of Andrej Kastrin
> > Sent: Monday, February 08, 2010 12:37 PM
> > To: r-h... 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)
> t(eigen(crossprod(x))$vectors)
> }
> 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(p[,1],p[,2],asp=1)
> # 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-h... 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.
>
> ______________________________________________
> R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list