简体   繁体   English

在 R 中绘制圆形随机变量的 PDF(极坐标图)

[英]Plotting PDFs of circular random variables in R (polar plots)

I am trying to find a sensible way of visualising probability density functions of circular random variables in R. The most appropriate way of doing so is on a polar plot.我试图找到一种合理的方法来可视化 R 中圆形随机变量的概率密度函数。最合适的方法是在极坐标图上。 However, I am not sure how to code this in R. Can someone illustrate how this can be done, using the wrapped exponential distribution as an example?但是,我不确定如何在 R 中对此进行编码。有人可以使用 包装指数分布为例说明如何做到这一点吗? I'd really appreciate it.我真的很感激。

The circular package seems focused on vonMises distribution.循环包似乎专注于 vonMises 分发。 You can roll your own wrapped exponential probability distribution:您可以推出自己的包裹指数概率分布:

wrapped.exp <- function(theta, lambda){ 
                          lambda*exp(-lambda*theta)/(1-exp(-2*pi*lambda) )}

And use plotrix package's radial.plot :并使用 plotrix 包的radial.plot

theta <- seq(0, 2*pi, len=100)
rval <- wrapped.exp(theta, .2)
# radial.plot's default radial limits are just the range so not really good for distributions.
radial.plot(rval,theta, radial.lim=c(0, 0.4))

在此处输入图片说明

Plotting code:绘图代码:

> png()
> rval <- wrapped.exp(theta, .2)
> radial.plot(rval,theta, radial.lim=c(0, 0.4))
> dev.off()

I would note that the "wrapped exponential" formula is not actually a true mathematical function on the closed interval [0, 2*pi] since this function would have two values at 0 = 2 pi, so it probably should have code to make any theta + or > 2 pi to be NA or something "mod 2 pi".我会注意到“包裹指数”公式实际上并不是闭区间[0, 2*pi]上的真正数学函数[0, 2*pi]因为该函数在 0 = 2 pi处有两个值,因此它可能应该有代码来制作任何theta + 或 > 2 pi 为 NA 或“mod 2 pi”。 Further note: That formula is really just a truncated exponential at 2 pi.进一步注意:该公式实际上只是 2 pi处的截断指数 Since it is a truncated density it seems that it should not have a 0 value at 2*pi.由于它是一个截断的密度,它似乎不应该在 2*pi 处具有 0 值。 Appears that radial plot uses the range of the radial vector rather than going from 0 to maximum.出现径向图使用径向矢量的范围,而不是从 0 到最大值。 Seems that it should default instead to c(0, range(lambda)[2]).似乎它应该默认为 c(0, range(lambda)[2])。 When I get time I will correct this answer.当我有时间时,我会更正这个答案。

The ezpolar function from pkg:pracma gives a more typical polar plot than plotrix::radial.plot , however it is like the base graphics function curve in that it takes a functional argument. pkg:pracma 中的ezpolar函数提供了比plotrix::radial.plot更典型的极坐标图,但是它就像基本图形函数curve ,因为它采用函数参数。

if(!require(pracma) ){ 
         install.packages("pracma", dependencies=TRUE); library(pracma) }
?ezpolar
wrapped.exp <- function(theta, lambda){ 
                      lambda*exp(-lambda*theta)/(1-exp(-2*pi*lambda) )}
theta <- seq(0, 2*pi, len=100)

# since it requires a functional argument with only one variable,
# I'm defining a separate helper function with predefined lambda

  png() ;  wrap.2 <- function(x) wrapped.exp(x,.2)
           ezpolar(wrap.2); dev.off()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM