if (FALSE) {
# Skew-normal / logistic example:
dens1 <- function(x, shape=4)
# skew-normal distribution's density
# see also: http://azzalini.stat.unipd.it/SN/Intro
{
return(2 * dnorm(x) * pnorm(shape * x))
}
dens2 <- function(x)
# logistic distribution's density
{
return(dlogis(x, location=0, scale=1))
}
rskewnorm <- function(n, shape=4)
# skew-normal random number generation
# (according to http://azzalini.stat.unipd.it/SN/faq-r.html)
{
delta <- shape / sqrt(shape^2+1)
u1 <- rnorm(n); v <- rnorm(n)
u2 <- delta * u1 + sqrt(1-delta^2) * v
return(apply(cbind(u1,u2), 1, function(x){ifelse(x[1]>=0, x[2], -x[2])}))
}
# compute convolution:
conv <- convolve(dens1, dens2)
# illustrate convolution:
n <- 100000
x <- rskewnorm(n)
y <- rlogis(n)
z <- x + y
# determine empirical and theoretical quantiles:
p <- c(0.001,0.01, 0.1, 0.5, 0.9, 0.99, 0.999)
equant <- quantile(z, prob=p)
tquant <- conv$quantile(p)
# show numbers:
print(cbind("p"=p, "empirical"=equant, "theoretical"=tquant))
# draw Q-Q plot:
rg <- range(c(equant, tquant))
plot(rg, rg, type="n", asp=1, main="Q-Q-plot",
xlab="theoretical quantile", ylab="empirical quantile")
abline(0, 1, col="grey")
points(tquant, equant)
}
Run the code above in your browser using DataLab