# Re: [R] is there a way to visualize 3D normal distributions?

From: Ben Bolker <bolker_at_zoo.ufl.edu>
Date: Sat 04 Feb 2006 - 04:35:10 EST

In response to your last question --

Duncan already gave you the answer in his last e-mail -- to give an explicit example, for a particular set of means and variance-covariance matrix:

library(rgl)
demo(shapes3d)
rgl.clear()
sphere <- ellipsoid3d(2,2,2,qmesh=TRUE)
means <- c(0,0,0)
S <- matrix(c(3,1,-1,1,2,-1,-1,-1,2),nrow=3) ellipsoid <- translate3d(rotate3d(sphere,matrix=chol(S)*qnorm(0.975)),

```                                     means[1],means[2],means[3])
```
axis3d()

Duncan Murdoch wrote:
> On 2/2/2006 5:01 PM, Michael wrote:
>

```>> shape3d only gives rigid sphere... not the free form ellipsoid that I
>> want...
```

>
>
> ? If you run the demo, you'll see ellipsoids...
>
> You just need to work out the appropriate transform to apply to a sphere
> to get the ellipsoid you want. I imagine something like this:
>
> sphere <- ellipsoid3d(2,2,2, qmesh=TRUE)
> ellipsoid <- translate3d(rotate3d(sphere, matrix=chol(S)), xbar, ybar,
> zbar)
>
>
> is what you want, where S is the covariance matrix, and xbar,ybar,zbar
> have the obvious meaning. (rotate3d() is used with a matrix that isn't
> a rotation matrix; it may not be obvious that this is allowed, but it is.)
>
> Duncan Murdoch
>
>
```>>
>> On 2/2/06, Ben Bolker <bolker@ufl.edu> wrote:
>>
>>> Duncan Murdoch <murdoch <at> stats.uwo.ca> writes:
>>>
>>>> On 2/2/2006 3:39 AM, Michael wrote:
>>>>
>>>>> Hi all,
>>>>>
>>>>> How do I visualize a contour of a tri-variate normal distribution?
>>>>>
>>>>> I just like to see the ellipsoid very much. I hope there is a easy way
>>>
>>> or
>>>
>>>>> existing method in R.
>>>>
>>>> The misc3d package includes a function for 3d contour plots; that
>>>> should
>>>> do what you want.
>>>>
>>>   is contour3d really necessary or could you just plot ellipsoids?
>>> (library(rgl); demo(shapes3d)) -- still a little bit of figuring
>>> to do, but this should get you most of the way there.
>>>
>>>   Ben Bolker
>>>
>>> ______________________________________________
>>> R-help@stat.math.ethz.ch mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> http://www.R-project.org/posting-guide.html
>>>
>>
>>     [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help@stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> http://www.R-project.org/posting-guide.html

--
620B Bartram Hall                            bolker@zoo.ufl.edu
Zoology Department, University of Florida    http://www.zoo.ufl.edu/bolker
Box 118525                                   (ph)  352-392-5697
Gainesville, FL 32611-8525                   (fax) 352-392-3704

______________________________________________
```
R-help@stat.math.ethz.ch mailing list