[R] Re: Plotting bivariate normal distributions.

ucgamdo@ucl.ac.uk ucgamdo at ucl.ac.uk
Mon Sep 1 12:38:27 CEST 2003


You'll find that it is a lot easier to do it in R:

# lets first simulate a bivariate normal sample
library(MASS)
bivn <- mvrnorm(1000, mu = c(0, 0), Sigma = matrix(c(1, .5, .5, 1), 2))

# now we do a kernel density estimate
bivn.kde <- kde2d(bivn[,1], bivn[,2], n = 50)

# now plot your results
contour(bivn.kde)
image(bivn.kde)
persp(bivn.kde, phi = 45, theta = 30)

# fancy contour with image
image(bivn.kde); contour(bivn.kde, add = T)

# fancy perspective
persp(bivn.kde, phi = 45, theta = 30, shade = .1, border = NA)

#########################################################################
Hi,

I've used the Mathematica to produce 3D graphics, contour plots of a
bivariate normal distribution

Now I want make these graphics in R, but i do not know how.
I would like to:
- Plot a 3D graph for some different variance matrix
- Plot the contour plots
- Find and try to plot (in the 3d graph ou contour plot) the (1-a)%
confidence region based in a chi-square(a) with the degrees of freedom
equal a 2 or bigger.

Below is the Mathematica Notebook that i've used until now


<< "Graphics`PlotField`"

NB[x_,y_]:=(1/((2 Pi)*Sqrt[a*b*(1-c^2)]))*Exp[(-1/(2*(1-c^2)))*( 
          ((x-u)/Sqrt[a])^2 + ((y-v)/Sqrt[b])^2 
			- 2*c(((x-u)/Sqrt[a])((y-v)/Sqrt[b]))
								)]

{{a,c}, {c,b}} = {{1,0}, {0,1}};  The covariance Matrix
{u,v} = {0,0};                    Mean vector
Plot3D[NB[x,y],{x,-1.5,1.5},{y,-1.5,1.5},
		AxesLabel->{x,y,z},
		BoxRatios->{1,1,1}];
ContourPlot[NB[x,y],{x,-1,1},{y,-1,1},
		Axes->True, 
		AxesLabel->{x,y}];

3d graph rotation
Do[
	Plot3D[NB[x,y],{x,-1.5,1.5},{y,-1.5,1.5},
		PlotPoints->20,
		Mesh ->False,
		SphericalRegion ->True,
		Axes ->None,
		Boxed ->False,
		ViewPoint->{2 Cos[t], 2 Sin[t], 1.3},
		BoxRatios->{1,1,1}
	],{t, 0, 2Pi-2Pi/36, 2Pi/36}]


Thanks, 
Rafael
-- 
  
  bertola at fastmail.fm

-- 

                          love email again




More information about the R-help mailing list