Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R JAGS检测模拟错误/生态学家搞砸了高级统计_R_Distribution_Jags - Fatal编程技术网

R JAGS检测模拟错误/生态学家搞砸了高级统计

R JAGS检测模拟错误/生态学家搞砸了高级统计,r,distribution,jags,R,Distribution,Jags,我是贝叶斯世界的新手,很高兴能将其应用到我的研究中。作为第一步,我尝试建立一个模拟模型,以估计声探测器10公里半径范围内的动物平均数量以及作为噪声级函数的检测概率。基本上,我想问的是,两个地点是否有1k动物,但噪音特征不同,模型能否告诉我两个研究区域之间的“真实”意思(1k动物)是否相同。希望是的 这是一个三步过程,将检测到的呼叫数(kk1)与检测概率(Pdet)和信噪比(SNR,对数标度)联系起来,最后与区域内动物的平均数量联系起来 我已经模拟了我的动物位置、源水平、传输损耗(取决于动物位置)

我是贝叶斯世界的新手,很高兴能将其应用到我的研究中。作为第一步,我尝试建立一个模拟模型,以估计声探测器10公里半径范围内的动物平均数量以及作为噪声级函数的检测概率。基本上,我想问的是,两个地点是否有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))
##################################################################
##################################################################
图书馆(runjags)
#库(plotrix)
图书馆(rjags)
#初始化,谢谢Matt
###伽马分布规格功能,谢谢马特###

gammaPr在JAGS模型中,Pdet是

kk1[ii] ~ dbin(Pdet, mu)
但是,然后将Pdet指定为beta概率分布函数:

Pdet<-pbeta(SNR[ii], betaPar1, betaPar2)
或者(与您模拟的数据更一致),您的意思是kk1[ii]是1000个伯努利试验的总和,具有个体特异性检测概率Pdet。所以你需要做的是让kk1[ii]是一系列特定的伯努利试验的总和。在JAGS中,观察到的数据必须是某些分布的随机实现,因此,例如,您不能写入

for(ii in 1:300){
for(jj in 1:mu){
kk11[jj] ~ dbern(p[jj])
}
kk1[ii] <- sum(kk11[jj])
}
for(1:300中的ii){
用于(1:mu中的jj){
kk11[jj]~dbern(p[jj])
}

kk1[ii]在JAGS模型中,Pdet是

kk1[ii] ~ dbin(Pdet, mu)
但是,然后将Pdet指定为beta概率分布函数:

Pdet<-pbeta(SNR[ii], betaPar1, betaPar2)
或者(与您模拟的数据更一致),您的意思是kk1[ii]是1000个伯努利试验的总和,具有个体特异性检测概率Pdet。因此您需要做的是让kk1[ii]例如,在JAGS中,你观察到的数据必须是某种分布的随机实现,因此你不能写

for(ii in 1:300){
for(jj in 1:mu){
kk11[jj] ~ dbern(p[jj])
}
kk1[ii] <- sum(kk11[jj])
}
for(1:300中的ii){
用于(1:mu中的jj){
kk11[jj]~dbern(p[jj])
}

kk1[ii]嗨,雅各布,谢谢!这真的很有帮助。我把N定为1000,只是为了减少变化。我还简化了模型以估算入住率(伯努利分布)而不是动物的数量。也许在以后某个时候将我的时间序列数据存储起来以估计动物的数量或行为是有意义的,但是,小步骤。嗨,雅各布,谢谢!这真的很有帮助。我已经将N固定为1000,只是为了减少变化。我还简化了模型以估计占用率(伯努利分布)而不是动物的数量。也许在以后的某个时候将我的时间序列数据存储起来以估计动物的数量或行为是有意义的,但是,婴儿步。