TMB Documentation  v1.9.11
linreg.cpp
1 
2 #include <TMB.hpp>
3 template<class Type>
4 Type objective_function<Type>::operator() ()
5 {
6  DATA_VECTOR(y);
7  DATA_VECTOR(x);
8  PARAMETER(a);
9  PARAMETER(b);
10  PARAMETER(sd);
11  vector<Type> mu = a + b * x;
12  Type nll = -sum(dnorm(y, mu, sd, true));
13  SIMULATE {
14  y = rnorm(mu, sd); // Simulate response
15  REPORT(y); // Report the simulation
16  }
17  return nll;
18 }
19 
#define DATA_VECTOR(name)
Get data vector from R and declare it as vector<Type>
Definition: tmb_core.hpp:236
Includes and sets all stuff needed to compile the user defined objective function.
#define REPORT(name)
Report scalar, vector or array back to R without derivative information.
Definition: tmb_core.hpp:313
#define SIMULATE
Mark code that is only executed during simulation.
Definition: tmb_core.hpp:329
Type dnorm(Type x, Type mean, Type sd, int give_log=0)
Probability density function of the normal distribution.
Definition: dnorm.hpp:17
#define PARAMETER(name)
Get parameter scalar from R and declare it as Type.
Definition: tmb_core.hpp:227
Type sum(Vector< Type > x)
Definition: convenience.hpp:33
Type rnorm(Type mu, Type sigma)
Simulate from a normal distribution.
Vector class used by TMB.
Definition: tmbutils.hpp:18
License: GPL v2