如何使用较少的包绘制二元正态分布的曲面和轮廓 [英] How to plot the surface and contours of a bivariate normal distribution using fewer packages

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

问题描述

我将绘制二元正态分布的 3D 表面及其轮廓(它可以是任何二元正态分布).我想在我的绘图中使用 perspcontour.我在互联网上搜索,但我找到了很多方法.他们中的大多数都使用了一些软件包.但是我想以使用更少的包甚至不安装任何包的方式来做到这一点(但是,我不知道这是可能的).你能用一个二元正态分布的例子告诉我你自己的方式吗?[更少的包,更好的解决方案].实际上,我希望看到绘制二元正态分布的曲面和轮廓的最简单方法.

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 中做任何你喜欢做的事情,只要你准备好编写自己的代码来做这件事.使用 perspcontour 仅用几行 R 代码就可以可视化二维表面,但是生成表面(即创建二维密度情节,不是微不足道的.

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 年 6 月 17 日创建

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

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

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