Help with simple measurement model, doesnt sample

Hello, I am new to stan and I could not get the following simple measurement error model to sample.
I am using rstan_2.19.2, r version 3.6.1, under macOS Catalina 10.15.1

model {
data {
    int <lower=0> N;            \\ number of obs 
    vector[N] E;                    \\ obs error 
    vector[N] I_obs;              \\ observations         
}
parameters {
     vector[N] I_true;               \\ unknown true value
     real I_tmu;                         \\ prior location
     real<lower=0> sigma_t;     \\ prior scale
}
 model {
     sigma_t ~ exponential(1);
     I_tmu ~ normal(0,3);
     I_true[N] ~ normal(I_tmu, sigma_t);    \\ prior
     I_obs ~ normal(I_true, E);                   \\ measurement model
 }

"

the error message was

model{
starting worker pid=10312 on localhost:11250 at 17:55:58.824
starting worker pid=10326 on localhost:11250 at 17:55:59.195
starting worker pid=10340 on localhost:11250 at 17:55:59.549
starting worker pid=10354 on localhost:11250 at 17:55:59.932

SAMPLING FOR MODEL '68b329da9893e34099c7d8ad5cb9c940' NOW (CHAIN 1).
[1] "Error in sampler$call_sampler(args_list[[i]]) : "                      
[2] "  Must use algorithm=\"Fixed_param\" for model that has no parameters."
error occurred during calling the sampler; sampling not done

SAMPLING FOR MODEL '68b329da9893e34099c7d8ad5cb9c940' NOW (CHAIN 2).
[1] "Error in sampler$call_sampler(args_list[[i]]) : "                      
[2] "  Must use algorithm=\"Fixed_param\" for model that has no parameters."
error occurred during calling the sampler; sampling not done

SAMPLING FOR MODEL '68b329da9893e34099c7d8ad5cb9c940' NOW (CHAIN 3).
[1] "Error in sampler$call_sampler(args_list[[i]]) : "                      
[2] "  Must use algorithm=\"Fixed_param\" for model that has no parameters."
error occurred during calling the sampler; sampling not done

SAMPLING FOR MODEL '68b329da9893e34099c7d8ad5cb9c940' NOW (CHAIN 4).
[1] "Error in sampler$call_sampler(args_list[[i]]) : "                      
[2] "  Must use algorithm=\"Fixed_param\" for model that has no parameters."
error occurred during calling the sampler; sampling not done
some chains had errors; consider specifying chains = 1 to debughere are whatever error messages were returned
[[1]]
Stan model '68b329da9893e34099c7d8ad5cb9c940' does not contain samples.

[[2]]
Stan model '68b329da9893e34099c7d8ad5cb9c940' does not contain samples.

[[3]]
Stan model '68b329da9893e34099c7d8ad5cb9c940' does not contain samples.

[[4]]
Stan model '68b329da9893e34099c7d8ad5cb9c940' does not contain samples.

I really appreciate any direction

1 Like

Can you share the data you are feeding into the model?

2 Likes

Its 2938 rows, the only data are N = 2938, then I_obs and E are of this form

          I_obs         E
1  -0.97396764 0.3427592
2  -0.29606615 0.3170522
3  -0.53237956 0.6635246
4  -1.26179014 0.2863943
5  -1.09079139 0.2116538
6  -0.81877390 0.2790631
7   0.19208005 0.6341045
8   0.16170778 0.1854708
9   0.15951793 0.5944968
10 -0.03871113 0.2455489

1 Like

Part of the issues is your slashes. They should be // not \ . At least on my Ubuntu machine in R.

testdata ← read.csv(“./Desktop/test_data.txt”, sep=“\t”,
na.strings=c(‘NA’,‘.’,‘#VALUE!’) )
testdata
dat ← list(
“N” = nrow(testdata),
“E” = testdata$E,
“I_obs” = testdata$I_obs)
str(dat)

then the model looks like this

simple_model ← "
data {
int<lower=0> N; //number of obs
vector[N] E; //obs error
vector[N] I_obs; //observations
}
parameters {
vector[N] I_true; //unknown true value
real I_tmu; //prior location
real<lower=0> sigma_t; //prior scale
}
model {
sigma_t ~ exponential(1);
I_tmu ~ normal(0,3);
I_true[N] ~ normal(I_tmu, sigma_t); //prior
I_obs ~ normal(I_true, E); //measurement model
}

"

2 Likes

Ah ok, thank you! did that sample for you?
I have the same error message as before

Hmmm yeah it sampled. This might be some weirdness on the OSX end of things.

Oh! Can you share your whole R code?

model { 
data { 
int <lower=0> N; \\ number of obs 
vector[N] E; \\ obs error 
vector[N] I_obs; \\ observations 
}

Is that model { a typo or is that in your code? Because that could be tripping the parser up

1 Like

That was a typo from “stan model” : / , Ara_winter’s code produced the same error message for me, thank you!

It produced the same error with those 10 rows, it might be the catalina thing, I thought I had fixed that, thank you for helping me!

@Duncan You are welcome! @andrjohns also has a good suggestion to check your model code.

Can you post the R syntax that you’re using to run the model? That might be where the issue is

Sure, thank you for looking, here it is using 10 cases and the simple model from @Ara_Winter,

library(rstan)

samp_list <- list(I_obs  = c(-0.97396764, -0.29606615, -0.53237956, -1.26179014, -1.09079139, -0.81877390, 0.19208005, 0.16170778, 0.15951793, -0.03871113), E = c(0.3427592, 0.3170522, 0.6635246, 0.2863943, 0.2116538, 0.2790631, 0.6341045, 0.1854708, 0.5944968, 0.2455489), N = 10)

simple_model <- "
data {
int<lower=0> N; //number of obs
vector[N] E; //obs error
vector[N] I_obs; //observations
}

parameters {
vector[N] I_true; //unknown true value
real I_tmu; //prior location
real<lower=0> sigma_t; //prior scale
}

model {
sigma_t ~ exponential(1);
I_tmu ~ normal(0,3);
I_true[N] ~ normal(I_tmu, sigma_t); //prior
I_obs ~ normal(I_true, E); //measurement model
}

"

stan_model <- stan(fit = simple_model, data = samp_list, cores = 4, chains = 4, warmup = 500, iter = 1e4)

When I run it, it produces

starting worker pid=901 on localhost:11832 at 20:49:11.202
starting worker pid=915 on localhost:11832 at 20:49:11.510
starting worker pid=929 on localhost:11832 at 20:49:11.812
starting worker pid=943 on localhost:11832 at 20:49:12.126

SAMPLING FOR MODEL '68b329da9893e34099c7d8ad5cb9c940' NOW (CHAIN 1).
[1] "Error in sampler$call_sampler(args_list[[i]]) : "                      
[2] "  Must use algorithm=\"Fixed_param\" for model that has no parameters."
error occurred during calling the sampler; sampling not done

SAMPLING FOR MODEL '68b329da9893e34099c7d8ad5cb9c940' NOW (CHAIN 2).
[1] "Error in sampler$call_sampler(args_list[[i]]) : "                      
[2] "  Must use algorithm=\"Fixed_param\" for model that has no parameters."
error occurred during calling the sampler; sampling not done

SAMPLING FOR MODEL '68b329da9893e34099c7d8ad5cb9c940' NOW (CHAIN 3).
[1] "Error in sampler$call_sampler(args_list[[i]]) : "                      
[2] "  Must use algorithm=\"Fixed_param\" for model that has no parameters."
error occurred during calling the sampler; sampling not done

Thanks! So the problem is coming from this part of the stan call: fit = simple_model. The fit option is used with existing stanfit objects (i.e. you’ve already compiled and fit a model and you want to re-estimate it).

You want to be using model_code = simple_model instead:

stan_model <- stan(model_code = simple_model, data = samp_list, cores = 4, chains = 4, warmup = 500, iter = 1e4)
2 Likes

Interesting when I run

stan_model ← stan(fit = simple_model, data = samp_list, cores = 4, chains = 4, warmup = 500, iter = 1e4)

I get those same errors.

but when I run

fit1 ← stan(model_code = simple_model,
model_name = “simplemodel”,
data = dat,
chains=4, iter=2000, refresh=0,
seed=12345, cores = 6,
init = “random”,
control=list(stepsize=0.001, adapt_delta=0.9999, max_treedepth = 14))

I don’t.

EDIT: @andrjohns got it!

1 Like

Ah ok, thank you @andrjohns and @Ara_Winter so much for your help!

2 Likes