if (FALSE) {
#==============================================
# hSDM.ZIB()
# Example with simulated data
#==============================================
#============
#== Preambule
library(hSDM)
#==================
#== Data simulation
# Set seed for repeatability
seed <- 1234
# Number of observations
nobs <- 1000
# Target parameters
beta.target <- matrix(c(0.2,0.5,0.5),ncol=1)
gamma.target <- matrix(c(1),ncol=1)
## Uncomment if you want covariates on the observability process
## gamma.target <- matrix(c(0.2,0.5,0.5),ncol=1)
# Covariates for "suitability" process
set.seed(seed)
X1 <- rnorm(n=nobs,0,1)
set.seed(2*seed)
X2 <- rnorm(n=nobs,0,1)
X <- cbind(rep(1,nobs),X1,X2)
# Covariates for "observability" process
W <- cbind(rep(1,nobs))
## Uncomment if you want covariates on the observability process
## set.seed(3*seed)
## W1 <- rnorm(n=nobs,0,1)
## set.seed(4*seed)
## W2 <- rnorm(n=nobs,0,1)
## W <- cbind(rep(1,nobs),W1,W2)
#== Simulating latent variables
# Suitability
logit.theta.1 <- X %*% beta.target
theta.1 <- inv.logit(logit.theta.1)
set.seed(seed)
y.1 <- rbinom(nobs,1,theta.1)
# Observability
set.seed(seed)
trials <- rpois(nobs,5) # Number of trial associated to each observation
trials[trials==0] <- 1
logit.theta.2 <- W %*% gamma.target
theta.2 <- inv.logit(logit.theta.2)
set.seed(seed)
y.2 <- rbinom(nobs,trials,theta.2)
#== Simulating response variable
Y <- y.2*y.1
#== Data-set
Data <- data.frame(Y,trials,X1,X2)
## Uncomment if you want covariates on the observability process
## Data <- data.frame(Y,trials,X1,X2,W1,W2)
#==================================
#== ZIB model
mod.hSDM.ZIB <- hSDM.ZIB(presences=Data$Y,
trials=Data$trials,
suitability=~X1+X2,
observability=~1, #=~1+W1+W2 if covariates
data=Data,
suitability.pred=NULL,
burnin=1000, mcmc=1000, thin=5,
beta.start=0,
gamma.start=0,
mubeta=0, Vbeta=1.0E6,
mugamma=0, Vgamma=1.0E6,
seed=1234, verbose=1,
save.p=0)
#==========
#== Outputs
pdf(file="Posteriors_hSDM.ZIB.pdf")
plot(mod.hSDM.ZIB$mcmc)
dev.off()
summary(mod.hSDM.ZIB$prob.p.latent)
summary(mod.hSDM.ZIB$prob.q.latent)
summary(mod.hSDM.ZIB$prob.p.pred)
}
Run the code above in your browser using DataLab