r - 创建遵循各种分布的相关变量

标签 r math distribution correlation covariance

问题

在 R 中,我想创建 n长度变量 L哪个关系由 correlation matrix 给出叫 cor_matrix .重要的一点是n变量可能遵循不同的分布(包括连续分布与离散分布)。

相关帖子

  • how-to-generate-sample-data-with-exact-moments
  • generate-a-random-variable-with-a-defined-correlation-to-an-existing-variable
  • r-constructing-correlated-variables

  • 修改自 third post以上列出,以下是所有 n 时的解决方案变量是连续的并且来自相同的分布。
    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)
    

    enter image description here

    但是,不能创建具有以下分布的相关变量
    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)
    

    enter image description here

    最佳答案

    按照@NatePope 和@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))
    

    enter image description here

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

    关于r - 创建遵循各种分布的相关变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32365016/

    相关文章:

    algorithm - 找到点的质心

    r - 将列添加到 R 中的空数据框中

    python - 我们可以提供一个自定义指标,用于与 H2O 中的 GLM 进行交叉验证吗?

    r - R 中 Shiny : Is it possible to output a color using renderText?

    c# - 如何将整数四舍五入到百位?

    java - 在 Java 中如何舍入到最接近的 20 的倍数?

    r - 将一个列表映射到另一个列表

    machine-learning - 两个具有不同分布的数据集的含义以及为什么神经网络不能一起处理它们?

    matplotlib - 使用比数据点更少的标记进行绘图(或绘制 CDF 的更好方法?)[matplotlib,或一般绘图帮助]

    c# - 最佳 WinForms 分发计划