本文介绍了估计JAGS中的未知响应变量-无监督学习的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据已知的分布参数来估算百分比覆盖率(COV)的响应值.我可以通过在OpenBUGS中将响应数据指定为NA(例如下面的代码)来做到这一点,但是JAGS不允许这样做.有谁知道我如何用JAGS做到这一点?

I am attempting to estimate response values of percentage cover (COV) from known distribution parameters. I can do this by specifying the response data as NAs in OpenBUGS (e.g. the code below) but JAGS won't allow this. Does anyone know how I can achieve this in JAGS?

我认为这属于无监督统计学习"类别

I think this falls into the category of 'unsupervised statistical learning'

model {
  for (i in 1:n.sites) {  # loop around sites
    # specify prior distribution forms for effectively unknown percentage  cover
    COV[i] ~ dbeta(a[i], b[i])T(r1[i], r2[i])
  }
}

# DATA
list(n.sites=5, COV=c(NA, NA, NA, NA, NA), a=c(7.1,2.2,13,10,13),
     b=c(25,11,44,27,44), r1=c(0.05,0.1,0.2,0.1,0.2),
     r2=c(0.15,0.3,0.6,0.3,0.6) )

# INITS
list(COV=c(0.1, 0.2, 0.4, 0.2, 0.4))

推荐答案

如果只想模拟与截断的beta分布一致的COV值,则可以从数据列表中省略COV.例如:

If you just want to simulate values for COV that are consistent with your truncated beta distribution, then you can omit COV from your list of data. For example:

library(R2jags)

cat('
model {
  for (i in 1:n.sites) {
    COV[i] ~ dbeta(a[i], b[i])T(r1[i], r2[i])
  }
}', file={M <- tempfile()})


dat <- list(n.sites=5, a=c(7.1, 2.2, 13, 10, 13), b=c(25, 11, 44, 27, 44),
            r1=c(0.05, 0.1, 0.2, 0.1, 0.2), r2=c(0.15, 0.3, 0.6, 0.3, 0.6))

j <- jags(dat, NULL, 'COV', M, 1, 10000, DIC=FALSE, n.burnin=0, n.thin=1)

这是10000个模拟的COV向量的矩阵的前几行...

Here are the top few rows of the matrix of 10000 simulated COV vectors...

head(j$BUGSoutput$sims.list$COV)

##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.1169165 0.2889155 0.2543063 0.2083161 0.2426788
## [2,] 0.1494647 0.1430956 0.2867575 0.2410594 0.2795923
## [3,] 0.1200414 0.2093230 0.2736719 0.2189734 0.2469634
## [4,] 0.1472082 0.1442609 0.2911482 0.2625216 0.2714883
## [5,] 0.1403574 0.1100977 0.2556352 0.1918480 0.2353231
## [6,] 0.1310404 0.1677148 0.3011752 0.1974136 0.2131811


编辑

由于您只是从已知分布中进行采样,因此您也可以在纯R中执行此操作(distr程序包提供了一些有助于从截断的分布中进行采样的功能).

Since you're just sampling from a known distribution, you can also do this in pure R (the distr package provides some functions that help with sampling from truncated distributions).

library(distr)
n <- 10000 # How many samples?
COV <- mapply(function(shape1, shape2, min, max) {
  r(Truncate(Beta(shape1, shape2), min, max))(n)
}, shape1=c(7.1, 2.2, 13, 10, 13), shape2=c(25, 11, 44, 27, 44),
   min=c(0.05, 0.1, 0.2, 0.1, 0.2), max=c(0.15, 0.3, 0.6, 0.3, 0.6))

上面,您应将shape1shape2minmax的等长向量传递给mapply,这将为每个shape1和依次对应shape2minmax.

Above, you should pass equal-length vectors of shape1, shape2, min and max to mapply, which will generate n random beta-distributed variates for each shape1 and it's corresponding shape2, min and max in turn.

我们可以比较COV列(我们的纯R样本)和j$BUGSoutput$sims.list$COV列(我们的JAGS样本)的内核密度.

We can compare the kernel densities of the columns of COV (our pure-R samples) to those of the columns of j$BUGSoutput$sims.list$COV (our JAGS samples).

par(mfrow=c(3, 2), mar=c(3, 0.5, 0.5, 0.5))
sapply(1:5, function(i) {
  djags <- density(j$BUGSoutput$sims.list$COV[, i])
  dr <- density(COV[, i])
  plot(djags, lwd=4, col='gray80', main='', ylab='', xlab='', yaxt='n',
       ylim=c(0, max(djags$y, dr$y)))
  lines(dr)
})
plot.new()
legend('topleft', c('JAGS', 'R'), col=c('gray80', 'black'), lwd=c(4, 1), bty='n')

这篇关于估计JAGS中的未知响应变量-无监督学习的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 03:16