Modelling Dependence with Copulas in R(非译文,纯水)

原文地址:
https://www.r-bloggers.com/modelling-dependence-with-copulas-in-r/


This post interprets the logic of Copulas using only MASS package. The author uses a good plotting package to show a very intuitive result and process of copula, while he also explain why copulas work.

The codes are written in R and simple, as can be seen below.

library(MASS)
set.seed(100)

m <- 3
n <- 2000
sigma <- matrix(c(1, 0.4, 0.2,
0.4, 1, 0.8,
0.2, -0.8, 1),
nrow = 3)
z <- mvrnorm(n, mu = rep(0, m), Sigma = sigma, empirical = T)
# rep 0 m times has (0,0,0) which is the mean of variables

library(psych)
cor(z, method = "spearman")
pairs.panels(z)

u <- pnorm(z)
pairs.panels(u)

library(rgl)
plot3d(u[,1], u[,2], u[,3], pch = 20, col = 'navyblue')

x1 <- qgamma(u[,1], shape = 2, scale = 1)
x2 <- qbeta(u[,2], 2, 2)
x3 <- qt(u[,3], df = 5)
plot3d(x1, x2, x3, pch = 20, col = "blue")

df <- cbind(x1, x2, x3)
pairs.panels(df)
cor(df, meth = "spearman")

library(copula)
set.seed(100)
myCop <- normalCopula(param = c(0.4, 0.2, 0.8), dim = 3, dispstr = "un")
myMvd <- mvdc(copula = myCop, margins = c("gamma", "beta", "t"),
paramMargins = list(list(shape = 2, scale = 1),
list(shape1 = 2, shape2 = 2),
list(df = 5)))
Z2 <- rMvdc(2000, myMvd)
colnames(Z2) <- c("x1", "x2", "x3")
pairs.panels(Z2)

Modelling Dependence with Copulas in R(非译文,纯水)_第1张图片
Modelling Dependence with Copulas in R(非译文,纯水)_第2张图片
Modelling Dependence with Copulas in R(非译文,纯水)_第3张图片
Modelling Dependence with Copulas in R(非译文,纯水)_第4张图片
Modelling Dependence with Copulas in R(非译文,纯水)_第5张图片
Modelling Dependence with Copulas in R(非译文,纯水)_第6张图片

Then the author does exactly the same procedure using copula package.

Above is the warm up.

Then two data sets is used to do a copula analysis.

Detailed information can be found in copula-4.r file.

你可能感兴趣的:(Modelling Dependence with Copulas in R(非译文,纯水))