Copula导致R

时间:2019-07-30 19:55:44

标签: r correlation cran probability-distribution

我有一个两列的表,它包含一个已经计算出的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包。

我用来确定要使用哪个Copula系列的以下VineCopula代码:

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

根据copula R手册(Link),建议使用生存的Gumbel,即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)

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

在这里,我想通过在同一条线图中将原始数据应用于坦率的copula来显示相关值, 我不知道如何提取坦率的copula结果? (只有一列,因此我可以绘制原始数据并进行视觉比较)

1 个答案:

答案 0 :(得分:0)

我不确定我是否正确理解您的问题。但是,如果要获取(从Frank copula生成的)关联数据,它们将存储在sim中。如果您要输入Kendall头,则应将其存储在fitcopula中。您不能将坦率的copula数据作为一列,因为它必须是矩阵。同样,pobs函数将为您提供矩阵结果,因此您无需使用as.matrix。如果您需要更多帮助,我们非常乐意为您提供帮助。