全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
3695 9
2009-03-13

老板布置任务,刚开始学习R,不太熟习,请教大家

先谢谢啦!!!!

现在,我用命令

library(MASS)

sigma<-matrix(c(1,0.5,0.5,1),2,2);

X<-mvrnorm(100,c(0,0),sigma);

产生了100个二维正态随机数,怎么产生它的三维图表示?

请大侠指点!!

[em12][em12]
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2009-3-14 00:54:00
You need to calculate the density for each element of an array whose rows correspond to x values, and columns to y values. And then use persp to draw the 3d plot. Hope the following helps.

library(MASS)
sigma<-matrix(c(1,0.5,0.5,1),2,2);
X<-mvrnorm(100,c(0,0),sigma);


x<-seq(-3,3,by=.01)
y<-x
phi<-function(x,y)
{sigma=cor(X)[1,2]
exp(-(x^2-2*sigma*x*y+y^2)/2/(1-sigma^2))/2/pi/sqrt(1-sigma^2)
}
z <- outer(x, y, phi)
persp(x,y,z,theta=30,phi=30,col="lightblue",shade=.3,border=NA)
title(paste("bivariate normal distribution with cor=",round(cor(X)[1,2],3)))
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2009-3-14 11:59:00

 


Thank you!


This sentence ‘exp(-(x^2-2*sigma*x*y+y^2)/2/(1-sigma^2))/2/pi/sqrt(1-sigma^2)’


is puzzling for me!


If we don't know these data is obtained from the normal distrbution,


we only have these data,how can I do?


I found a package 'rgl' which is used for 3D ,



#**********************alpha正态污染图*****************************
alpha=0.1;sigma=5;
fn=function(x,y){(1-alpha)/(2*pi)*exp(-(x^2+y^2)/2)+
alpha/(2*pi*sigma)*exp(-(x^2+y^2)/(2*sigma))  }


x<-seq(-2,2,len=17);y<-seq(-2,2,len=17);
z<-outer(x,y,fn);
color<-rainbow(41)[1+round(10*outer(x,y,fn))]  ;
library(rgl)         #rgl为专门画3D图的包
persp3d(x,y,z,color=color,smooth=FALSE)
surface3d(x,y,z+0.01,front="lines",back="culled")
#******************使图像自动旋转**********************************
M <- par3d("userMatrix")
play3d( par3dinterp( userMatrix=list(M,
                                     rotate3d(M, pi/2, 1, 0, 0),
                                     rotate3d(M, pi/2, 0, 1, 0) ) ),
        duration=4 )
movie3d( spin3d(), duration=5 )
#******************************************************************


Please check for me ,if you can draw a little time!


Thank you very much!


 


 

[此贴子已经被作者于2009-3-14 11:59:36编辑过]

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2009-3-20 22:26:00
that expression "exp(-(x^2-2*sigma*x*y+y^2)/2/(1-sigma^2))/2/pi/sqrt(1-sigma^2)" is the expression for bivariate-normal distribution density function.

if you have no prior knowledge about the distribution of your data. there is not too much you can do. maybe use some non-parametric methods to estimate their potential distributions.
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2009-3-21 09:58:00
If you want to plot 3D. you can also use the function contour(),the details you can get form the help files.
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2009-3-21 12:29:00
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群