首页 > 解决方案 > Copula导致R

问题描述

我有一个两列的表,它包含一个已经计算的 2 个变量的索引,一个简单的引用如下:

 V1, V2
 0.46,1.08
 0.84,1.05
-0.68,0.93
-0.99,0.68
-0.87,0.30
-1.08,-0.09
-1.16,-0.34
-0.61,-0.43
-0.65,-0.48
 0.73,-0.48

为了找出上述数据之间的相关性,我使用了 R 中的 copula 包。

下面的 VineCopula 代码我用来确定使用哪个 Copula 家族:

library(VineCopula)
selectedCopula <- BiCopSelect(u,v,familyset=NA)
selectedCopula

它建议使用生存 Gumbel,根据 copula R 手册(链接)的 Gumbel Copula 的旋转版本

但是,我选择了 Frank copula,因为它提供了对称的依赖结构,并且它允许将数据中的正依赖建模为负依赖,这有多合理?

还有一件事,在运行以下自我解释的 copula 代码后:


# Estimate V1 distribution parameters and visually compare simulated vs observed data
x_mean <- mean(mydata$V1)
#Normal Distribution
hist(mydata$V1, breaks = 20, col = "green", density = 30)
hist(rnorm( nrow(mydata), mean = x_mean, sd = sd(mydata$V1)), 
breaks = 20,col = "blue", add = T, density = 30, angle = -45)

# Same for V2
y_mean <- mean(mydata$V2)
#Normal Distribution
hist(mydata$V2, breaks = 20, col = "green", density = 30)
hist(rnorm(nrow(mydata), mean = y_mean,sd = sd(mydata$V2)), 
breaks = 20, col = "blue", add = T, density = 30, angle = -45)


# Measure association using Kendall's Tau
cor(mydata, method = "kendall")


#Fitting process with copula choice
# Estimate copula parameters
cop_model <- frankCopula(dim = 2)
m <- pobs(as.matrix(mydata))
fit <- fitCopula(cop_model, m, method = 'ml')
coef(fit)

# Check Kendall's tau value for the frank copula with  = 3.236104 
tau(frankCopula(param = 3.23))

#Building the bivariate distribution using frank copula

# Build the bivariate distribution
sdx =sd(mydata$V1)
sdy =sd(mydata$V2)
my_dist <- mvdc(frankCopula(param = 3.23, dim = 2), margins = c("norm","norm"), 
                paramMargins = list(list(mean = x_mean, sd=sdx), 
                                    list(mean = y_mean, sd=sdy)))

# Generate 439 random sample observations from the multivariate distribution
v <- rMvdc(439, my_dist)
# Compute the density
pdf_mvd <- dMvdc(v, my_dist)
# Compute the CDF
cdf_mvd <- pMvdc(v, my_dist)

# Sample 439 observations from the distribution
sim <- rMvdc(439,my_dist)

# Plot the data for a visual comparison
plot(mydata$V1, mydata$V2, main = 'Test dataset x and y', col = "blue")
points(sim[,1], sim[,2], col = 'red')
legend('bottomright', c('Observed', 'Simulated'), col = c('blue', 'red'), pch=21)

即使对于极值,绘制的数据集也显示出良好的拟合结果。

在这里,我想在同一个折线图中显示应用 frank copula 和原始数据的相关值,我不知道如何提取 frank copula 结果?(单列,以便我可以使用原始数据进行绘图并进行视觉比较)

标签: rcorrelationcranprobability-distribution

解决方案


我不确定我是否正确理解了您的问题。但是,如果您想获取 copula 数据(从 Frank copula 生成),它们存储在sim. 如果您要求Kendalltau,那么它们应该存储在fitcopula. 您不能将坦率的 copula 数据作为一列,因为它必须是矩阵。此外,pobs函数会给你一个矩阵的结果,所以你不需要使用as.matrix. 如果您需要更多帮助,我很乐意提供帮助。


推荐阅读