Incomplete final line found on model.stan


#1

I have the following R code

library(rstan)
library(tidyverse)

clean_data <- read_csv("clean_data.csv")
media_data <- clean_data %>% select(contains("m_"))
long_media_array <- c(clean_data$m_tv,clean_data$m_rd,clean_data$m_online)
# data Prep
N <- nrow(clean_data)
Y <- clean_data$sales
max_lag <- 13
num_media <- 3
lag_vec <- seq(0, max_lag - 1)
X_media <- array(data = media_data, dim = c(3,13))
X_media <- array(data = long_media_array, dim = c(nrow(clean_data),3,13))
num_ctrl <- 1
X_ctrl <- clean_data %>% select(price) %>% as.vector()

stan_data <- list(N=N, Y=Y, max_lag=max_lag, num_media=num_media,
                  lag_vec=lag_vec,X_media=X_media,
                  num_ctrl=num_ctrl,X_ctrl=X_ctrl)

m.stan <- stan(file = "model.stan",data = stan_data, iter = 3000, chains = 1)

and the following stan file

functions {
    // the Hill function
    real Hill(real t, real ec, real slope) {
      return 1 / (1 + (t / ec)^(-slope));
      }
    // the adstock transformation with a vector of weights
    real Adstock(row_vector t, row_vector weights) {
      return dot_product(t, weights) / sum(weights);
      }
}

data {
    // the total number of observations
    int<lower=1> N;
    // the vector of sales
    real<lower=0> Y[N];
    // the maximum duration of lag effect, in weeks
    int<lower=1> max_lag;
    // the number of media channels
    int<lower=1> num_media;
    // a vector of 0 to max_lag - 1
    row_vector[max_lag] lag_vec;
    // 3D array of media variables
    row_vector[max_lag] X_media[N, num_media];
    // the number of other control variables
    int<lower=1> num_ctrl;
    // a matrix of control variables
    row_vector[num_ctrl] X_ctrl[N];
}

parameters {
    // residual variance
    real<lower=0> noise_var;
    // the intercept
    real tau;
    // the coefficients for media variables
    vector<lower=0>[num_media] beta_medias;
    // coefficients for other control variables
    vector[num_ctrl] gamma_ctrl;
    // the retention rate and delay parameter for the adstock transformation of
    // each media
    vector<lower=0,upper=1>[num_media] retain_rate;
    vector<lower=0,upper=max_lag-1>[num_media] delay;
    // ec50 and slope for Hill function of each media
    vector<lower=0,upper=1>[num_media] ec;
    vector<lower=0>[num_media] slope;
}

transformed parameters {
    // a vector of the mean response
    real mu[N];
    // the cumulative media effect after adstock
    real cum_effect;
    // the cumulative media effect after adstock, and then Hill transformation
    row_vector[num_media] cum_effects_hill[N];
    row_vector[max_lag] lag_weights;

    for (nn in 1:N) {
      for (media in 1 : num_media) {
        for (lag in 1 : max_lag) {
          lag_weights[lag] = pow(retain_rate[media], (lag - 1 - delay[media]) ^ 2);
        }
      cum_effect = Adstock(X_media[nn, media], lag_weights);
      cum_effects_hill[nn, media] = Hill(cum_effect, ec[media], slope[media]);
      }
      mu[nn] = tau +
      dot_product(cum_effects_hill[nn], beta_medias) +
      dot_product(X_ctrl[nn], gamma_ctrl);
      }
}

model {
    retain_rate ~ beta(3,3);
    delay ~ uniform(0, max_lag - 1);
    slope ~ gamma(3, 1);
    ec ~ beta(2,2);
    tau ~ normal(0, 5);
    for (media_index in 1 : num_media) {
    beta_medias[media_index] ~ normal(0, 1);
    }
    for (ctrl_index in 1 : num_ctrl) {
    gamma_ctrl[ctrl_index] ~ normal(0,1);
    }
    noise_var ~ inv_gamma(0.05, 0.05 * 0.01);
    Y ~ normal(mu, sqrt(noise_var));
}

When I run the code I get the following warning

incomplete final line found on model.stan

The program runs, but it has the above warning and it is slow. I was wondering how I could resolve this warning and, if possible, speed up the computation.

The simulated data is here


#2

The error is persistent issue with how R parses text – just add a new line at the end of your Stan program to get ride of it. This will not, however, affect your model and hence will not address the performance issues.


#3

Thanks. That was probably the easiest fix I ever made to code. Also, I am not too concerned about the performance. I don’t think that I can do much to fix the speed. I am asking for estimates of many parameters.


#4

We try to strongly discourage this kind of prior as it provides much more information about the variable having large values than most users realize. We discuss in the manual and link to several papers Andrew Gelman wrote on the topic.

You can also vectorize those looped sampling statements.


#5

Hi, Thanks for looking at my code. I will checkout those priors in the manual. Which chapter should I be looking at?

Also, I find it difficult to vectorize triple nested for loops and vice versa. If you have any additional examples on this I would like to check that out also.