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.
Can anyone please help
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()
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
# 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)
#> Loading required package: stats4
#>
#> 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,
col = adjustcolor(col = "blue",
alpha.f = 0.5))
If this does not help, please provide a reproducible example. And please take a look here:
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 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)
#> Loading required package: stats4
#>
#> 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[1], measures[2]))
# shading areas below arithmetic mean, above upper 0.01 point and in between
polygon(x = c(min(exposures), exposures[exposures <= measures[1]], measures[1]),
y = c(0, probabilities[exposures <= measures[1]], 0),
border = NA,
col = adjustcolor(col = "red",
alpha.f = 0.5))
polygon(x = c(measures[1], exposures[(exposures >= measures[1]) & (exposures <= measures[2])], measures[2]),
y = c(0, probabilities[(exposures >= measures[1]) & (exposures <= measures[2])], 0),
border = NA,
col = adjustcolor(col = "green",
alpha.f = 0.5))
polygon(x = c(measures[2], exposures[exposures >= measures[2]], max(exposures)),
y = c(0, probabilities[exposures >= measures[2]], 0),
border = NA,
col = adjustcolor(col = "blue",
alpha.f = 0.5))