{"action":"created","issue":{"url":"https://api.github.com/repos/nutterb/HydeNet/issues/58","labels_url":"https://api.github.com/repos/nutterb/HydeNet/issues/58/labels{/name}","comments_url":"https://api.github.com/repos/nutterb/HydeNet/issues/58/comments","events_url":"https://api.github.com/repos/nutterb/HydeNet/issues/58/events","html_url":"https://github.com/nutterb/HydeNet/issues/58","id":77058539,"number":58,"title":"Multidimensional Arrays for Discrete CPTs in JAGS","user":{"login":"jarrod-dalton","id":11472793,"avatar_url":"https://avatars.githubusercontent.com/u/11472793?v=3","gravatar_id":"","url":"https://api.github.com/users/jarrod-dalton","html_url":"https://github.com/jarrod-dalton","followers_url":"https://api.github.com/users/jarrod-dalton/followers","following_url":"https://api.github.com/users/jarrod-dalton/following{/other_user}","gists_url":"https://api.github.com/users/jarrod-dalton/gists{/gist_id}","starred_url":"https://api.github.com/users/jarrod-dalton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jarrod-dalton/subscriptions","organizations_url":"https://api.github.com/users/jarrod-dalton/orgs","repos_url":"https://api.github.com/users/jarrod-dalton/repos","events_url":"https://api.github.com/users/jarrod-dalton/events{/privacy}","received_events_url":"https://api.github.com/users/jarrod-dalton/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/nutterb/HydeNet/labels/enhancement","name":"enhancement","color":"84b6eb"}],"state":"open","locked":false,"assignee":{"login":"nutterb","id":1848445,"avatar_url":"https://avatars.githubusercontent.com/u/1848445?v=3","gravatar_id":"","url":"https://api.github.com/users/nutterb","html_url":"https://github.com/nutterb","followers_url":"https://api.github.com/users/nutterb/followers","following_url":"https://api.github.com/users/nutterb/following{/other_user}","gists_url":"https://api.github.com/users/nutterb/gists{/gist_id}","starred_url":"https://api.github.com/users/nutterb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nutterb/subscriptions","organizations_url":"https://api.github.com/users/nutterb/orgs","repos_url":"https://api.github.com/users/nutterb/repos","events_url":"https://api.github.com/users/nutterb/events{/privacy}","received_events_url":"https://api.github.com/users/nutterb/received_events","type":"User","site_admin":false},"milestone":null,"comments":5,"created_at":"2015-05-16T15:12:43Z","updated_at":"2015-05-29T21:51:01Z","closed_at":null,"body":"Here is a sample JAGS script that uses a multidimensional array object fed from `rjags`:\r\n\r\n```\r\nmodel{\r\n  # Likelihood:\r\n  di1 ~ dcat(priorProbs)\r\n  di2 ~ dcat(priorProbs)\r\n\r\n  for ( i in 1:dim1 ) {\r\n    for( j in 1:dim2 ){\r\n      di3[i,j] ~ dcat( probArray[i,j,] )\r\n    }\r\n  }\r\n}\r\n```\r\n\r\nHere is the R code used to generate the list object fed to JAGS via `rjags`:\r\n\r\n```\r\nn <- 50000 \r\ndata <- data.frame(\r\n  di1=as.factor(1:6 %*% rmultinom(n,1,prob=c(.4,.3,.15,.10,.03,.02))),\r\n  di2=as.factor(1:6 %*% rmultinom(n,1,prob=rev(c(.4,.3,.15,.10,.03,.02)))),\r\n  di3=as.factor(1:6 %*% rmultinom(n,1,prob=c(.15,.10,.02,.3,.4,.03)))\r\n)\r\ncpt <- HydeNet::cpt(di3 ~ di1 + di2, data)\r\n\r\n\r\ndataList = list(\r\n    dim1 = 6,\r\n    dim2 = 6,\r\n    priorProbs = c(rep(.001->ppp,5),1-ppp),  #rep(1,6),\r\n    probArray = cpt\r\n)\r\n```\r\n\r\n(I made the probabilities heavily skewed to di1=6 and di2=6 to check results.) Here is some of the `coda.samples()` posterior matrix:\r\n\r\n```\r\n     di1 di2 di3[1,1] di3[2,1] di3[3,1] di3[4,1] di3[5,1] di3[6,1] di3[1,2]\r\n[1,]   6   6        1        5        5        4        1        4        4\r\n[2,]   6   6        4        4        5        5        1        4        5\r\n[3,]   6   6        2        5        4        1        5        4        5\r\n[4,]   6   6        5        1        4        2        4        5        4\r\n[5,]   6   6        4        2        1        5        4        1        4\r\n[6,]   6   6        5        4        1        4        1        4        3\r\n     di3[2,2] di3[3,2] di3[4,2] di3[5,2] di3[6,2] di3[1,3] di3[2,3] di3[3,3]\r\n[1,]        5        5        3        5        2        4        1        2\r\n[2,]        5        5        2        5        2        3        1        5\r\n[3,]        1        2        5        1        1        2        5        5\r\n[4,]        5        5        1        4        2        2        4        4\r\n[5,]        5        2        2        5        4        4        2        4\r\n[6,]        2        6        2        4        1        4        5        1\r\n     di3[4,3] di3[5,3] di3[6,3] di3[1,4] di3[2,4] di3[3,4] di3[4,4] di3[5,4]\r\n[1,]        4        5        4        5        2        4        4        5\r\n[2,]        4        5        1        5        1        5        6        5\r\n[3,]        5        5        5        3        4        1        5        1\r\n[4,]        5        5        4        5        5        5        5        5\r\n[5,]        5        5        4        5        5        5        1        5\r\n[6,]        5        5        1        5        5        5        6        4\r\n     di3[6,4] di3[1,5] di3[2,5] di3[3,5] di3[4,5] di3[5,5] di3[6,5] di3[1,6]\r\n[1,]        5        5        4        1        5        4        5        4\r\n[2,]        5        4        5        5        4        4        5        2\r\n[3,]        5        4        5        4        5        4        5        5\r\n[4,]        4        6        4        4        5        6        1        1\r\n[5,]        6        4        5        5        5        5        4        5\r\n[6,]        6        2        2        4        5        2        4        4\r\n     di3[2,6] di3[3,6] di3[4,6] di3[5,6] di3[6,6]\r\n[1,]        5        2        4        5        4\r\n[2,]        4        4        5        5        5\r\n[3,]        4        5        5        5        5\r\n[4,]        2        5        5        5        1\r\n[5,]        5        1        4        5        4\r\n[6,]        5        4        5        5        5\r\n```\r\n\r\nNotice lots of irrelevant samples from `di3`.  The only one that matters is the one indexed by `di1` and `di2` for each respective row of the matrix. To fix this, modify the JAGS code a bit, and don't ask for `di3Sample` when calling `coda.samples()`:\r\n\r\n```\r\nmodel{\r\n  # Likelihood:\r\n  di1 ~ dcat(priorProbs)\r\n  di2 ~ dcat(priorProbs)\r\n\r\n  for ( i in 1:dim1 ) {\r\n    for( j in 1:dim2 ){\r\n      di3Sample[i,j] ~ dcat( probArray[i,j,] )\r\n    }\r\n  }\r\n\r\n  di3 <- di3Sample[di1, di2]\r\n}\r\n```\r\n\r\nThis looks better:\r\n\r\n```\r\n      di1 di2 di3\r\n [1,]   6   6   4\r\n [2,]   6   6   5\r\n [3,]   6   6   5\r\n [4,]   6   6   5\r\n [5,]   6   6   1\r\n [6,]   6   6   2\r\n [7,]   6   6   1\r\n [8,]   6   6   5\r\n [9,]   6   6   4\r\n[10,]   6   6   4\r\n[11,]   6   6   5\r\n[12,]   6   6   1\r\n[13,]   6   6   5\r\n[14,]   6   6   4\r\n[15,]   6   6   5\r\n[16,]   6   6   4\r\n[17,]   6   6   5\r\n[18,]   6   6   4\r\n[19,]   6   6   5\r\n[20,]   6   6   1\r\n```\r\n\r\nAnd just to check that we're getting the probabilities we think we're getting:\r\n\r\n```\r\ncs = coda.samples( jagsModel , variable.names=parameters , \r\n                   n.iter=nIter , thin=thinSteps )\r\n\r\nd <- do.call(\"rbind\", cs)\r\n\r\nd$di1 <- as.factor(d$di1)\r\nd$di2 <- as.factor(d$di2)\r\nd$di3 <- as.factor(d$di3)\r\n\r\ncpt <- cpt(di3 ~ di1 + di2, data=d)\r\n\r\ncpt[di1=6, di2=6, ]\r\n```\r\n\r\n```\r\n         1          2          3          4          5          6 \r\n0.14950980 0.10294118 0.02696078 0.27696078 0.41421569 0.02941176\r\n```\r\n\r\nThe original probabilities defined in the construction of the training dataset were:\r\n\r\n```\r\n#di3=as.factor(1:6 %*% rmultinom(n,1,prob=c(.15,.10,.02,.3,.4,.03)))\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/nutterb/HydeNet/issues/comments/106944273","html_url":"https://github.com/nutterb/HydeNet/issues/58#issuecomment-106944273","issue_url":"https://api.github.com/repos/nutterb/HydeNet/issues/58","id":106944273,"user":{"login":"nutterb","id":1848445,"avatar_url":"https://avatars.githubusercontent.com/u/1848445?v=3","gravatar_id":"","url":"https://api.github.com/users/nutterb","html_url":"https://github.com/nutterb","followers_url":"https://api.github.com/users/nutterb/followers","following_url":"https://api.github.com/users/nutterb/following{/other_user}","gists_url":"https://api.github.com/users/nutterb/gists{/gist_id}","starred_url":"https://api.github.com/users/nutterb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nutterb/subscriptions","organizations_url":"https://api.github.com/users/nutterb/orgs","repos_url":"https://api.github.com/users/nutterb/repos","events_url":"https://api.github.com/users/nutterb/events{/privacy}","received_events_url":"https://api.github.com/users/nutterb/received_events","type":"User","site_admin":false},"created_at":"2015-05-29T21:51:01Z","updated_at":"2015-05-29T21:51:01Z","body":"er...crap!  I just uploaded it to the `development` branch.  I'm a genius.  \r\n\r\nBut I have to go make dinner so I can't fix it right now.  Everything should run fine as long as you aren't using any `cpt` models, so I don't think I'll bother to revert. (at the very least, it passes `check` right up until the examples when it tries to create the JAGS code for a `cpt` model)."}}