Syntax error with in transformed parameters block

Hi everyone, I am trying to build a model and I’m getting the error:


No matches for:

vector[] + vector[]

Available argument signatures for operator+:

int + int
real + real
vector + vector
row vector + row vector
matrix + matrix
vector + real
row vector + real
matrix + real
real + vector
real + row vector
real + matrix
+row vector

expression is ill formed
error in ‘model_model_nb_baseline_seasons’ at line 46, column 74

45:   // getting mu in log form 
46:   log_mu_home = home_att[season, home_team] + away_def[season, away_team];
47:   log_mu_away = away_att[season, away_team] + home_def[season, home_team]; 

Error in stanc(model_code = paste(program, collapse = “\n”), model_name = model_cppname, :
failed to parse Stan model ‘model_nb_baseline_seasons’ due to the above error.

Has anyone encountered this problem and know a potential solution to this?


This is saying that you can’t add arrays of vectors together. Arrays in stan are just containers and so they don’t obey vector arithmetic. You should be able to pull out the vectors from the arrays and add those together.

what are the definitions for variables season, home_team, and away_team ?

given the error message, I’m guessing they’re of type int[], and you’re getting this one because Stan’s multi-indexing feature allows you to use int arrays as indexes.

the Stan compiler treats the expressions home_att[season, home_team] etc. as multi-indexed expressions, therefore the terms evaluate to type vector[] (array of vectors). This is very different than the expression home_att[1,2] where the indexes are scalars - that has type real.

As Jonah said, you need to loop over the arrays. However, looking at your code, I’m confused by how the dimensions are going to line up - log_mu_home is length ngames but the lengths of home_att etc are of length nteams.

Or use matrices instead of arrays of vectors.

It doesn’t matter how long home_att is. If log_mu_home is M x N, then season must be int[M] and home_team and away_team must be int[N].