 # right/left skewed normal distribution curve in R and shading the area?

How can we create skewed normal distribution curve in R ?

My aim is to produce skewed normal distribution to represent accounting CVA (expcted losses) and regulatory CVA (99% quantile). I want to have these in a same figure and shaded

These are the codes that I have used, to produce the figure. However, when i introduce shade function, the skew despair.

``````library(sn)

X <- seq(-1, 2, 0.01)
plot(X, dsn(X, xi = 0.1, omega = 0.3, alpha = 5), type = "l")
abline(v = 0.2)
shadenorm(below=1, justbelow = TRUE, color = 'blue')

``````

I would also like to label axis, y labelled as probability while x labelled exposure.

I know that there is similar post [quote="AbhishekHP, post:1, topic:39115, full:true"]
How can we create skewed normal distribution curve in R ?
How to vary the skewedness using a variable ?
i.e. height of the peak and tail of the plot ?

Although many links say that they have an answer but non worked

This is in addition to earlier question:
How to calculate the cumulative area of the overlapped normal distribution curve in R ? solved by @Yarnabrina

``````# Tried
library(fGarch)
library(tidyverse)
N <- 10000
x <- rnbinom(N, 10, .5)
dsnorm(x, mean = 0, sd = 1, xi = 1.5, log = FALSE)
# psnorm(x, mean = 0, sd = 1, xi = 1.5) %>% plot()
# qsnorm(x, mean = 0, sd = 1, xi = 1.5) %>% plot()
# rsnorm(x, mean = 0, sd = 1, xi = 1.5) %>% plot()

plot(dsnorm(x, mean = 0, sd = 1, xi = 1.5, log = FALSE))
[/quote]

but it does not answer my question``````

I do not understand what is the problem. My previous code seems to work fine, if I guess correctly what you want to do. I can not be sure, as I do not know what does `shadenorm` do, or which package contains this function. Here's an example with base graphics, and you can switch to `ggplot2` if you prefer.

``````library(sn)
#>
#> Attaching package: 'sn'
#> The following object is masked from 'package:stats':
#>
#>     sd

values <- seq(from = -1,
to = 2,
by = 0.01)
densities <- dsn(x = values,
xi = 0.1,
omega = 0.3,
alpha = 5)

plot(x = values,
y = densities,
type = "l")
abline(v = 0.2)
polygon(x = c(min(values), values[values < 0.2], 0.2),
y = c(0, densities[values < 0.2], 0),
border = NA,
alpha.f = 0.5))
`````` If this does not help, please provide a reproducible example. And please take a look here:

1 Like

Thanks @Yarnabrina the codes are working fine, however, what if I want to add another shading in that same graph, say to indicate 99% percentile. The codes are working fine.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

This is a general solution to shade below a point, above a point and in between two points. I have intentionally didn't use `mean` and `quantile` so that you can do that yourself. Hope this helps.

``````# loading library
library(sn)
#>
#> Attaching package: 'sn'
#> The following object is masked from 'package:stats':
#>
#>     sd

# generating data
exposures <- seq(from = -1,
to = 2,
by = 0.01)
probabilities <- dsn(x = exposures,
xi = 0.1,
omega = 0.3,
alpha = 5)

# calculating measures
measures <- quantile(x = exposures,
probs = c(0.40, 0.60))

# plotting distribution
plot(x = exposures,
y = probabilities,
type = "l")

# adding vertical lines at selected measures
abline(v = c(measures, measures))

# shading areas below arithmetic mean, above upper 0.01 point and in between
polygon(x = c(min(exposures), exposures[exposures <= measures], measures),
y = c(0, probabilities[exposures <= measures], 0),
border = NA,
alpha.f = 0.5))
polygon(x = c(measures, exposures[(exposures >= measures) & (exposures <= measures)], measures),
y = c(0, probabilities[(exposures >= measures) & (exposures <= measures)], 0),
border = NA,
alpha.f = 0.5))
polygon(x = c(measures, exposures[exposures >= measures], max(exposures)),
y = c(0, probabilities[exposures >= measures], 0),
border = NA,
alpha.f = 0.5))
`````` Created on 2020-04-11 by the reprex package (v0.3.0)

1 Like

I manage to plot using the following codes however I am failing to cut the `(abline = mu)` and `(abline = quan)` to stop at the distribution curve only

``````library(sn)
#>
#> Attaching package: 'sn'
#> The following object is masked from 'package:stats':
#>
#>     sd

Exposures <- seq(from = -2,
to = 4,
by = 0.01)
Probability <- dsn(x = Exposures,
xi = 0.1,
omega = 0.72,
alpha = 5)
plot(x = Exposures,
y = Probability,
main = "probability distribution of exposure, representing accounting CVA",
type = "l")
mu=mean(Exposures)
quan=quantile(Exposures,c(0.99))
abline(v = quan)
abline(v = mu)
polygon(x = c(min(Exposures), Exposures[Exposures < mu], mu),
y = c(0, Probability[Exposures < mu], 0),
border = NA,
alpha.f = 0.5)
)
``````

I have also tried to shade the area between the mean (mu) and quan with the following codes but seems not to be working properly.

``````polygon(x = c(mu,Exposures[Exposures < quan], mu),
y = c(0, Probability[Exposures < quan], 0),
border = NA,