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

asked Apr 13, 2020 at 18:21
6
  • 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<Type>::operator()() [with Type = CppAD::AD<double>]’: 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<char>}) (CppAD::AD<double>)’ 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. Commented Apr 13, 2020 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! Commented Apr 14, 2020 at 12:24
  • I haven't gotten near solving it, you should post your solution to guide the blind. Commented Apr 14, 2020 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. Commented Apr 14, 2020 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. Commented Apr 14, 2020 at 21:28

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.