简体   繁体   中英

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. 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? I'd really appreciate it.

The circular package seems focused on vonMises distribution. 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 :

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". Further note: That formula is really just a truncated exponential at 2 pi. Since it is a truncated density it seems that it should not have a 0 value at 2*pi. Appears that radial plot uses the range of the radial vector rather than going from 0 to maximum. Seems that it should default instead to 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.

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()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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