After I call the sdreport
function, I get an unexpected error: Error in MakeADFun(obj$env$data, obj$env$parameters, type = "ADFun", ADreport = TRUE, : 'data' must be a list
which is strange, because I pass the data as list:
simplex_reg_full.cpp:
// Simplex Regression
#include <TMB.hpp>
template<class Type>
Type objective_function<Type>::operator() ()
{
// Data provided from user
DATA_VECTOR(Y);
DATA_MATRIX(X);
// Parameter of the model
PARAMETER_VECTOR(Beta);
PARAMETER(log_sd);
// Variables used in the programming;
int n = Y.size();
vector<Type> eta(n);
vector<Type> d0(n);
vector<Type> mu(n);
Type pi = 3.141593;
Type nll = 0.0;
// Linear predictor
eta = X*Beta;
// logit is link function, and inv.logit is its inverse and is being applied
mu = 1/(1+exp(-eta));
for( int i=0; i<n; i++){
d0(i) = pow((Y(i)-mu(i)),2)/(Y(i)*(1-Y(i))*pow(mu(i),2)*pow((1-mu(i)),2));
nll -= - 0.5*log(2*pi) - 0.5*log(exp(log_sd)) - (3/2)*log(Y(i)*(1-Y(i))) - (1/(2*exp(log_sd)))*d0(i);
}
return nll;
}
R code:
compile('simplex_reg_full.cpp')
dyn.load(dynlib("simplex_reg_full"))
dados <- read.table("simplex.txt",header=TRUE)
data <- list(Y = dados$y,
X = cbind(1, dados[, "MEDIA"]))
obj <- MakeADFun(data = data,
parameters = list(Beta = c(0,0), log_sd = 2),
DLL = "simplex_reg_full",
inner.method = "newton", # Laplace
method = "BFGS",
hessian = T,
smartsearch=T)
opt <- do.call("optim", obj)
rep <- sdreport(opt)
Error in MakeADFun(obj$env$data, obj$env$parameters, type = "ADFun", ADreport = TRUE, :
'data' must be a list
- How to fix the error in
sdreport
? - Extra question:
PARAMETER_VECTOR(Beta)
is easier to code, however, the output fromMakeADFun
repeats Beta for every covariate. Is there a way to inform the program to use the name of the variable instead?
For question 1: It necessary to call
sdreport
for the object, nor the optimized function:sdreport(obj)
works.