stan-dev / posteriordb

Database with posteriors of interest for Bayesian inference
181 stars 36 forks source link

[WIP] fixes dog_log in Stan #246

Closed bob-carpenter closed 2 years ago

bob-carpenter commented 2 years ago

This fixes the problem with the Stan model, but we still need to generate new reference samples. I'm not sure how to do that.

MansMeg commented 2 years ago

Cool. I'll try to fix this asap.

bob-carpenter commented 2 years ago

I just realized you have instructions for this at:

https://github.com/stan-dev/posteriordb/blob/master/doc/CONTRIBUTING.md

I'll just try to follow those and submit a proper PR.

bob-carpenter commented 2 years ago

I couldn't find any complete step-by-step instructions. The closest I found was here:

https://github.com/stan-dev/posteriordb/blob/master/doc/CONTRIBUTING.md

but then that seems to be a near duplicate of this:

https://github.com/stan-dev/posteriordb-r/blob/main/docs/CONTRIBUTING.md

So I'm starting out confused. So I started with the one on posteriordb, which I found first, but I got lost on instruction number one:

Start by installing and loading the posteriordb R package.

I hunted around and found another repository called posterordb-r that has its own install instructions. Was that what I was supposed to do? If so, a link would be super helpful to reduce uncertainty (I tend to get overwhelmed with uncertainty in install instructions). The instructions in posteriordb-r are clearer, but I only found those after the initial guesswork following instructions on posteriordb.

Now I'm stuck on what to do if the data is already there for a model. I guess I'll just rewrite it all because I don't know how to update.

In the Add Data instruction, there's an unbound variable eight_schools that was never defined:

dat <- pdb_data(eight_schools, info = di)

But then I have another problem, which is that the dogs data is in json format in a .zip file. So I'm stuck on the Add Data instruction. Do I really have to unpack the JSON and then figure out how to generate an equivalent R dump file before I can submit a new model?

Anyway, that's as far as I could get before I couldn't figure out what to do next. If you update the instructions to explain what to do with JSON data and why there's an unbound variable in the example, I can probably keep going myself.

Here's the script I have so far

library(posteriordb)
library(rstan)
pdb_path = "~/github/stan-dev/posteriordb"
Sys.setenv(PDB_PATH = pdb_path)
pdbl <- pdb_local()

metadata <-
    list(name = "dogs_log",
         keywords = c("ARM", "Ch. 24", "stan_examples"),
         title = "Logarithmic Mixed Effects Model",
         description = "y ~ exp(n_avoid + n_shock)",
         urls = "https://raw.githubusercontent.com/stan-dev/example-models/master/ARM/Ch.24/dogs_log.stan",
         references = "gelman2006data",
         added_date = Sys.Date(),
         added_by = "Bob Carpenter")
di <- pdb_data_info(metadata)

# Access the data
# file_path <- system.file("~/temp2/dogs/dogs.json")
# You can check the file with: file.edit(file_path)
# source(file_path)

# Create the data object
# dat <- pdb_data(eight_schools, info = di)

I unzipped the data in a temp directory, but I'm not sure if that's what I was supposed to be doing or whether the data needed to be somewhere in the right place in posteriordb.

bob-carpenter commented 2 years ago

After several more hours of flailing, I'm admitting defeat. The main problems at the moment are:

  1. I don't know how to deal with JSON data directly. So I just read it in and munged it, but that's not a good enough solution going forward. Is there a way to directly deal with JSON? If not, is there a way to auto-convert JSON to R dump? The basic JSON reader I found on R doesn't read in the appropriate format, so I had to munge in my R script (see below).
  2. The pdb_data command was given a variable eight_schools that didn't exist. I created a data list in R manually of the kind we feed to RStan and tried to use that. No idea if its' the right thing to do as there aren't narrative instructions explaining anything. There's eventually a check_pdb_posterior, but when that fails, I don't know where the problem originated.
  3. I couldn't understand the system.file instructions. I'm a very weak R programmer, but I also wasn't sure what the intention was. So I just hacked in a direct path to a temp file, but have no idea if that's going to work. I'm just flailing at this point.
  4. When I try the write_pdb for the "add model" instructions, it fails because a file already exists that it's hoping to write. If I try to delete that file before running, it fails in the next write_pdb. Something seems off in those instructions or I'm not coding metadata right or ...?
  5. I didn't see any instructions on how to fill out the metadata. It seems largely redundant among the different sources of metadata. What are all the fields and how should they be filled in? I just guessed and am wondering if that could be a source of failure (the uncertainty piles up after a sequence of hacks to get around failures).
  6. The biggest help for me would be a working script that does this whole process end-to-end for my model and data. So I'm providing my Stan model, a link to the JSON data, my attempt to munge the data into R, and my R script, showing where things break if you just try to follow the instructions as written.

Stan Model

data {
  int<lower=0> n_trials;
  int<lower=0> n_dogs;
  int<lower=0, upper=1> y[n_dogs, n_trials];
}
transformed data {
  matrix[n_dogs, n_trials] n_avoid;
  matrix[n_dogs, n_trials] n_shock;
  for (n in 1:n_dogs) {
    n_avoid[n, 1] = 0;
    n_shock[n, 1] = 0;
    for (t in 2:n_trials) {
      n_avoid[n, t] = n_avoid[n, t - 1] + 1 - y[n, t - 1];
      n_shock[n, t] = n_shock[n, t - 1] + y[n, t - 1];
    }
  }
}
parameters {
  real<lower=-100, upper=0> beta1;
  real<lower=0, upper=100> beta2;
}
model {
  matrix[n_dogs, n_trials] logit_p = beta1 * n_avoid + beta2 * n_shock;

  // priors
  beta1 ~ uniform(-100, 0);
  beta2 ~ uniform(0, 100);

  // likelihood
  for (i in 1:n_dogs) {
    y[i] ~ bernoulli_logit(logit_p[i]);
  }
}

JSON Data

I'd really like to do this from JSON without having to go through R's data format. Here's the link to the gzipped JSON file that's currently being distributed:

https://github.com/stan-dev/posteriordb/blob/master/posterior_database/data/data/dogs.json.zip

R Data

Someone should verify that I munged this correctly. It's in my script after I manually unzipped the .zip file (I didn't have the patience to figure out how to do that programatically in R).

n_dogs <- 31
n_trials <- 25
y <- 
structure(c(0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0,
1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1,
0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0,
1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0,
0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
.Dim = c(31, 25))

R Script

Here's my best guess, but the adds are all throwing errors because files exist, even if I delete them before starting the run. My best guess is that one of the first calls is creating them and then the later calls are balking. That's why I'd really appreciate a working script that does all this. If I can get my hands on that, then I can add a bunch more models. As is, I can't even add this one.

library(posteriordb)
library(rstan)
library(rjson)

pdb_path = "~/github/stan-dev/posteriordb"
Sys.setenv(PDB_PATH = pdb_path)
pdbl <- pdb_local()

# add data
metadata <-
    list(name = "dogs_log",
         keywords = c("ARM", "Ch. 24", "stan_examples"),
         title = "Logarithmic Mixed Effects Model",
         description = "y ~ exp(n_avoid + n_shock)",
         urls = "https://raw.githubusercontent.com/stan-dev/example-models/master/ARM/Ch.24/dogs_log.stan",
         references = "gelman2006data",
         added_date = Sys.Date(),
         added_by = "Bob Carpenter")
di <- pdb_data_info(metadata)
jdata <- fromJSON(file = "~/temp2/dogs/dogs.json")
n_dogs <- jdata$n_dogs
n_trials <- jdata$n_trials
y <- matrix(NA, nrow=31, ncol=25)
for (n in 1:31) y[n, 1:25] <- jdata$y[n][[1]]
rdata_path <- "~/temp2/dogs/dogs.data.R"
stan_rdump(c("n_dogs", "n_trials", "y"), file = rdata_path)
# just guessing here as the example used an undefined variable for data_list
data_list <- list(n_dogs = n_dogs, n_trials = n_trials, y = y)
# Create the data object
dat <- pdb_data(data_list, info = di)
write_pdb(dat, pdbl)

# add model
# this feels super redundant at the metadata level
model_metadata <- metadata
model_metadata$framework = "stan"
mi <- pdb_model_info(model_metadata)
# if this has to be a system file, example instructions don't work
file_path <- "~/temp2/dogs/dogs_log.stan"
smc <- readLines(file_path)
sm <- rstan::stan_model(model_code = smc)
mc <- model_code(sm, info = mi)
# this fails because files exist
write_pdb(mc, pdbl)

# add posterior object (what is this?)
post_metadata <- list(pdb_model_code = mc,
          pdb_data = dat,
          keywords = c("ARM", "Ch. 24", "stan_examples"),
          urls = "https://raw.githubusercontent.com/stan-dev/example-models/master/ARM/Ch.24/dogs_log.stan",
          references = "gelman and hill, arm ch. 24",
          dimensions = list("dimensions" = 2, "dim" = 2),  ## no idea what this is and why it has dimensions twice
          reference_posterior_name = NULL,
          added_date = Sys.Date(),
          added_by = "Bob Carpenter")
po <- pdb_posterior(post_metadata, pdbl)
write_pdb(po, pdbl)
check_pdb_posterior(po)  
MansMeg commented 2 years ago

Thanks for all the comments, Bob!

I realized that this was not clear enough in the contribution documentation so I took some time to try to improve it in the way you mentioned. I have also tried to improve the R package with better and more intuitive constructors. The steps needed to add a model/data/posterior should be much more clear, I hope. Instead, I have created the R instructions as a vignette in the R package. See here.

I don't know how to deal with JSON data directly. So I just read it in and munged it, but that's not a good enough solution going forward. Is there a way to directly deal with JSON? If not, is there a way to auto-convert JSON to R dump? The basic JSON reader I found on R doesn't read in the appropriate format, so I had to munge in my R script (see below).

If you just pass the R data as a list to the R package using as.pdb_data(). Then you can just write it to the posterior database with write_pdb(). I have made this clear now in the vignette.

The pdb_data command was given a variable eight_schools that didn't exist. I created a data list in R manually of the kind we feed to RStan and tried to use that. No idea if its' the right thing to do as there aren't narrative instructions explaining anything. There's eventually a check_pdb_posterior, but when that fails, I don't know where the problem originated.

Good point. I fixed that to make it easier and more clear in the vignette.

I couldn't understand the system.file instructions. I'm a very weak R programmer, but I also wasn't sure what the intention was. So I just hacked in a direct path to a temp file, but have no idea if that's going to work. I'm just flailing at this point.

That makes sense, it is not intuitive. I have removed that part now and just added the stan code and the data directly.

When I try the write_pdb for the "add model" instructions, it fails because a file already exists that it's hoping to write. If I try to delete that file before running, it fails in the next write_pdb. Something seems off in those instructions or I'm not coding metadata right or ...?

Hmmm. Yes. The default is that overwrite = FALSE. Then it won't overwrite the file, but throw and error. A simple approach is to set overwrite = TRUE. I have now added also this to the vignette. Maybe overwrite = TRUE should be the default instead.

I didn't see any instructions on how to fill out the metadata. It seems largely redundant among the different sources of metadata. What are all the fields and how should they be filled in? I just guessed and am wondering if that could be a source of failure (the uncertainty piles up after a sequence of hacks to get around failures).

Good point! This is included in another md-file that is not clearly referenced. I have now also added this to the vignette and the contribution instructions. As you say, the metadata is kind of bloated just now with redundancies. We should probably discuss how that can be better structured. The problem is that we have both situations where the same data is used by multiple models (model comparison) and where the same models are used for multiple datasets (effects of different data properties/sizes).

The biggest help for me would be a working script that does this whole process end-to-end for my model and data. So I'm providing my Stan model, a link to the JSON data, my attempt to munge the data into R, and my R script, showing where things break if you just try to follow the instructions as written.

Yes. I fully agree. This has now been added. in the vignette Please let me know if this is now more clear to you. I also appreciate your extensive comment a lot. It helps to improve!

Below I fixed your code that now should work (if you reinstall the posteriordb R package). Let me know if it still doesn't work. I'm sorry for the hassle!

library(posteriordb)
library(rstan)
library(rjson)

pdb_path = "~/github/stan-dev/posteriordb"
Sys.setenv(PDB_PATH = pdb_path)
pdbl <- pdb_local()

# add data
metadata <-
  list(name = "dogs_log",
       keywords = c("ARM", "Ch. 24", "stan_examples"),
       title = "Logarithmic Mixed Effects Model",
       description = "y ~ exp(n_avoid + n_shock)",
       urls = "https://raw.githubusercontent.com/stan-dev/example-models/master/ARM/Ch.24/dogs_log.stan",
       references = "gelman2006data",
       added_date = Sys.Date(),
       added_by = "Bob Carpenter")
di <- as.pdb_data_info(metadata)

n_dogs <- 31
n_trials <- 25
y <-
  structure(c(0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1,
              1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
              1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0,
              1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1,
              0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0,
              1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0,
              0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1,
              1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
              1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0,
              0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
              0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
            .Dim = c(31, 25))
data_list <- list(n_dogs = n_dogs, n_trials = n_trials, y = y)
# Create the data object
dat <- as.pdb_data(data_list, info = di)
write_pdb(dat, pdbl, overwrite = TRUE)

# add model
# this feels super redundant at the metadata level
model_metadata <- metadata
model_metadata$framework = "stan"
mi <- as.pdb_model_info(model_metadata)
# if this has to be a system file, example instructions don't work

smc <- "
data {
  int<lower=0> n_trials;
  int<lower=0> n_dogs;
  int<lower=0, upper=1> y[n_dogs, n_trials];
}
transformed data {
  matrix[n_dogs, n_trials] n_avoid;
  matrix[n_dogs, n_trials] n_shock;
  for (n in 1:n_dogs) {
    n_avoid[n, 1] = 0;
    n_shock[n, 1] = 0;
    for (t in 2:n_trials) {
      n_avoid[n, t] = n_avoid[n, t - 1] + 1 - y[n, t - 1];
      n_shock[n, t] = n_shock[n, t - 1] + y[n, t - 1];
    }
  }
}
parameters {
  real<lower=-100, upper=0> beta1;
  real<lower=0, upper=100> beta2;
}
model {
  matrix[n_dogs, n_trials] logit_p = beta1 * n_avoid + beta2 * n_shock;

  // priors
  beta1 ~ uniform(-100, 0);
  beta2 ~ uniform(0, 100);

  // likelihood
  for (i in 1:n_dogs) {
    y[i] ~ bernoulli_logit(logit_p[i]);
  }
}
"
sm <- rstan::stan_model(model_code = smc)
mc <- as.model_code(sm, info = mi)
# this fails because files exist
write_pdb(mc, pdbl, overwrite = TRUE)

# add posterior object (what is this?)
post_metadata <- list(pdb_model_code = mc,
                      pdb_data = dat,
                      keywords = c("ARM", "Ch. 24", "stan_examples"),
                      urls = "https://raw.githubusercontent.com/stan-dev/example-models/master/ARM/Ch.24/dogs_log.stan",
                      references = "gelman and hill, arm ch. 24",
                      dimensions = list("dimensions" = 2, "dim" = 2),  ## no idea what this is and why it has dimensions twice
                      reference_posterior_name = NULL,
                      added_date = Sys.Date(),
                      added_by = "Bob Carpenter")
po <- as.pdb_posterior(post_metadata)
write_pdb(po, pdbl)
check_pdb_posterior(po)
bob-carpenter commented 2 years ago

Thanks, @MansMeg, that all sounds great. I take it you're going to finish this PR, which I'll then be happy to review.

MansMeg commented 2 years ago

Great! I have all these edits in the other PR, so I will close this one now. The PR with all the content is #248.

The other also has a lot of additional models (from Phil's groups) and all the fixes of the dogs models in that PR as well.

I will give you a heads up as soon as the PR is ready. hopefully in the coming days.