1

I am writing to implement a Simplex regression in this file (simplex_reg.cpp):

// Simplex Regression
#include <TMB.hpp>
template<class Type>
Type objective_function<Type>::operator() ()
{
  // Data provided from user
  DATA_VECTOR(Y);
  DATA_MATRIX(X);
  DATA_STRING(log);
  // Parameter of the model
  PARAMETER_VECTOR(Beta);
  // Variables used in the programming;
  int n = Y.size();
  vector<Type> eta(n); 
  vector<Type> d0(n); 
  vector<Type> mu(n); 
  Type sd = 0.0;
  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));
  // Density
  d0 = pow((Y-mu),2)/(Y*(1-Y)*pow(mu,2)*pow((1-mu),2));
  sd = sum(d0)/n;
  d0 = -0.5*log(2*pi) -0.5*log(sd) - (3/2)*log(Y*(1-Y)) - (1/(2*sd))*d0;

  if(log == "false"){
    d0 = exp(d0);
  }
  nll = -sum(d0);
  return nll;
}

However, it return an errors when I do compile('simplex_reg.cpp', "-O0 -g"). When I use gdbsource() as below it returns Program returned without errors:

library(TMB)
setwd("~/Google Drive/Mestrado/dissertacao/TMB")
compile('simplex_reg.cpp', "-O0 -g")
gdbsource("Teste.R")

I have already read the Compilation and run time errors, but I couldn't understand how gdbsourcereally works.

So, How do I run the debug/gdbsource into "simplex_reg.cpp" file to find my mistake?

Thanks in advance

  • building in terminal ubuntu 18.04 I get the following: `-I/home/chris/R-361/R-3.6.1/src/include simplex_reg.cpp: In instantiation of ‘Type objective_function::operator()() [with Type = CppAD::AD]’: tmb_core.hpp:1135:7: required from here simplex_reg.cpp:28:16: error: no match for call to ‘(std::__cxx11::string {aka std::__cxx11::basic_string}) (CppAD::AD)’ d0 = -0.5*log(2*pi) -0.5*log(sd) - (3/2)*log(Y*(1-Y)) - (1/(2*sd))*d0;` as first error. And same within R using your > `compile('simplex_reg.cpp', '-O0 -ggdb')`. Is as far as I've gotten. – Chris Apr 13 '20 at 21:46
  • 1
    Tks Chris! Did you look through all the compilation messages to find this message? (I already solved this problem, but I want to know for the next day). When you said "building in terminal ubuntu", which commands did you use? If you reply in the answer section I will give you the credits, thanks in advance! – Guilherme Parreira Apr 14 '20 at 12:24
  • I haven't gotten near solving it, you should post your solution to guide the blind. – Chris Apr 14 '20 at 15:47
  • I got the solution via commenting the code and running smaller parts of it. But I didn't understand the workflow of `gdbsource()`. For example, I didn't manage to find the error message that you found, this is the path that I need. – Guilherme Parreira Apr 14 '20 at 17:18
  • gdbsource() only comes into play after simplex_reg.o or .so has been fully compiled, like working with with an R script in debug. You can str() objects and values and watch execution, but without the .o, nothing happens. This is what is misleading about the `Program exited without error` after running >gdbsource(simplex_reg.cpp), because the message refers to gdb, not simplex_reg.cpp. While nonsensical, I ran it many times. My approach to c++ is akin to 1000 monkeys with typewriters attempting Rilke poems. And I'd lost my path to your Teste.R, alas. – Chris Apr 14 '20 at 21:28
  • When I run `gdbsource("simplex_reg.cpp", interactive = T)` after `compile("simplex_reg.cpp", "-O0 -g")` it opens *GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git* and I am able to type. However, when I try to type some R code or call any variable of my `.cpp` nothing happens. After that I need to kill the process to be able to use R again. – Guilherme Parreira Apr 29 '20 at 21:14

0 Answers0