data-manipulationstanrstanhierarchical-bayesianpystan

stan to manipulate and fix syntax for data


max_lag is a fixed integer number for all media. I need to have a specific lag for each media. So, how can I have a different lag for every media, and how data and parameters have to change into the syntax? For Example: max_lag_channel_media1 = 10; max_lag_channel_media2 = 4; max_lag_channel_media3 = 6

model_data2 = {
    'N': len(df),
    'max_lag': max_lag, 
    'num_media': num_media,
    'X_media': X_media, 
    'mu_mdip': mu_mdip,
    'num_ctrl': X_ctrl.shape[1],
    'X_ctrl': X_ctrl, 
    'y': df_mmm['sales'].values
}

model_code2 = '''
functions {
  // the adstock transformation with a vector of weights
  real Adstock(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 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;
  // matrix of media variables
  matrix[N+max_lag-1, num_media] X_media;
  // vector of media variables' mean
  real mu_mdip[num_media];
  // the number of other control variables
  int<lower=1> num_ctrl;
  // a matrix of control variables
  matrix[N, num_ctrl] X_ctrl;
}
parameters {
  // residual variance
  real<lower=0> noise_var;
  // the intercept
  real tau;
  // the coefficients for media variables and base sales
  vector<lower=0>[num_media+num_ctrl] beta;
  // the decay and peak parameter for the adstock transformation of
  // each media
  vector<lower=0,upper=1>[num_media] decay;
  vector<lower=0,upper=ceil(max_lag/2)>[num_media] peak;
}
transformed parameters {
  // the cumulative media effect after adstock
  real cum_effect;
  // matrix of media variables after adstock
  matrix[N, num_media] X_media_adstocked;
  // matrix of all predictors
  matrix[N, num_media+num_ctrl] X;
  
  // adstock, mean-center, log1p transformation
  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[max_lag-lag+1] <- pow(decay[media], (lag - 1 - peak[media]) ^ 2);
      }
     cum_effect <- Adstock(sub_col(X_media, nn, media, max_lag), lag_weights);
     X_media_adstocked[nn, media] <- log1p(cum_effect/mu_mdip[media]);
    }
  X <- append_col(X_media_adstocked, X_ctrl);
  } 
}
model {
  decay ~ beta(3,3);
  peak ~ uniform(0, ceil(max_lag/2));
  tau ~ normal(0, 5);
  for (i in 1 : num_media+num_ctrl) {
    beta[i] ~ normal(0, 1);
  }
  noise_var ~ inv_gamma(0.05, 0.05 * 0.01);
  y ~ normal(tau + X * beta, sqrt(noise_var));
}
'''

Solution

  • In your data, make max_lag an array of integers rather than a single integer; each element should store the max lag for one medium. Like this:

    int<lower=1> max_lag[num_media];
    

    Then, when you build X_media_adstocked, use the correct max_lag on each iteration of the loop. You'll need to define lag_weights inside the loop rather than beforehand, because it will have a different length every time:

    for (nn in 1:N) {
      for (medium in 1 : num_media) {
        row_vector[max_lag[medium]] lag_weights;
        for (lag in 1 : max_lag[medium]) {
          lag_weights[max_lag[medium]-lag+1] <- pow(decay[medium], (lag - 1 - peak[medium]) ^ 2);
        }
        cum_effect <- Adstock(sub_col(X_media, nn, medium, max_lag[medium]), lag_weights);
        X_media_adstocked[nn, medium] <- log1p(cum_effect/mu_mdip[medium]);
      }
    }