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))
我遇到的问题是,我希望正态分布不仅仅围绕其均值,而且要在一条直线或一个圆上.在后一种情况下,我希望输出类似于火山.我想我必须首先在循环中创建一些概率.我怎样才能做到这一点?或者我也应该使用一些表面命令来绘制输出?我很确定这与二元正态无关.
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
例如:
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屋!