按照各种分布创建相关变量 [英] Create correlated variables following various distributions

查看:67
本文介绍了按照各种分布创建相关变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

在R中,我想创建长度为Ln变量,其关系由

In R, I would like to create n variables of length L which relationship is given by a correlation matrix called cor_matrix. The important point is that the n variables may follow different distributions (including continuous vs discrete distributions).

相关帖子

  1. how-to-generate-sample-data-with-exact-moments> how-to-generate-sample-data-精确时刻

生成一个具有与现有变量的已定义关联的随机变量

r-constructing-correlated-variables

根据上面列出的第三篇文章进行了修改,以下是在所有变量是连续的,并且来自相同的分布.

Modified from the third post listed above, the following is a solution whenever all n variables are continuous and come from the same distribution.

library(psych) 

set.seed(199)

fun = function(cor_matrix, list_distributions, L)
{
    n = length(list_distributions)
    if (ncol(cor_matrix) != nrow(cor_matrix)) stop("cor_matrix is not square")
    if (nrow(cor_matrix) != n) stop("the length of list_distributions should match the number of columns and rows of cor_matrix")
    if (L<=1) stop("L should be > 1")

    fit = principal(cor_matrix, nfactors=n, rotate="none")
    loadings = matrix(fit$loadings[1:n, 1:n], nrow=n,ncol=n,byrow=F)
    cases = t(sapply(1:n, FUN=function(i, L) list_distributions[[i]](L), L=L))
    multivar = loadings %*% cases
    T_multivar = t(multivar)
    vars=as.data.frame(T_multivar)
    return(vars)
}

L = 1000
cor_matrix =  matrix(c (1.00, 0.90, 0.20 ,
                     0.90, 1.00, 0.40 ,
                     0.20, 0.40, 1.00), 
                  nrow=3,ncol=3,byrow=TRUE)

list_distributions = list(function(L)rnorm(L,0,2), function(L)rnorm(L,10,10), function(L) rnorm(L,0,1))
vars = fun(cor_matrix, list_distributions, L)
cor(vars)
plot(vars)

但是,不能创建具有以下分布的相关变量

However, one cannot create correlated variables with the following distributions

list_distributions = list(function(L)rnorm(L,0,2), function(L)round(rnorm(L,10,10)), function(L) runif(L,0,1))
vars = fun(cor_matrix, list_distributions, L)
cor(vars)
plot(vars)

推荐答案

按照@NatePope和@ JoshO'Brien的建议使用copulas

Using copulas as suggested by @NatePope and @JoshO'Brien

library(mvtnorm)

set.seed(199)

fun = function(cor_matrix, list_distributions, L)
{
    n = length(list_distributions)
    # Correlated Gaussian variables
    Gauss = rmvnorm(n=L, mean = rep(0,n), sig=cor_matrix)
    # convert them to uniform distribution.
    Unif = pnorm(Gauss) 
    # Convert them to whatever I want
    vars = sapply(1:n, FUN = function(i) list_distributions[[i]](Unif[,i]))
    return(vars)
}

L = 2000
cor_matrix =  matrix(c (1.00, 0.90, 0.80 ,
                     0.90, 1.00, 0.6,
                     0.80, 0.6, 1.00), 
                  nrow=3,ncol=3,byrow=TRUE)

list_distributions = list(function(L) qpois(L,7), function(L) round(qnorm(L,100,10)), function(L) qnorm(L,-100,1))

vars = fun(cor_matrix, list_distributions, L)
cor(vars)
plot(as.data.frame(vars))

此解决方案的默认设置是创建相关的正态分布变量,然后再将它们转换为均匀分布的变量.可能有一种性能更高的解决方案,可以直接创建均匀分布的相关变量.

This solution has the default of creating correlated normally distributed variables to transform them to uniformly distributed variables afterward. There is probably a more performant solution that would directly create uniformly distributed correlated variables.

这篇关于按照各种分布创建相关变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆