Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes parse_data() to remove trailing double underscores #1

Merged
merged 5 commits into from
Apr 20, 2021

Conversation

SteveBronder
Copy link

Summary:

Fixed for stan-dev/stanc3#865 so that rstan can correctly check the user data when the model uses Eigen::Map types.

Intended Effect:

Allow rstan to correctly parse data that's represented as an Eigen::Map

How to Verify:

I complied this rstan branch then ran the following R code

file.remove(system.file("stanc.js", package = "rstan"))
library(rstan)

model_code = "
data {
  int<lower=0> N;
  vector[N] weight;
  vector[N] diam1;
  vector[N] diam2;
  vector[N] canopy_height;
  vector[N] total_height;
  vector[N] density;
  vector[N] group;
}
transformed data {           // log transformations
  vector[N] log_weight = log(weight);
  vector[N] log_diam1 = log(diam1);
  vector[N] log_diam2 = log(diam2);
  vector[N] log_canopy_height = log(canopy_height);
  vector[N] log_total_height = log(total_height);
  vector[N] log_density = log(density);
  matrix[N,6] x = [log_diam1', log_diam2', log_canopy_height',
log_total_height', log_density', group']';
}
parameters {
  real alpha;
  vector[6] beta;
  real<lower=0> sigma;
}
model {
  log_weight ~ normal_id_glm(x, alpha, beta, sigma);
}
"

model_data = list(
  N = 46,
  weight = 
    c(401.3, 513.7, 1179.2, 308, 855.2, 268.7, 155.5, 1253.2, 328, 614.6,
      60.2, 269.6, 448.4, 120.4, 378.7, 266.4, 138.9, 1020.8, 635.7, 621.8, 579.8,
      326.8, 66.7, 68, 153.1, 256.4, 723, 4052, 345, 330.9, 163.5, 1160, 386.6, 693.5,
      674.4, 217.5, 771.3, 341.7, 125.7, 462.5, 64.5, 850.6, 226, 1745.1, 908, 213.5),
  diam1 = 
    c(1.8, 1.7, 2.8, 1.3, 3.3, 1.4, 1.5, 3.9, 1.8, 2.1, 0.8, 1.3, 1.2,
      1.5, 2.8, 1.4, 1.5, 2.4, 1.9, 2.3, 2.1, 2.4, 1, 1.3, 1.1, 1.3, 2.5, 5.2, 2, 1.6,
      1.4, 3.2, 1.9, 2.4, 2.5, 2.1, 2.4, 2.4, 1.9, 2.7, 1.3, 2.9, 2.1, 4.1, 2.8, 1.27),
  diam2 = 
    c(1.15, 1.35, 2.55, 0.85, 1.9, 1.4, 0.5, 2.3, 1.35, 1.6, 0.63, 0.95,
      0.9, 0.7, 1.7, 0.85, 0.6, 2.4, 1.55, 1.6, 1.7, 1.3, 0.4, 0.6, 0.7, 1.2, 2.3, 4,
      1.6, 1.6, 1, 1.9, 1.8, 2.4, 1.8, 1.5, 2.2, 1.7, 1.2, 2.5, 1.1, 2.7, 1, 3.8, 2.5,
      1),
  canopy_height = 
    c(1, 1.33, 0.6, 1.2, 1.05, 1, 0.9, 1.3, 0.6, 0.8, 0.6, 0.95,
      1.2, 0.7, 1.2, 1.1, 0.64, 1.2, 1.2, 1.3, 1, 0.9, 1, 0.5, 0.9, 0.6, 1.4, 2.5,
      1.4, 1.3, 1.1, 1.5, 0.8, 1.1, 1.3, 0.85, 1.5, 1.2, 1.15, 1.5, 0.7, 1.9, 1.5,
      1.5, 1.5, 0.62),
  total_height = 
    c(1.3, 1.35, 2.16, 1.8, 1.55, 1.2, 1, 1.7, 0.8, 1.2, 0.9, 1.35,
      1.4, 1, 1.7, 1.5, 0.65, 1.5, 1.7, 1.7, 1.5, 1.5, 1.2, 0.7, 1.2, 0.8, 1.7, 3,
      1.7, 1.6, 1.5, 1.9, 1.1, 1.6, 2, 1.25, 2, 1.3, 1.45, 2.2, 0.7, 1.9, 1.8, 2, 2.2,
      0.92),
  density = 
    c(1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
      1, 1, 1, 1, 5, 9, 1, 1, 1, 3, 1, 3, 7, 1, 2, 2, 2, 3, 1, 1, 2, 2, 1, 1),
  group = 
    c(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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
)

model_fit = stan(
  model_code = model_code,
  data = model_data,
  chains = 1,
  verbose = TRUE,
  model_name = "tester"
)

Side Effects:

Hopefully none

Documentation:

No new docs

Reviewer Suggestions:

Can you run this with your epi package? That should be a good check that this works

Copyright and Licensing

Please list the copyright holder for the work you are submitting (this will be you or your assignee, such as a university or company): Steve Bronder

By submitting this pull request, the copyright holder is agreeing to license the submitted work under the following licenses:

hsbadr and others added 2 commits April 20, 2021 12:33
…scores and regex matches to numerics for working with Eigen::Map types in the stan model
@SteveBronder SteveBronder changed the base branch from develop to stan_2.25 April 20, 2021 20:02
@SteveBronder
Copy link
Author

@hsbadr feel free to use this patch or yours with the $, I think the regex is right either way

rstan/rstan/R/misc.R Outdated Show resolved Hide resolved
@hsbadr
Copy link
Owner

hsbadr commented Apr 20, 2021

Thanks @SteveBronder! I've suggested a few edits and will merge after testing.

Co-authored-by: Hamada S. Badr <hamada.s.badr@gmail.com>
@SteveBronder
Copy link
Author

Sounds good!

@hsbadr hsbadr merged commit 36fa553 into hsbadr:stan_2.25 Apr 20, 2021
@hsbadr
Copy link
Owner

hsbadr commented Apr 20, 2021

@hsbadr feel free to use this patch or yours with the $, I think the regex is right either way

FYI. I've tested this and it fixes the issue, but cc02cf4 is required to avoid removing double underscores within a variable name (e.g., x__1 should not be parsed as x1).

@SteveBronder
Copy link
Author

Gotcha makes sense!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants