I want to write a Stan function that is analogous to colSums() in R, for some matrix modeling. My understanding is that this doesn’t exist in Stan (see Rowsum or columnsum of a matrix - #2 by bgoodri). I wrote the following function in the functions block, which is syntactically correct:
vector colSums(matrix M){
int ncol;
vector[ncol] sums;
ncol = cols(M);
for(i in 1:ncol){
sums[i] = sum(M[,i]); //sums[i] = sum(col(M,i));
}
return(sums);
}
However, when I actually apply this function to a matrix in a Stan model, I get this error:
Exception: Exception: Found negative dimension size in variable declaration; variable=sums; dimension size expression=ncol; expression value=-2147483648
… at the line where I ran colSums(M)
. Hopefully this is just a naïve error, but can anyone tell what I’ve done wrong?