示例#1
0
Type objective_function<Type>::operator() ()
{
  // Data
  DATA_INTEGER( n_y );
  DATA_INTEGER( n_s );
  DATA_IVECTOR( s_i );
  DATA_VECTOR( y_i );

  // Parameters
  PARAMETER( x0 );
  //PARAMETER( log_sdz ); //variability in site effect
  //PARAMETER_VECTOR( z_s );  //site effect

  // Objective funcction
  Type jnll = 0;

  // Probability of data conditional on fixed and random effect values
  vector<Type> ypred_i(n_y);
  for( int i=0; i<n_y; i++){
    ypred_i(i) = exp( x0 );//+ z_s(s_i(i)) );
    jnll -= dpois( y_i(i), ypred_i(i), true );
  }

  // Probability of random coefficients
  //for( int s=0; s<n_s; s++){
  //  jnll -= dnorm( z_s(s), Type(0.0), exp(log_sdz), true );
  //}

  // Reporting
  //Type sdz = exp(log_sdz);

  //REPORT( sdz );
  //REPORT( z_s );
  REPORT( x0 );

  //ADREPORT( sdz );
  //ADREPORT( z_s );
  ADREPORT( x0 );

  return jnll;
}
示例#2
0
Type objective_function<Type>::operator() () {
// data:
DATA_MATRIX(x_ij);
DATA_VECTOR(y_i);
DATA_IVECTOR(k_i); // vector of IDs
DATA_INTEGER(n_k); // number of IDs

// parameters:
PARAMETER_VECTOR(b_j)
PARAMETER_VECTOR(sigma_j);
PARAMETER(log_b0_sigma);
PARAMETER_VECTOR(b0_k);

int n_data = y_i.size(); // get number of data points to loop over

// Linear predictor
vector<Type> linear_predictor_i(n_data);
vector<Type> linear_predictor_sigma_i(n_data);
linear_predictor_i = x_ij*b_j;
linear_predictor_sigma_i = sqrt(exp(x_ij*sigma_j));

Type nll = 0.0; // initialize negative log likelihood

for(int i = 0; i < n_data; i++){
  nll -= dnorm(y_i(i), b0_k(k_i(i)) + linear_predictor_i(i) , linear_predictor_sigma_i(i), true);
}
for(int k = 0; k < n_k; k++){
  nll -= dnorm(b0_k(k), Type(0.0), exp(log_b0_sigma), true);
}

REPORT( b0_k );
REPORT(b_j );

ADREPORT( b0_k );
ADREPORT( b_j );

return nll;
}
示例#3
0
文件: track.cpp 项目: cavios/tshydro
Type objective_function<Type>::operator() ()
{
  DATA_VECTOR(height);
  DATA_VECTOR(times);
  DATA_IVECTOR(timeidx);
  DATA_IARRAY(trackinfo);
  DATA_VECTOR(weights);

  PARAMETER(logSigma);
  PARAMETER(logSigmaRW);
  PARAMETER(logitp);
  PARAMETER_VECTOR(u);

  int timeSteps=times.size();
  int obsDim=height.size();
  int noTracks=trackinfo.dim[0];

  Type p=ilogit(logitp); 
  
  Type ans=0;
 
  Type sdRW=exp(logSigmaRW);
  for(int i=1;i<timeSteps;i++)
    ans += -dnorm(u(i),u(i-1),sdRW*sqrt(times(i)-times(i-1)),true); 

  Type sdObs=exp(logSigma);
  for(int t=0;t<noTracks;t++){
    vector<Type> sub=height.segment(trackinfo(t,0),trackinfo(t,2));
    vector<Type> subw=weights.segment(trackinfo(t,0),trackinfo(t,2));
    for(int i=0;i<trackinfo(t,2);i++){
      ans += nldens(sub(i),u(timeidx(trackinfo(t,0))-1),sdObs/sqrt(subw(i)),p);
    } 
  }

  return ans;
}
示例#4
0
Type objective_function<Type>::operator() () {
// data:
DATA_MATRIX(x_ij); // fixed effect model matrix
DATA_MATRIX(x_sigma_ij); // fixed effect model matrix
DATA_VECTOR(y_i); // response vector
DATA_IVECTOR(pholder_i); // vector of IDs for strategy
DATA_IVECTOR(strategy_i); // vector of IDs for permit holder
DATA_INTEGER(n_pholder); // number of IDs for pholder
DATA_INTEGER(n_strategy); // number of IDs for strategy
DATA_INTEGER(diversity_column); // fixed effect column position of diversity
DATA_VECTOR(b1_cov_re_i); // predictor data for random slope
DATA_VECTOR(b2_cov_re_i); // predictor data for random slope
/* DATA_VECTOR(b3_cov_re_i); // predictor data for random slope */
DATA_VECTOR(g1_cov_re_i); // predictor data for random slope
DATA_IVECTOR(spec_div_all_1); // indicator for if there is variability in diversity

// parameters:
PARAMETER_VECTOR(b_j);
PARAMETER_VECTOR(sigma_j);
PARAMETER(log_b0_pholder_tau);
// PARAMETER(log_b1_pholder_tau);
PARAMETER(log_b0_strategy_tau);
PARAMETER(log_b1_strategy_tau);
PARAMETER(log_b2_strategy_tau);
/* PARAMETER(log_b3_strategy_tau); */
PARAMETER_VECTOR(b0_pholder);
// PARAMETER_VECTOR(b1_pholder);
PARAMETER_VECTOR(b0_strategy);
PARAMETER_VECTOR(b1_strategy);
PARAMETER_VECTOR(b2_strategy);
/* PARAMETER_VECTOR(b3_strategy); */

//PARAMETER(log_g0_pholder_tau);
PARAMETER(log_g0_strategy_tau);
PARAMETER(log_g1_strategy_tau);
// PARAMETER_VECTOR(g0_pholder);
PARAMETER_VECTOR(g0_strategy);
PARAMETER_VECTOR(g1_strategy);

int n_data = y_i.size();

// Linear predictor
vector<Type> linear_predictor_i(n_data);
vector<Type> linear_predictor_sigma_i(n_data);
vector<Type> eta(n_data);
vector<Type> eta_sigma(n_data);
linear_predictor_i = x_ij*b_j;
linear_predictor_sigma_i = x_sigma_ij*sigma_j;

/* // set slope deviations that we can't estimate to 0: */
/* for(int i = 0; i < n_data; i++){ */
/*   if(spec_div_all_1(strategy_i(i)) == 1) { */
/*     b1_strategy(strategy_i(i)) = 0; */
/*     g1_strategy(strategy_i(i)) = 0; */
/*   } */
/* } */

Type nll = 0.0; // initialize negative log likelihood

for(int i = 0; i < n_data; i++){

  eta(i) = b0_pholder(pholder_i(i)) +
      // b1_pholder(pholder_i(i)) * b1_cov_re_i(i) +
      b0_strategy(strategy_i(i)) +
      b1_strategy(strategy_i(i)) * b1_cov_re_i(i) +
      b2_strategy(strategy_i(i)) * b2_cov_re_i(i) +
      /* b3_strategy(strategy_i(i)) * b3_cov_re_i(i) + */
      linear_predictor_i(i);

  eta_sigma(i) = sqrt(exp(
          // g0_pholder(pholder_i(i)) +
          g0_strategy(strategy_i(i)) +
          g1_strategy(strategy_i(i)) * g1_cov_re_i(i) +
          linear_predictor_sigma_i(i)));

  nll -= dnorm(y_i(i), eta(i), eta_sigma(i), true);
}

for(int k = 0; k < n_pholder; k++){
  nll -= dnorm(b0_pholder(k), Type(0.0), exp(log_b0_pholder_tau), true);
  // nll -= dnorm(g0_pholder(k), Type(0.0), exp(log_g0_pholder_tau), true);
  // nll -= dnorm(b1_pholder(k), Type(0.0), exp(log_b1_pholder_tau), true);
}

for(int k = 0; k < n_strategy; k++){
  nll -= dnorm(b0_strategy(k), Type(0.0), exp(log_b0_strategy_tau), true);
  nll -= dnorm(g0_strategy(k), Type(0.0), exp(log_g0_strategy_tau), true);

  // only include these species diversity slope deviations
  // if there was sufficient variation in species diversity
  // to estimate them:
  if(spec_div_all_1(k) == 0) {
    nll -= dnorm(b1_strategy(k), Type(0.0), exp(log_b1_strategy_tau), true);
    nll -= dnorm(g1_strategy(k), Type(0.0), exp(log_g1_strategy_tau), true);
  }

  nll -= dnorm(b2_strategy(k), Type(0.0), exp(log_b2_strategy_tau), true);
  /* nll -= dnorm(b3_strategy(k), Type(0.0), exp(log_b3_strategy_tau), true); */
}

// Reporting
/* Type b0_pholder_tau = exp(log_b0_pholder_tau); */
/* Type b0_strategy_tau = exp(log_b0_strategy_tau); */
// Type b1_tau = exp(log_b1_tau);
/* Type g0_pholder_tau = exp(log_g0_pholder_tau); */
/* Type g0_strategy_tau = exp(log_g0_strategy_tau); */
// Type g1_tau = exp(log_g1_tau);

vector<Type> combined_b1_strategy(n_strategy);
vector<Type> combined_g1_strategy(n_strategy);
for(int k = 0; k < n_strategy; k++){
  // these are fixed-effect slopes + random-effect slopes
  combined_b1_strategy(k) = b_j(diversity_column) + b1_strategy(k);
  combined_g1_strategy(k) = sigma_j(diversity_column) + g1_strategy(k);
}

/* REPORT(b0_pholder); */
REPORT(b0_strategy);
REPORT(eta);
REPORT(b1_strategy);
REPORT(b_j);
/* REPORT(g0_pholder); */
REPORT(g0_strategy);
REPORT(g1_strategy);
/* REPORT(b0_tau); */
// REPORT(b1_tau);
/* REPORT(g0_tau); */
// REPORT(g1_tau);
REPORT(combined_b1_strategy);
REPORT(combined_g1_strategy);

// /* ADREPORT(b0_pholder); */
// ADREPORT(b0_strategy);
// ADREPORT(b1_strategy);
// ADREPORT(b_j);
// /* ADREPORT(g0_pholder); */
// ADREPORT(g0_strategy);
// ADREPORT(g1_strategy);
// /* ADREPORT(b0_tau); */
// ADREPORT(b1_tau);
// /* ADREPORT(g0_tau); */
// ADREPORT(g1_tau);
ADREPORT(combined_b1_strategy);
ADREPORT(combined_g1_strategy);

return nll;
}
Type objective_function<Type>::operator() () {
// data:
DATA_MATRIX(x_ij);
DATA_VECTOR(y_i);
DATA_IVECTOR(k_i); // vector of IDs
DATA_INTEGER(n_k); // number of IDs
DATA_INTEGER(n_j); // number of IDs
DATA_VECTOR(b1_cov_re_i); // predictor data for random slope
DATA_VECTOR(sigma1_cov_re_i); // predictor data for random slope
//DATA_VECTOR(sigma2_cov_re_i); // predictor data for random slope

// parameters:
PARAMETER_VECTOR(b_j)
PARAMETER_VECTOR(sigma_j);
PARAMETER(log_b0_sigma);
PARAMETER_VECTOR(b0_k);
PARAMETER(log_b1_sigma);
PARAMETER_VECTOR(b1_k);
PARAMETER(log_sigma0_sigma);
PARAMETER(log_sigma1_sigma);
PARAMETER_VECTOR(sigma0_k);
PARAMETER_VECTOR(sigma1_k);

int n_data = y_i.size(); // get number of data points to loop over

// Linear predictor
vector<Type> linear_predictor_i(n_data);
vector<Type> linear_predictor_sigma_i(n_data);
linear_predictor_i = x_ij*b_j;
linear_predictor_sigma_i = x_ij*sigma_j;

Type nll = 0.0; // initialize negative log likelihood

for(int i = 0; i < n_data; i++){
  nll -= dnorm(
      y_i(i),

      b0_k(k_i(i)) + b1_k(k_i(i)) * b1_cov_re_i(i) +
      linear_predictor_i(i),

      sqrt(exp(
          sigma0_k(k_i(i)) +
          sigma1_k(k_i(i)) * sigma1_cov_re_i(i) +
          linear_predictor_sigma_i(i))),

      true);
}
for(int k = 0; k < n_k; k++){
  nll -= dnorm(b0_k(k), Type(0.0), exp(log_b0_sigma), true);
  nll -= dnorm(b1_k(k), Type(0.0), exp(log_b1_sigma), true);
  nll -= dnorm(sigma0_k(k), Type(0.0), exp(log_sigma0_sigma), true);
  nll -= dnorm(sigma1_k(k), Type(0.0), exp(log_sigma1_sigma), true);
  //nll -= dnorm(sigma2_k(k), Type(0.0), exp(log_sigma2_sigma), true);
}

// Reporting
Type b0_sigma = exp(log_b0_sigma);
Type b1_sigma = exp(log_b1_sigma);
Type sigma0_sigma = exp(log_sigma0_sigma);
Type sigma1_sigma = exp(log_sigma1_sigma);
//Type sigma2_sigma = exp(log_sigma2_sigma);

vector<Type> b1_b1_k(n_k);
vector<Type> sigma1_sigma1_k(n_k);
for(int k = 0; k < n_k; k++){
  // these are fixed-effect slopes + random-effect slopes
  b1_b1_k(k) = b_j(n_j) + b1_k(k);
  sigma1_sigma1_k(k) = sigma_j(n_j) + sigma1_k(k);
}

REPORT( b0_k );
REPORT( b1_k );
REPORT( b_j );
REPORT( sigma0_k );
REPORT( sigma1_k );
//REPORT( sigma2_k );
REPORT(b0_sigma);
REPORT(b1_sigma);
REPORT(sigma0_sigma);
REPORT(sigma1_sigma);
//REPORT(sigma2_sigma);
REPORT(b1_b1_k);
REPORT(sigma1_sigma1_k);

//ADREPORT( b0_k );
//ADREPORT( b1_k );
//ADREPORT( b_j );
//ADREPORT( sigma0_k );
//ADREPORT( sigma1_k );
//ADREPORT( sigma2_k );
//ADREPORT(b0_sigma);
//ADREPORT(b1_sigma);
//ADREPORT(sigma0_sigma);
//ADREPORT(sigma1_sigma);
//ADREPORT(sigma2_sigma);
//ADREPORT(b1_b1_k);
//ADREPORT(sigma1_sigma1_k);

return nll;
}