简体   繁体   English

基于Winbugs / JAGS脚本在R中模拟多元分布中的数据

[英]Simulating data from multivariate distribution in R based on Winbugs/JAGS script

I am trying to simulate data, based on part of a JAGS/Winbugs script. 我正在尝试基于JAGS / Winbugs脚本的一部分模拟数据。 The script comes from Eaves & Erkanli (2003, see, http://psych.colorado.edu/~carey/pdffiles/mcmc_eaves.pdf , page 295-296). 该脚本来自Eaves&Erkanli(2003年,请参阅http://psych.colorado.edu/~carey/pdffiles/mcmc_eaves.pdf ,第295-296页)。

The (part of) the script I want to base my simulations on is as follows (different variable names than in the original paper): 我要作为模拟基础的脚本(部分)如下(变量名与原始论文中的不同):

 for(fam in 1 : nmz ){
    a2mz[fam, 1:N] ~ dmnorm(mu[1:N], tau.a[1:N, 1:N])
    a1mz[fam, 1:N] ~ dmnorm(a2mz[fam, 1:N], tau.a[1:N, 1:N])
 }

 #Prior
 tau.a[1:N, 1:N] ~ dwish(omega.g[,], N) 

I want to simulate data in R for the parameters a2mz and a1mz as given in the script above. 我想在R中针对上面脚本中给出的参数a2mz和a1mz模拟数据。

So basically, I want to simualte data from -N- (eg = 3) multivariate distributions with -fam- (eg 10) persons with sigma tau.a. 因此,基本上,我想与-fam-(例如10个)sigma tau.a的人同时模拟-N-(例如= 3)个多元分布的数据。

To make this more illustrative: The purpose is to simulate genetic effects for -fam- (eg 10) families. 为了使其更具说明性:目的是模拟-fam-(例如10个)家族的遗传效应。 The genetic effect is the same for each family (eg monozygotic twins), with a variance of tau.a (eg 0.5). 每个家庭的遗传效应是相同的(例如,单卵双胞胎),tau.a的变异(例如0.5)。 Of these genetic effects, 3 'versions' (3 multivariate distributions) have to be simulated. 在这些遗传效应中,必须模拟3个“版本”(3个多元分布)。

What I tried in R to simulate the data as given in the JAGS/Winbugs script is as follows: 我在R中尝试模拟JAGS / Winbugs脚本中给出的数据的方法如下:

 library(MASS)
 nmz = 10 #number of families, here e.g. 10
 var_a = 0.5 #tau.g in the script

 a2_mz <- mvrnorm(3, mu = rep(0, nmz), Sigma = diag(nmz)*var_a)

This simulates data for the a2mz parameter as referred to in the JAGS/Winbugs script above: 这将模拟上面的JAGS / Winbugs脚本中提到的a2mz参数的数据:

 > print(t(a2_mz))
        [,1]       [,2]        [,3]
  [1,] -1.1563683 -0.4478091 -0.15037563
  [2,]  0.5673873 -0.7052487  0.44377336
  [3,]  0.2560446  0.9901964 -0.65463341
  [4,] -0.8366952  0.4924839 -0.56891991
  [5,]  0.7343780  0.5429955  0.87529201
  [6,]  0.5592868 -0.3899988 -0.33709105
  [7,] -1.8233663 -0.7149141 -0.18153049
  [8,] -0.8213804 -1.4397075 -0.09159725
  [9,] -0.7002797 -0.3996970 -0.29142215
  [10,]  1.1084067  0.3884869 -0.46207940 

However, when I then try to use these data to simulate data for the a1mz (third line of the JAGS/Winbugs) script, then something goes wrong and I am not sure what: 但是,当我随后尝试使用这些数据来模拟a1mz(JAGS / Winbugs的第三行)脚本的数据时,出现了问题,我不确定是什么:

 a1_mz <- mvrnorm(3, mu = t(a2_mz), Sigma = c(diag(nmz)*var_a, diag(nmz)*var_a,     diag(nmz)*var_a))

This results in the error: Error in eigen(Sigma, symmetric = TRUE, EISPACK = EISPACK) : non-square matrix in 'eigen' 这导致错误:本征错误(Sigma,对称= TRUE,EISPACK = EISPACK):“本征”中的非平方矩阵

Can anyone give me any hints or tips on what I am doing wrong? 谁能给我有关我做错事情的任何提示或技巧?

Many thanks, Best regards, inga 非常感谢,最诚挚的问候,印加

mvrnorm() takes a mean-vector and a variance matrix as input, and that's not what you're feeding it. mvrnorm()将均值向量和方差矩阵作为输入,这不是您要提供的内容。 I'm not sure I understand your question, but if you want to simulate 3 samples from 3 different multivariate normal distributions with same variance and different mean. 我不确定我是否理解您的问题,但是如果您想模拟来自3个具有相同方差和均值的3个不同多元正态分布的样本。 Then just use: 然后使用:

a1_mz<-array(dim=c(dim(a2_mz),3))
for(i in 1:3)  a1_mz[,,i]<-mvrnorm(3,t(a2_mz)[,i],diag(nmz)*var_a)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM