R在(x,y)点附近的3D正态分布图 [英] 3D Plot of normal distribution in R around a (x,y) point
问题描述
我想在(x,y,z)坐标系上绘制正态分布的单变量正态密度函数。
我正在使用的代码是:
I want to plot a univariate normal density function of the normal distribution onto a (x,y,z) coordinate system. The code I am using is:
library(rgl)
open3d()
x <- seq(0, 10, length=100)
y <- seq(0, 10, length=100)
z = outer(x,y, function(x,y) dnorm(x,2.5,1)*dnorm(y,2.5,1))
persp3d(x, y, z,col = rainbow(100))
我遇到的一个问题是我希望正态分布不仅在均值附近,而且还希望在直线或圆上。在后一种情况下,我希望输出类似于火山。我想我必须首先在循环中创建一些概率。我怎样才能做到这一点?还是我还应该使用一些Surface命令来绘制输出?我很确定这与二元正态无关。
The problem I an encountering is that I want the normal distribution not to be around its mean only but also to be on a straight line or a circle. In latter case, I would expect the output to be similar to a volcano. I guess I must first create some probabilities within a loop. How can I do this? Or should I also use some surface command to plot the output? I am pretty sure this has nothing to do with a bivariate normal though.
最好的
富士
推荐答案
第一部分很简单:只是不要让您的 z
依赖于 y $ c例如$ c>
The first part is easy: just don't let your z
depend on y
for instance:
z = outer(x,y, function(x,y) dnorm(x,2.5,1))
persp3d(x, y, z,col = rainbow(100))
对于第二部分,您可以想象正态分布的均值位于 x ^ 2 + y ^ 2 = 1
圆上。您将拥有无限的径向正态分布。试试这个:
For the second part, you can imagine that the means of the normal distribution lie on the x^2+y^2=1
circle. You will have infinite normal distributions with radial directions. Try this:
#define the volcano function
volcano<-function(x,y,sigma=1/2) {
alpha<-atan(y/x)+pi*(x<0)
d<-sqrt((cos(alpha)-x)^2 + (sin(alpha)-y)^2)
dnorm(d,0,sigma)
}
x<-seq(-2,2,length.out=100)
y<-seq(-2,2,length.out=100)
z<-outer(x,y,volcano)
persp3d(x, y, z,col = rainbow(100))
这篇关于R在(x,y)点附近的3D正态分布图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!