{"id":799,"date":"2014-02-02T23:57:49","date_gmt":"2014-02-02T23:57:49","guid":{"rendered":"http:\/\/www.georg-hosoya.de\/wordpress\/?p=799"},"modified":"2014-02-03T12:05:48","modified_gmt":"2014-02-03T12:05:48","slug":"jags-as-a-tool-for-simulation-studies","status":"publish","type":"post","link":"https:\/\/www.georg-hosoya.de\/wordpress\/?p=799","title":{"rendered":"JAGS as a tool for simulation studies"},"content":{"rendered":"<p>In one of my last posts I hinted to the fact that JAGS could be used for simulation studies. Let&#8217;s have a look! So basically simulation studies are very useful if one wants to check if a model one is developing makes sense. The main aim is to assess, if the parameters one has plugged into a data simulation process based on the model could be reasonably recovered by an estimation process. In JAGS it is possible to simulate data given a model and a set of known parameters and to estimate the parameters given the model and the simulated data set. For today&#8217;s example I have chosen a simple psychometric latent variable measurement model from classical test theory, which is very well understood and can be easily estimated with most software capable of latent variable modeling. Here it is:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.georg-hosoya.de\/wordpress\/wp-content\/ql-cache\/quicklatex.com-dfb233815f9ec8c28db5f804799ae1d3_l3.png\" class=\"ql-img-inline-formula \" alt=\"&#92;&#109;&#117;&#95;&#123;&#49;&#105;&#125;&#61;&#92;&#108;&#97;&#109;&#98;&#100;&#97;&#95;&#49;&#92;&#99;&#100;&#111;&#116;&#92;&#101;&#116;&#97;&#95;&#105;&#43;&#92;&#98;&#101;&#116;&#97;&#95;&#49;\" title=\"Rendered by QuickLaTeX.com\" height=\"17\" width=\"129\" style=\"vertical-align: -4px;\"\/><br \/>\n<img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.georg-hosoya.de\/wordpress\/wp-content\/ql-cache\/quicklatex.com-f82ca7b28221b682b21960b04b2823ca_l3.png\" class=\"ql-img-inline-formula \" alt=\"&#92;&#109;&#117;&#95;&#123;&#50;&#105;&#125;&#61;&#92;&#108;&#97;&#109;&#98;&#100;&#97;&#95;&#50;&#92;&#99;&#100;&#111;&#116;&#92;&#101;&#116;&#97;&#95;&#105;&#43;&#92;&#98;&#101;&#116;&#97;&#95;&#50;\" title=\"Rendered by QuickLaTeX.com\" height=\"17\" width=\"130\" style=\"vertical-align: -4px;\"\/><br \/>\n<img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.georg-hosoya.de\/wordpress\/wp-content\/ql-cache\/quicklatex.com-cf7100b2b10c3a9ec5ea84be400d6202_l3.png\" class=\"ql-img-inline-formula \" alt=\"&#92;&#109;&#117;&#95;&#123;&#51;&#105;&#125;&#61;&#32;&#49;&#92;&#99;&#100;&#111;&#116;&#92;&#101;&#116;&#97;&#95;&#105;&#43;&#92;&#98;&#101;&#116;&#97;&#95;&#51;\" title=\"Rendered by QuickLaTeX.com\" height=\"16\" width=\"121\" style=\"vertical-align: -4px;\"\/>.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.georg-hosoya.de\/wordpress\/wp-content\/ql-cache\/quicklatex.com-91037644af2378cccd6647996f699397_l3.png\" class=\"ql-img-inline-formula \" alt=\"&#121;&#95;&#123;&#49;&#105;&#125;&#32;&#92;&#115;&#105;&#109;&#32;&#92;&#109;&#98;&#111;&#120;&#123;&#100;&#110;&#111;&#114;&#109;&#125;&#32;&#40;&#92;&#109;&#117;&#95;&#123;&#49;&#105;&#125;&#44;&#32;&#92;&#115;&#105;&#103;&#109;&#97;&#94;&#50;&#95;&#123;&#101;&#49;&#125;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"21\" width=\"163\" style=\"vertical-align: -6px;\"\/><br \/>\n<img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.georg-hosoya.de\/wordpress\/wp-content\/ql-cache\/quicklatex.com-f82e1c6bfe570ae105ca7a71f417eece_l3.png\" class=\"ql-img-inline-formula \" alt=\"&#121;&#95;&#123;&#50;&#105;&#125;&#32;&#92;&#115;&#105;&#109;&#32;&#92;&#109;&#98;&#111;&#120;&#123;&#100;&#110;&#111;&#114;&#109;&#125;&#32;&#40;&#92;&#109;&#117;&#95;&#123;&#50;&#105;&#125;&#44;&#32;&#92;&#115;&#105;&#103;&#109;&#97;&#94;&#50;&#95;&#123;&#101;&#50;&#125;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"20\" width=\"163\" style=\"vertical-align: -5px;\"\/><br \/>\n<img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.georg-hosoya.de\/wordpress\/wp-content\/ql-cache\/quicklatex.com-85ae9e0d5c3f715a5790d239c7084921_l3.png\" class=\"ql-img-inline-formula \" alt=\"&#121;&#95;&#123;&#51;&#105;&#125;&#32;&#92;&#115;&#105;&#109;&#32;&#92;&#109;&#98;&#111;&#120;&#123;&#100;&#110;&#111;&#114;&#109;&#125;&#32;&#40;&#92;&#109;&#117;&#95;&#123;&#51;&#105;&#125;&#44;&#32;&#92;&#115;&#105;&#103;&#109;&#97;&#94;&#50;&#95;&#123;&#101;&#51;&#125;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"20\" width=\"163\" style=\"vertical-align: -5px;\"\/><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.georg-hosoya.de\/wordpress\/wp-content\/ql-cache\/quicklatex.com-b9fa81ecb73e5567a8fe1ca4a0aaa3a6_l3.png\" class=\"ql-img-inline-formula \" alt=\"&#92;&#101;&#116;&#97;&#95;&#105;&#32;&#92;&#115;&#105;&#109;&#32;&#92;&#109;&#98;&#111;&#120;&#123;&#100;&#110;&#111;&#114;&#109;&#125;&#32;&#40;&#48;&#44;&#32;&#92;&#115;&#105;&#103;&#109;&#97;&#94;&#50;&#95;&#123;&#92;&#101;&#116;&#97;&#125;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"22\" width=\"136\" style=\"vertical-align: -7px;\"\/><\/p>\n<p>The basic idea is that a latent trait <img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.georg-hosoya.de\/wordpress\/wp-content\/ql-cache\/quicklatex.com-2331754e034d6688262b1d58109022be_l3.png\" class=\"ql-img-inline-formula \" alt=\"&#92;&#101;&#116;&#97;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"9\" style=\"vertical-align: -4px;\"\/> is measured by three items (<img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.georg-hosoya.de\/wordpress\/wp-content\/ql-cache\/quicklatex.com-6e60d2ffa05dc6b07fb1bde932f61d98_l3.png\" class=\"ql-img-inline-formula \" alt=\"&#121;&#95;&#123;&#49;&#105;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"21\" style=\"vertical-align: -4px;\"\/> to <img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.georg-hosoya.de\/wordpress\/wp-content\/ql-cache\/quicklatex.com-9e5acc1eb6ff3df00ae9a305ee47ca58_l3.png\" class=\"ql-img-inline-formula \" alt=\"&#121;&#95;&#123;&#51;&#105;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"21\" style=\"vertical-align: -4px;\"\/>) with different difficulties (<img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.georg-hosoya.de\/wordpress\/wp-content\/ql-cache\/quicklatex.com-b4afa35c1c4f5678e57a348a7c834dd6_l3.png\" class=\"ql-img-inline-formula \" alt=\"&#92;&#98;&#101;&#116;&#97;&#95;&#49;\" title=\"Rendered by QuickLaTeX.com\" height=\"16\" width=\"16\" style=\"vertical-align: -4px;\"\/> to <img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.georg-hosoya.de\/wordpress\/wp-content\/ql-cache\/quicklatex.com-044c3c7f8435a0c7216487b230f83451_l3.png\" class=\"ql-img-inline-formula \" alt=\"&#92;&#98;&#101;&#116;&#97;&#95;&#51;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"24\" style=\"vertical-align: -4px;\"\/>, different loadings (<img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.georg-hosoya.de\/wordpress\/wp-content\/ql-cache\/quicklatex.com-bcfeec1a7b945d5241e2fb092e1b2dd5_l3.png\" class=\"ql-img-inline-formula \" alt=\"&#92;&#108;&#97;&#109;&#98;&#100;&#97;&#95;&#49;\" title=\"Rendered by QuickLaTeX.com\" height=\"17\" width=\"16\" style=\"vertical-align: -4px;\"\/> and <img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.georg-hosoya.de\/wordpress\/wp-content\/ql-cache\/quicklatex.com-6b004891da8c3e035b4bd8120d6d65a2_l3.png\" class=\"ql-img-inline-formula \" alt=\"&#92;&#108;&#97;&#109;&#98;&#100;&#97;&#95;&#50;\" title=\"Rendered by QuickLaTeX.com\" height=\"16\" width=\"17\" style=\"vertical-align: -3px;\"\/>) and different measurement error variances (<img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.georg-hosoya.de\/wordpress\/wp-content\/ql-cache\/quicklatex.com-7a97eb034d60152b68aa9d384081daac_l3.png\" class=\"ql-img-inline-formula \" alt=\"&#92;&#115;&#105;&#103;&#109;&#97;&#94;&#50;&#95;&#123;&#101;&#49;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"21\" width=\"22\" style=\"vertical-align: -6px;\"\/> to <img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.georg-hosoya.de\/wordpress\/wp-content\/ql-cache\/quicklatex.com-d69dca4b3847aa5220580d5bb75f6216_l3.png\" class=\"ql-img-inline-formula \" alt=\"&#92;&#115;&#105;&#103;&#109;&#97;&#94;&#50;&#95;&#123;&#101;&#51;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"20\" width=\"23\" style=\"vertical-align: -5px;\"\/>). For identification reasons, the loading of item 3 is set to 1. Furher the variance of the trait (<img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.georg-hosoya.de\/wordpress\/wp-content\/ql-cache\/quicklatex.com-73271157845753b2b9923d26672e3d9e_l3.png\" class=\"ql-img-inline-formula \" alt=\"&#92;&#115;&#105;&#103;&#109;&#97;&#94;&#50;&#95;&#123;&#92;&#101;&#116;&#97;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"22\" width=\"18\" style=\"vertical-align: -7px;\"\/>) has to be estimated from data.<\/p>\n<p>For simulating data with JAGs we have to specify a data block. The model specification is straight forward, note that we have to hand over the true parameters to JAGS, for example with an R script. This is pretty handy because one could design a thorough simulation study for different sample size and parameter combinations. The estimation part has to be specfied in a model block.<\/p>\n<pre lang=\"rsplus\" line=\"1\">\r\n#Simulation\r\ndata{\r\n  \r\n  for(i in 1:n)\r\n  {\r\n      y1[i]~dnorm(mu1.true[i], tau.y1.true);\r\n      y2[i]~dnorm(mu2.true[i], tau.y2.true);\r\n      y3[i]~dnorm(mu3.true[i], tau.y3.true);\r\n      \r\n      mu1.true[i]<-lambda1.true*eta.true[i]+beta1.true\r\n      mu2.true[i]<-lambda2.true*eta.true[i]+beta2.true\r\n      mu3.true[i]<-eta.true[i]+beta3.true\r\n  }\r\n  \r\n  tau.y1.true<-pow(sigma.y1.true, -2); \r\n  tau.y2.true<-pow(sigma.y2.true, -2);\r\n  tau.y3.true<-pow(sigma.y3.true, -2);\r\n\r\n}\r\n\r\n# Estimation\r\nmodel{\r\n  \r\n  for(i in 1:n)\r\n  {\r\n    y1[i]~dnorm(mu1[i], tau.y1);\r\n    y2[i]~dnorm(mu2[i], tau.y2);\r\n    y3[i]~dnorm(mu3[i], tau.y3);\r\n    \r\n    mu1[i]<-lambda1*eta[i]+beta1\r\n    mu2[i]<-lambda2*eta[i]+beta2\r\n    mu3[i]<-eta[i]+beta3\r\n  }\r\n  \r\n  for(i in 1:n)\r\n  {\r\n    eta[i]~dnorm(0,tau.eta);\r\n  }\r\n  \r\n  # Priors for the loadings\r\n  lambda1~dnorm(0,1.0E-3)\r\n  lambda2~dnorm(0,1.0E-3)\r\n  bias_lambda1<-lambda1-lambda1.true\r\n\r\n  # Priors for the easiness parameters\r\n  beta1~dnorm(0,1.0E-3)\r\n  beta2~dnorm(0,1.0E-3)\r\n  beta3~dnorm(0,1.0E-3)\r\n  bias_beta1<-beta1-beta1.true\r\n\r\n  # Hyperprior for latent trait distribution\r\n  tau.eta<-pow(sigma.eta,-2)\r\n  sigma.eta~dunif(0,10) \r\n\r\n  # Priors for the measurement errors\r\n   tau.y1<-pow(sigma.y1, -2); \r\n   tau.y2<-pow(sigma.y2, -2);\r\n   tau.y3<-pow(sigma.y3, -2);\r\n   sigma.y1~dunif(0,10);\r\n   sigma.y2~dunif(0,10); \r\n   sigma.y3~dunif(0,10); \r\n}\r\n<\/pre>\n<p>This may look a bit daunting, but it really took only 15 minutes to set that up.<\/p>\n<p>The simulation is controlled by an R script, which hands over the true parameters to JAGS:<\/p>\n<pre lang=\"rsplus\" line=\"1\">\r\nlibrary(R2jags)\r\nlibrary(mcmcplots)\r\n\r\nn=500 # number of subjects\r\n\r\n# generate 'true' latent trait values\r\neta.true<-rnorm(n, 0, 4)\r\n# generate 'true' factor loadings\r\nlambda1.true=1\r\nlambda2.true=0.5\r\n\r\nbeta1.true=1\r\nbeta2.true=2\r\nbeta3.true=3\r\n\r\n# generate 'true' error standard deviations\r\nsigma.y1.true=1\r\nsigma.y2.true=0.5\r\nsigma.y3.true=0.75\r\n\r\ndata<-list(\"n\", \"eta.true\", \"lambda1.true\", \"lambda2.true\", \"sigma.y1.true\", \"sigma.y2.true\", \"sigma.y3.true\",\r\n           \"beta1.true\", \"beta2.true\", \"beta3.true\")\r\n\r\nparameters<-c(\"lambda1\",\"lambda2\", \"sigma.eta\", \"sigma.y1\", \"sigma.y2\", \"sigma.y3\", \r\n              \"beta1\", \"beta2\", \"beta3\", \"bias_beta1\")\r\n\r\noutput<-jags.parallel(data, inits= NULL, parameters, model.file=\"simple_1f.txt\",n.chains=2, n.iter=35000, n.burnin=30000)\r\nattach.jags(output)\r\ndenplot(output)\r\ntraplot(output)\r\nplot(output)\r\n <\/pre>\n<p>If we inspect the posterior densities based on <em>one<\/em> dataset with sample size 500 we see that we pretty much hit the spot. That is, all posterior densities are covering the generating true parameters.<br \/>\n<a href=\"http:\/\/www.georg-hosoya.de\/wordpress\/wp-content\/uploads\/2014\/02\/JAGS_sim.jpeg\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.georg-hosoya.de\/wordpress\/wp-content\/uploads\/2014\/02\/JAGS_sim.jpeg\" alt=\"JAGS_sim\" width=\"564\" height=\"519\" class=\"alignnone size-full wp-image-818\" srcset=\"https:\/\/www.georg-hosoya.de\/wordpress\/wp-content\/uploads\/2014\/02\/JAGS_sim.jpeg 564w, https:\/\/www.georg-hosoya.de\/wordpress\/wp-content\/uploads\/2014\/02\/JAGS_sim-300x276.jpeg 300w, https:\/\/www.georg-hosoya.de\/wordpress\/wp-content\/uploads\/2014\/02\/JAGS_sim-326x300.jpeg 326w\" sizes=\"(max-width: 564px) 100vw, 564px\" \/><\/a><br \/>\nPlease note that in the estimation code a posterior density for the difference between the data generating parameter <img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.georg-hosoya.de\/wordpress\/wp-content\/ql-cache\/quicklatex.com-b4afa35c1c4f5678e57a348a7c834dd6_l3.png\" class=\"ql-img-inline-formula \" alt=\"&#92;&#98;&#101;&#116;&#97;&#95;&#49;\" title=\"Rendered by QuickLaTeX.com\" height=\"16\" width=\"16\" style=\"vertical-align: -4px;\"\/> and its' samples from the posterior is specified. The zero is very well covered, but keep in mind that this is based only on <em>one<\/em> dataset. To replicate a more traditional approach to bias, a viable route could be to simulate several hundreds of datasets and to record for example the posterior mean for each parameter and data set to assess bias. <\/p>\n<p>As always, keep in mind that this is pretty experimental code and estimating latent variable models with JAGS is not very well explored yet as far as I know. However, the main point of this post was to show how JAGS could be used in combination with R to run simulation studies in a probabilistic graphical framework. Should work for multilevel models or generalized mixed models too.<\/p>\n<p>Edit: I just stumbled about this <a href=\"http:\/\/robertgrantstats.wordpress.com\/2014\/01\/31\/need-to-do-a-simulation-study-on-a-bayesian-model-use-stan\/\">recent blog post by Robert Grand<\/a>. So <a href=\"http:\/\/mc-stan.org\/\">stan<\/a> should work too. Interesting!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In one of my last posts I hinted to the fact that JAGS could be used for simulation studies. Let&#8217;s have a look! So basically simulation studies are very useful if one wants to check if a model one is &hellip; <a href=\"https:\/\/www.georg-hosoya.de\/wordpress\/?p=799\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.georg-hosoya.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/799"}],"collection":[{"href":"https:\/\/www.georg-hosoya.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.georg-hosoya.de\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.georg-hosoya.de\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.georg-hosoya.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=799"}],"version-history":[{"count":31,"href":"https:\/\/www.georg-hosoya.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/799\/revisions"}],"predecessor-version":[{"id":832,"href":"https:\/\/www.georg-hosoya.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/799\/revisions\/832"}],"wp:attachment":[{"href":"https:\/\/www.georg-hosoya.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.georg-hosoya.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.georg-hosoya.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}