[R] Any packages or ways available providing un-/a-/non-symmetric centered distributions?
Paul Menzel
paulepanter at users.sourceforge.net
Sun Dec 11 10:43:01 CET 2011
Dear R folks,
I would like to use un-/a-/non-symmetric centered, i. e. the expectation
vanishes/is zero, distributions.
Searching for that is not that easy since all three words un-, a-,
non-symmetric seem to be common.
Do I need to create such a distribution myself by for example composing
a distribution?
I would use `sample()` and for integer-valued the following would be one
example. The expectation vanishes too, 1/4 * (-4 - 1 + 2 + 3) = 0.
> x = sample(c(-4L, -1L, 2L, 3L))
> y = sample(c(-4L, -1L, 2L, 3L), 1e7, replace=TRUE); mean(y)
[1] 0.0006821
> # replicate will produce a matrix, but in this case it should not matter for the mean
> x = replicate(2, sample(c(-4L, -1L, 2L, 3L), replace=TRUE)); x
[,1] [,2]
[1,] -1 2
[2,] 3 -1
[3,] 3 -1
[4,] -1 3
> mean(x)
[1] 0.875
> # Passing the length explicitly to `sample()` as a work around.
> x = replicate(1e1, sample(c(-4L, -1L, 2L, 3L), 1, replace=TRUE)); x
[1] -4 2 2 3 -4 -4 2 2 2 2
For continues (without 0) distributions I came up with the following.
> # -rexp(1, 2) has expectation -1.
> # sqrt(pi/2) * abs(rnorm(1))
> # http://en.wikipedia.org/wiki/Half-normal_distribution
> # X ~ N(0, 1) ⇒ E(|X|) = √(2/π)
> x = sample(c(sqrt(pi/2) * abs(rnorm(1)), -rexp(1, 1)), 1); x
[1] 0.2730692
Without passing `, 1` as the length to `sample` I would get two values.
> x = sample(c(sqrt(pi/2) * abs(rnorm(1)), -rexp(1, 1))); x
[1] -0.4055199 1.8830235
Wanting to use `replicate()` therefore the explicit length for
`sample()` should be passed here too.
> x = replicate(1e5, sample(c(sqrt(pi/2) * abs(rnorm(1)), -rexp(1, 1)), 1)); mean(x)
[1] -0.001099127
Are there other sophisticated ways to create such distributions easily?
Thanks,
Paul
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20111211/d7613d40/attachment.bin>
More information about the R-help
mailing list