Cov_exp_quad on a 2D grid: rstan error

Hi all,
I have N=45 measures of the air temperature made on fixed coordinates (x,y) and I would like to fit a 2D Gaussian Process.
The values are stored in a dataframe (named measured) with three columns(x, y, VALUE).
A snippet of my STAN code is :

data {
    int<lower=1> N; //number of observation
    vector[N] X[2]; //coordinates
    vector[N] Y; //observation
    ...
}

parameters {
    real<lower=0> rho;
    real<lower=0> sigma;
    real<lower=0> alpha;
   ...
}

model {
    matrix[N, N] K = cov_exp_quad(X, alpha, rho);
    ....

The structure of the STAN model for the GP follows the manual.
But when I try to run the model:

fit = sampling(model1, data = list(N=nrow(measured), X= matrix(c(measured$xsc,measured$ysc), nrow= nrow(measured)), Y=measured$VALUE), iter=200)

I receive the error

trying deprecated constructor; please alert package maintainer
Error in new_CppObject_xp(fields$.module, fields$.pointer, …) :
no valid constructor available for the argument list
failed to create the sampler; sampling not done

I understood that the reason of the error is in the use of the matrix notation between R and STAN for the input argument of the cov_exp_quad_function(...)

What is the correct data structure?

Session info ------------------------------------------------------------------
setting value
version R version 3.4.2 (2017-09-28)
system x86_64, linux-gnu
ui X11
language
collate en_US.UTF-8
tz Europe/Rome
date 2018-01-09
Packages ----------------------------------------------------------------------
package * version date source
rstan * 2.17.2 2017-12-21 CRAN (R 3.4.2)
StanHeaders * 2.17.1 2017-12-20 CRAN (R 3.4.2)

1 Like

Most likely something in the data block was not passed (properly). But we would need to see the whole Stan program and data list to verify that.

1 Like

I asked a very similar question recently here. What worked for me was to declare something like

row_vector[2] X[N];

with x and y coordinates effectively declared separately as real[N]. The assignment to the row vectors is not intuitively obvious.

1 Like

Thanks @Michael_Peck and @bgoodri for the precious help!
if the data section becomes

data {
    int<lower=1> N; //number of observation
    real X1[N]; 
    real X2[N];
    vector[N] Y; //observations
} 

and defining

 transformed data {
    row_vector[2] X[N];
    for (n in 1:N) {
        X[n, 1] = X1[n];
        X[n, 2] = X2[n];
    }
}

then the model

model {
    matrix[N, N] cov =   cov_exp_quad(X, alpha, rho) + diag_matrix(rep_vector(square(sigma), N));
    matrix[N, N] L_cov = cholesky_decompose(cov);
...
}

works and fits.

3 Likes