如何使用更少的包绘制双变量正态分布的表面和轮廓 [英] How to plot the surface and contours of a bivariate normal distribution using fewer packages

查看:90
本文介绍了如何使用更少的包绘制双变量正态分布的表面和轮廓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要绘制一个二元正态分布的3D表面及其轮廓(可以是任何二元正态分布).我想在图形中使用 persp contour .我在Internet上进行了搜索,但是发现了很多方法.他们中的大多数人已经使用了一些软件包.但是我想以一种使用更少软件包的方式来执行此操作,或者甚至不安装任何软件包(但是,我不知道这是可能的).您能否以二元正态分布为例,向我展示自己的方式?[更少的软件包,更好的解决方案].的确,我希望看到绘制二元正态分布的表面和轮廓的最简单方法.

I am going to plot the 3D surface of a bivariate normal distribution and its contours (it could be any bivariate normal distribution). I want to use persp and contour in my drawing. I searched on the Internet, but I found many ways to do it. Most of them have used some packages. But I would like to do this in a way that uses fewer packages or even without installing any package (however, I do not know it is possible). Could you please show me your own way with an example of a bivariate normal distribution? [Fewer packages, better solution]. Indeed I would like to see the simplest way of drawing a surface and contours of a bivariate normal distribution.

推荐答案

只要您准备编写自己的代码即可在R中完成任何您喜欢的事情而无需使用包.使用 persp contour 仅用几行R代码就可以可视化2d表面,但是生成表面(即创建2d密度)情节,并非无关紧要.

You can do just about anything you like in R without using packages, as long as you are prepared to write your own code to do it. Visualising a 2d surface is possible in just a few lines of R code using persp and contour, but generating the surface (i.e. creating a 2d density plot, is not trivial.

在这里,您可以仅使用 mnormt 包(如注释中所述)生成二维双变量正态分布曲面.对于此示例,我组成了一个虚拟方差-协方差矩阵.

Here is how you can generate a 2d bivariate normal distribution surface using just the mnormt package (as requested in the comments). For this example, I have made up a dummy variance-covariance matrix.

library(mnormt)

x     <- seq(-5, 5, 0.25) 
y     <- seq(-5, 5, 0.25)
mu    <- c(0, 0)
sigma <- matrix(c(2, -1, -1, 2), nrow = 2)
f     <- function(x, y) dmnorm(cbind(x, y), mu, sigma)
z     <- outer(x, y, f)

contour(x, y, z)


persp(x, y, z, theta = -30, phi = 25, 
      shade = 0.75, col = "gold", expand = 0.5, r = 2, 
      ltheta = 25, ticktype = "detailed")

reprex程序包(v0.3.0)创建于2020-06-17 sup>

Created on 2020-06-17 by the reprex package (v0.3.0)

这篇关于如何使用更少的包绘制双变量正态分布的表面和轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆