我是贝叶斯世界的新手,很高兴将它应用到我的研究中。作为第一步,我尝试建立一个模拟模型,该模型估计声学探测器10km半径范围内的动物平均数和作为噪声水平函数的检测概率。基本上,我问两个地方是否有1k动物,但噪音特征不同,模型能告诉我两个研究区域之间的“真实”意味着(1k只动物)是否相同。希望是的。
这是一个三步过程,它将检测到的呼叫数(kk1)与检测概率(Pdet)与信噪比(SNR,对数标度)最终关联到该区域中动物的平均数。
我已经模拟了我的动物位置,震源水平,传播损失(取决于动物位置),SNR和Pdet功能如下。
##################################################################
library(runjags)
#library(plotrix)
library(rjags)
# Initializations, thanks Matt
### Gamma distribution specification function, thanks Matt! ###
gammaPr<-function(mu, sd)
{
shape<-mu^2/sd^2
rate<-mu/sd^2
x<-seq(max(0, mu-4*sd),mu+4*sd, length.out=100)
#plot(x, dgamma(x, shape=shape, rate=rate), main=paste("Prior distribution with mean ",mu," and sd ", sd), type="l")
#print(paste("The two parameters of the Gamma distribution are shape=",shape," and rate=",rate))
return(c(shape,rate))
}
##################
# Simulated Data #
##################
# Number of animals within the detection radius (5km)
N=1000
#x=seq(0,5,by=1/N)
# Idiot checking:
# Since this is point sampling and (for the moment) we assume that the number
# of animals available to be detected increases linearly with radius
#x1 = rbeta(N, shape1 = 2, shape2 = 1)
#theta = runif(N, min = 0, max=2*pi)
#radial.plot(lengths = x1, radial.pos = theta, rp.type = 's')
## We know the parameters for Source Levels values from the literature
nlpr=gammaPr(120, 30)
# Time series
tt=seq(1, 300, by=1)
# Number of animals at each site available to be sampled
N1=numeric(length=length(tt))
N2=N1
NL1=N1 # Noise Level Distribution
NL2=N1
TL1=N1 # Estimated Transmission loss
TL2=N1
kk1=N1 # Number of animals detected by the sensor
kk2=N1
for (ii in 1:length(tt)){
#N1[ii]=round(rnorm(1, mean = 700, sd=100)) # Screw it-there are enough problems to deal with
#N2[ii]=round(rnorm(1, mean = 700, sd=100))
N1[ii]=1000
N2[ii]=1000
# Random whale distances
r1=rbeta(N1[ii], shape1 = 2, shape2 = 1)*5 # assumed (known)
r2=rbeta(N2[ii], shape1 = 2, shape2 = 1)*5
# Random source levels for each whale
slpr=gammaPr(131, 20) # assumed (known)
SL1=rgamma(N1[ii], slpr[1], slpr[2])
SL2=rgamma(N2[ii], slpr[1], slpr[2])
# Noise Levels for each sensor
nlpr=gammaPr(120, 30)
NL1[ii]=rgamma(1, nlpr[1], nlpr[2]) # measured (known)
nlpr=gammaPr(90, 30)
NL2[ii]=rgamma(1, nlpr[1], nlpr[2])
# TL Transmission loss from each animal to sensor
TL1=20*log10(r1*1000) # this one is tricky, we can assume TL but it's based on multiple captures so how this make it into the probability density function escapes me
TL2=20*log10(r2*1000)
# Signal to noise ratio for each animal call
SNR1=SL1-(1/(1000*r1)^2)-NL1[ii] # calculated
SNR2=SL2-(1/(1000*r2)^2)-NL2[ii]
# Detection probability for a given SNR threshold
Pdet1=pbeta(SNR1, shape1 = 1.5, shape2 = 2.5) # unknown, we will need to estimate shape1 and shape from the JAGS model
Pdet2=pbeta(SNR2, shape1 = 1.2, shape2 = 2.5)
## Idiot checking again
#yy=pbeta(seq(-40,40, by=.01), 1.5, 2.5)
#plot(seq(-40,40, by=.01),yy, xlim=c(-2,2))
# Number of animals detected is binomial based on the detection function
kk1[ii]=sum(rbinom(N1[ii], size=1, prob = Pdet1))
kk2[ii]=sum(rbinom(N2[ii], size=1, prob = Pdet2))
}
data=data.frame(cbind(tt, N1,N2, NL1, NL2,kk1, kk2))
##################################################################
这很好,但是我没有成功构建一个JAGS模型 可以预测动物的平均数量,更不用说在两个不同的研究区域。该模型的最新版本在逻辑上对[我]有意义,会引发有关重新定义节点Pdet 1的错误。但由于这些是我试图估计的价值,因此相当令人困惑。非常感谢任何帮助。
也许这是定义临时变量的问题? E.g.?但这与我的问题有点不同,因为我只使用一个循环。
提前感谢您的时间。
#############################################################################
# Model Fitting#
#############################################################################
# Build the JAGS model to determine if the true number
# of animals at each location is different
# We are trying to estimate the mean population size (mu)
# Number of trials (detection periods)
N=nrow(data)
# We guess that the median range of animals within the detection
# area is 2500 m (will be updated later with propationg modelling)
# This is stupid. There needs to be stochasticity in both values.
MedTL=20*log10(2500)
MedSL=rgamma(1, slpr[1], slpr[2])
# We are trying to estimate the population mean and the
# detection function parameters
modeltext <- '
model{
#### liklihood ###
for(ii in 1:N){
# Number of animals detected is distributed by a normal function the total available (mu)
# and the probability that they are detected(Pdet)
kk1[ii] ~ dbin(Pdet, mu)
# Pdet is a beta distribution with parameters estimated from above
Pdet<-pbeta(SNR[ii], betaPar1, betaPar2)
# SNR (signal to noise ratio) is the Source Level-Noise Levl-Transmission Loss
SNR[ii]<-MedSL-MedTL-NL1[ii]
# SL and RL need stochasticity.....
}
## Priors ###
# Mu is dependent on how the animals are distributed in space. In this case, 1000. Because that's what I told it to do.
mu~dpois(lambda)
# We know mu must be greater than 0 and poison
lambda~dgamma(11, .01) # approximately what they should be based on mu 1000, sd 300
# We will assume we know a bit about the detection function, we will be helpful
betaPar1 ~ dnorm(1,.3)
betaPar2 ~ dnorm(2,.3)
}
#data# kk1, NL1, N, slpr, MedTL, MedSL
#monitor# betaPar1, betaPar2, mu, pdet
'
# We could just run the model like this:
results1 <- run.jags(modeltext, n.chains=2)
答案 0 :(得分:0)
在你的JAGS模型中,Pdet是
中的标量概率kk1[ii] ~ dbin(Pdet, mu)
但是你将Pdet指定为beta概率分布函数:
Pdet<-pbeta(SNR[ii], betaPar1, betaPar2)
据推测,你实际上意味着Pdet是来自beta分布的抽象。这将是
Pdet ~ pbeta(SNR[ii], betaPar1, betaPar2)
或者(与您模拟的数据更一致),您的意思是kk1 [ii]是具有个体特异性检测概率Pdet的1000个伯努利试验的总和。所以你需要做的是让kk1 [ii]成为一系列具体的bernoulli试验的总和。在JAGS中,您观察到的数据必须是某些分布的随机实现,因此您无法写入,例如
for(ii in 1:300){
for(jj in 1:mu){
kk11[jj] ~ dbern(p[jj])
}
kk1[ii] <- sum(kk11[jj])
}
幸运的是,JAGS已经预见到了你的quandry,并且已经为你的目的实现了特殊的可观察函数dsum()。查看JAGS手册。
总而言之,除非我误读你的模拟,你真正需要的是用kk1 [ii]循环时间步骤,但是在那个循环中你需要循环在每个时间步骤中,每个人都需要有自己的模型检测概率。在这一点上,你可能会想,问一下,但他们可能有足够的数据来模拟跨时间步长的各个检测概率?&#34;答案是,您真正需要建模的是多年来个体检测概率的分布。这将与传感器的各个距离的分布,系统的(可能随时间变化的)噪声特性等有关。
好消息是(第一次近似),只要您可以模拟R中的数据,就可以在JAGS中制定相应的模型。您现在面临的挑战是确保模拟生成您想要的数据,然后JAGS模型实际上与模拟相对应。
<强>后记强>
为此,我想要注意模拟和您可能没有预期的模型之间的一个奇怪的区别。在模拟中,您修复N = 1000。在模型中,mu的先验是伽马泊松混合。你有理由这样做吗?一个原因是如果感兴趣的真实数量是泊松参数lambda,它对应于系统中动物的密度,从中绘制N. 在这种情况下,您应该将其作为数据模拟的一部分加入:即
N <- rpois(1000)
你应该监视lambda(而不是mu)作为JAGS输出中感兴趣的主要数量。 [你还需要在这里思考N是否被绘制一次然后固定,或者它是否在不同的时间步骤重复绘制]。 否则,您可能会认为mu更为直观。