Skip to content

Commit

Permalink
Fix mistakes in R files
Browse files Browse the repository at this point in the history
  • Loading branch information
ito4303 committed Apr 12, 2017
1 parent bac5927 commit a73984d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
11 changes: 5 additions & 6 deletions BPA/Ch.07/7.10.2_Time-dependent_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 10,10 @@ set.seed(123)
## Read data
## The data generation code is in bpa-code.txt, available at
## http://www.vogelwarte.ch/de/projekte/publikationen/bpa/complete-code-and-data-files-of-the-book.html
stan_data <- read_rdump("cjs_mnl_age.data.R")
stan_data <- read_rdump("cjs_mnl.data.R")

## Parameters monitored
params <- c("mean_phijuv", "mean_phiad", "mean_p")
params <- c("phi", "p", "fit", "fit_new")

## MCMC settings
ni <- 2000
Expand All @@ -22,12 22,11 @@ nb <- 1000
nc <- 4

## Initial values
inits <- function() list(mean_phijuv = runif(1, 0, 1),
mean_phiad = runif(1, 0, 1),
mean_p = runif(1, 0, 1))
inits <- function() list(phi= runif(dim(stan_data$marr)[2] - 1, 0, 1),
p = runif(dim(stan_data$marr)[2] - 1, 0, 1))

## Call Stan from R
cjs_mnl_age <- stan("cjs_mnl_age.stan",
cjs_mnl_age <- stan("cjs_mnl.stan",
data = stan_data, init = inits, pars = params,
chains = nc, iter = ni, warmup = nb, thin = nt,
seed = 1,
Expand Down
37 changes: 37 additions & 0 deletions BPA/Ch.07/7.10.3_Age-dependent_models.R
Original file line number Diff line number Diff line change
@@ -0,0 1,37 @@
## 7. Estimation of survival probabilities using capture-recapture data
## 7.10. Fitting the CJS to data in the m-array format: the multinomial likelihood
## 7.10.3. Age-dependent models

library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
set.seed(123)

## Read data
## The data generation code is in bpa-code.txt, available at
## http://www.vogelwarte.ch/de/projekte/publikationen/bpa/complete-code-and-data-files-of-the-book.html
stan_data <- read_rdump("cjs_mnl_age.data.R")

## Parameters monitored
params <- c("mean_phijuv", "mean_phiad", "mean_p")

## MCMC settings
ni <- 2000
nt <- 1
nb <- 1000
nc <- 4

## Initial values
inits <- function() list(mean_phijuv = runif(1, 0, 1),
mean_phiad = runif(1, 0, 1),
mean_p = runif(1, 0, 1))

## Call Stan from R
cjs_mnl_age <- stan("cjs_mnl_age.stan",
data = stan_data, init = inits, pars = params,
chains = nc, iter = ni, warmup = nb, thin = nt,
seed = 1,
open_progress = FALSE)

## Summarize posteriors
print(cjs_mnl_age, digits = 3)

0 comments on commit a73984d

Please sign in to comment.