I am searching a way to produce correlated random numbers sampled from several binomial distributions in JAGS.
In R, I use normal copula to correlate samples from several binomials, however, I do not know how to do that in JAGS.
Any help would be appreciated,
Last edit: Arnaud 2015-10-27
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
model{## Set up prior for bivariate normal.# Mean vectorzero[1]<-0zero[2]<-0# Variance-covariance matrixSigma.X[1,1]<-1Sigma.X[1,2]<-rhoSigma.X[2,1]<-rhoSigma.X[2,2]<-1# Precision matrixTau.X<-inverse(Sigma.X)## Correlated bivariate normalX[1:2]~dmnorm(zero,Tau.X)## Correlated uniformsp[1]<-pnorm(X[1],0,1)p[2]<-pnorm(X[2],0,1)## Set up breaks for transformation to binomialfor(iin1:N){breaks[i]<-pbin(i-1,prob,N)}## Transform to binomialY[1]~dinterval(p[1],breaks)Y[2]~dinterval(p[2],breaks)}
R script:
library(rjags)m<-jags.model("copula.bug",data=list(rho=0.5,N=5,prob=0.3))s<-coda.samples(m,"Y",n.iter=10000)[[1]]# Marginal distributions are correcttable(s[,1])table(s[,2])table(rbinom(10000,prob=0.3,size=5))# Binomials are correlatedcor(s[,1],s[,2])
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
size<-matrix(rep(50,15),nrow=3)prob<-matrix(rep(c(0.2,0.5,0.8),each=5),nrow=3,byrow=T)obsProb<-rep(0.8,5)library(rjags)m<-jags.model("C:/temp/modelCOPULA.bug",data=list(rho=0.7,size=size,prob=prob,obsProb=obsProb))s<-coda.samples(m,c("probSample"),n.iter=10000)[[1]]cor(s[,substring(colnames(s),1,13)=="probSample[2,"])#test correlation for year 2par(mfrow=c(2,2))hist(s[,substring(colnames(s),1,14)=="probSample[2,1"],breaks=20,xlim=c(0,1));hist(s[,substring(colnames(s),1,14)=="probSample[2,2"],breaks=20,xlim=c(0,1));hist(s[,substring(colnames(s),1,14)=="probSample[2,3"],breaks=20,xlim=c(0,1))
For each of my 5 years, the model gives me three proportions sampled from correlated binomials (each of them having a defined mean). The correlation between the binomial is given.
In the current version, the "fitting" portion of the code is not active. In this case, no problem, the correlation among the proportion sampled for the binomial correspond to the one given as rho (always just a little lower).
Now, here is my problem. In activating the "fitting" portion of the code, I include observed proportions for each year and I make a link with the sampled one (including a normal error). In that case, the correlation no longer exists (just run the code removing the comments in front of the "fitting" part and look at the correlation at year 2, for example).
How can I do to keep the correlation in that case ?
In fact, I thought that the fitting would have updated the information in the three binomials, but keeping the correlation.
Any idea ??
Arnaud
Last edit: Arnaud 2017-02-23
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Sorry to insist on this subject, but I hope finding a way to solve my problem.
Thanks to Martyn Plummer, I have a solution to produce correlated samples from binomials distributions (see above).
However, when fitting samples from one of the distributions to some "observed values", the correlation between binomials samples disappear.
In fact, it seems that the distribution of the binomial for which I provide "observed" values is updated (perfect), but samples from the other binomial distributions are no longer correlated.
Is their a way to maintain this correlation even when fitting to "observed" data?
Thanks
Arnaud
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Dear JAGS users,
I am searching a way to produce correlated random numbers sampled from several binomial distributions in JAGS.
In R, I use normal copula to correlate samples from several binomials, however, I do not know how to do that in JAGS.
Any help would be appreciated,
Last edit: Arnaud 2015-10-27
Something like this:
Model file "copula.bug"
R script:
Thanks Martyn, I will have to take time to study your answer !!
Cheers
Thanks a lot, it's exactly what I need.
Much appreciated !
Hi Martyn,
Long time since I tried to use your suggestion.
Here is a slightly modified version that fit my needs
The R script:
For each of my 5 years, the model gives me three proportions sampled from correlated binomials (each of them having a defined mean). The correlation between the binomial is given.
In the current version, the "fitting" portion of the code is not active. In this case, no problem, the correlation among the proportion sampled for the binomial correspond to the one given as rho (always just a little lower).
Now, here is my problem. In activating the "fitting" portion of the code, I include observed proportions for each year and I make a link with the sampled one (including a normal error). In that case, the correlation no longer exists (just run the code removing the comments in front of the "fitting" part and look at the correlation at year 2, for example).
How can I do to keep the correlation in that case ?
In fact, I thought that the fitting would have updated the information in the three binomials, but keeping the correlation.
Any idea ??
Arnaud
Last edit: Arnaud 2017-02-23
Dears JAGS users,
Sorry to insist on this subject, but I hope finding a way to solve my problem.
Thanks to Martyn Plummer, I have a solution to produce correlated samples from binomials distributions (see above).
However, when fitting samples from one of the distributions to some "observed values", the correlation between binomials samples disappear.
In fact, it seems that the distribution of the binomial for which I provide "observed" values is updated (perfect), but samples from the other binomial distributions are no longer correlated.
Is their a way to maintain this correlation even when fitting to "observed" data?
Thanks
Arnaud