// Space time #include template Type objective_function::operator() () { // Data DATA_INTEGER(n_data); // Total number of observations DATA_VECTOR(Y); // Count data DATA_FACTOR(NAind); // 1 = Y is NA, 0 = is not NA DATA_FACTOR(NegBin); // Use negative binomial? 0:No; 1:Yes DATA_INTEGER(n_stations) // Number of stations DATA_FACTOR(meshidxloc); // Pointers into random effects vector x DATA_INTEGER(n_years) // Number of years DATA_INTEGER(n_p) // number of columns in covariate matrix X DATA_MATRIX(X); // Covariate design matrix // SPDE objects DATA_SPARSE_MATRIX(G0); DATA_SPARSE_MATRIX(G1); DATA_SPARSE_MATRIX(G2); // Fixed effects PARAMETER_VECTOR(alpha); // Mean of Gompertz-drift field PARAMETER(phi); // Offset of beginning from equilibrium PARAMETER(log_tau_E); // log-inverse SD of Epsilon PARAMETER(log_tau_O); // log-inverse SD of Omega PARAMETER(log_kappa); // Controls range of spatial variation PARAMETER(rho); // Autocorrelation (i.e. density dependence) PARAMETER_VECTOR(ln_VarInfl); // Overdispersion parameters // Gaussian random fields PARAMETER_ARRAY(Epsilon_input); // Spatial process variation PARAMETER_VECTOR(Omega_input); // Spatial variation in carrying capacity using namespace density; int i,j; Type g = 0; // spatial derived variables Type kappa2 = exp(2.0*log_kappa); Type kappa4 = kappa2*kappa2; Type pi = 3.141592; Type Range = sqrt(8) / exp( log_kappa ); Type SigmaE = 1 / sqrt(4*pi*exp(2*log_tau_E)*exp(2*log_kappa)); Type SigmaO = 1 / sqrt(4*pi*exp(2*log_tau_O)*exp(2*log_kappa)); Eigen::SparseMatrix Q = kappa4*G0 + Type(2.0)*kappa2*G1 + G2; // Probability of Gaussian random fields g += SEPARABLE(AR1(rho),GMRF(Q))(Epsilon_input); g += GMRF(Q)(Omega_input); // Likelihood contribution from observations vector eta(n_data); vector nu(n_data); vector mean_abundance(n_years); matrix Dji(n_stations,n_years); matrix Epsilon(n_stations,n_years); vector Omega(n_stations); vector Equil(n_stations); eta = X*alpha.matrix(); int ii = 0; for (int j=0;j