Question

Is there a density function for the two-piece Normal distribution:

enter image description here

on CRAN? Thought I would check before I code one. I have checked the distribution task view. It is not listed there. I have looked in a couple of likely packages, but to no avail.

Update: I have added dsplitnorm, psplitnorm, qsplitnorm and rsplitnorm functions to the fanplot package.

Was it helpful?

Solution

If you choose to construct your own version of the distribution, you might be interested in distr. It (and the related packages distrEx, distrSim, distrTEst, distrTeach and distrDoc) have been written to provide a unified interface for constructing new distributions from existing ones. (I constructed this example with the help of the wonderful vignette that accompanies the distrDoc package and which can be gotten by typing vignette("distr").)

This implements the split normal distribution, which may not be exactly what you are after. Using the distr toolset, though, it shouldn't be too hard to adjust this to fit your exact needs.

library(distr)

## Construct the distribution object.
## Here, it's a split normal distribution with mode=0, and lower- and
## upper-half standard deviations of 1 and 2, respectively.
splitNorm <- UnivarMixingDistribution(Truncate(Norm(0,2), upper=0), 
                                      Truncate(Norm(0,1), lower=0), 
                                      mixCoeff=c(0.5, 0.5))
## Construct its density function ...
dsplitNorm <- d(splitNorm)
## ... and a function for sampling random variates from it
rsplitNorm <- r(splitNorm)

## Compare the density it returns to that from rnorm()
dsplitNorm(-1)
# [1] 0.1760327    
dnorm(-1, sd=2)
# [1] 0.1760327    

## Sample and plot a million random variates from the distribution
x <- rsplitNorm(1e6)         
hist(x, breaks=100, col="grey")

## Plot the distribution's continuous density
plot(splitNorm, to.draw.arg="d") 

enter image description here

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top