Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating multidimensional data

Does R have a package for generating random numbers in multi-dimensional space? For example, suppose I want to generate 1000 points inside a cuboid or a sphere.

like image 793
Pradeep Avatar asked Feb 16 '11 13:02

Pradeep


People also ask

What is multidimensional data example?

For example, a dimensional table for an item may contain the attributes item_name, brand, and type. A multidimensional data model is organized around a central theme, for example, sales. This theme is represented by a fact table. Facts are numerical measures.

What is meant by multidimensional data?

Multidimensional data is a data set with many different columns, also called features or attributes. The more columns in the data set, the more likely you are to discover hidden insights. In this case, two-dimensional analysis falls flat. Think of this data as being in a cube on multiple planes.

Which sources can be used to create multidimensional model?

It is typically created after the data source object, using the schema of the source database as the basis. However, you can choose other ways to build a model, including starting with cubes and dimensions, and generating the schema that best supports your design.


1 Answers

I have some functions for hypercube and n-sphere selection that generate dataframes with cartesian coordinates and guarantee a uniform distribution through the hypercube or n-sphere for an arbitrary amount of dimensions :

GenerateCubiclePoints <- function(nrPoints,nrDim,center=rep(0,nrDim),l=1){

    x <-  matrix(runif(nrPoints*nrDim,-1,1),ncol=nrDim)
    x <-  as.data.frame(
            t(apply(x*(l/2),1,'+',center))
          )
    names(x) <- make.names(seq_len(nrDim))
    x
}

is in a cube/hypercube of nrDim dimensions with a center and l the length of one side.

For an n-sphere with nrDim dimensions, you can do something similar, where r is the radius :

GenerateSpherePoints <- function(nrPoints,nrDim,center=rep(0,nrDim),r=1){
    #generate the polar coordinates!
    x <-  matrix(runif(nrPoints*nrDim,-pi,pi),ncol=nrDim)
    x[,nrDim] <- x[,nrDim]/2
    #recalculate them to cartesians
    sin.x <- sin(x)
    cos.x <- cos(x)
    cos.x[,nrDim] <- 1  # see the formula for n.spheres

    y <- sapply(1:nrDim, function(i){
        if(i==1){
          cos.x[,1]
        } else {
          cos.x[,i]*apply(sin.x[,1:(i-1),drop=F],1,prod)
        }
    })*sqrt(runif(nrPoints,0,r^2))

    y <-  as.data.frame(
            t(apply(y,1,'+',center))
          )

    names(y) <- make.names(seq_len(nrDim))
    y
}

in 2 dimensions, these give :

enter image description here

From code :

 T1 <- GenerateCubiclePoints(10000,2,c(4,3),5)
 T2 <- GenerateSpherePoints(10000,2,c(-5,3),2)
 op <- par(mfrow=c(1,2))
 plot(T1)
 plot(T2)
 par(op)
like image 183
Joris Meys Avatar answered Oct 05 '22 14:10

Joris Meys