Interpolation

More Information for R interpolation in http://stat.ethz.ch/R-manual/R-patched/library/stats/html/approxfun.html NOTE: this example illustrates an interpolation with random points given from ROOT and procedures made in R's environment.

Author: Omar Zapata
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Wednesday, June 23, 2021 at 11:36 AM.

In [1]:
%%cpp -d
#include<TRInterface.h>
#include<TRandom.h>
#include<vector>
In file included from input_line_41:1:
In file included from /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/TRInterface.h:15:
In file included from /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/TRObject.h:14:
/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/RExports.h:54:9: fatal error: 'RcppCommon.h' file not found
#include<RcppCommon.h>
        ^~~~~~~~~~~~~~
In [2]:
ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
In file included from libRInterface dictionary payload:5:
/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/RExports.h:54:9: fatal error: 'RcppCommon.h' file not found
#include<RcppCommon.h>
        ^~~~~~~~~~~~~~
Error in <TInterpreter::AutoParse>: Error parsing payload code for class ROOT::R::TRInterface with content:

#line 1 "libRInterface dictionary payload"


#define _BACKWARD_BACKWARD_WARNING_H
// Inline headers
#include "RExports.h"
#include "TRDataFrame.h"
#include "TRFunctionExport.h"
#include "TRFunctionImport.h"
#include "TRInterface.h"
#include "TRInternalFunction.h"
#include "TRObject.h"

#undef  _BACKWARD_BACKWARD_WARNING_H

input_line_42:2:35: error: incomplete type 'ROOT::R::TRInterface' named in nested name specifier
 ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
                         ~~~~~~~~~^~~~~~~~~~~~~
libRInterface dictionary forward declarations' payload:9:93: note: forward declaration of 'ROOT::R::TRInterface'
namespace ROOT{namespace R{class __attribute__((annotate("$clingAutoload$TRInterface.h")))  TRInterface;}}
                                                                                            ^

Creating points

In [3]:
TRandom rg;
std::vector<Double_t> x(10),y(10);
for(int i=0;i<10;i++)
{
   x[i]=i;
   y[i]=rg.Gaus();
}

r["x"]=x;
r["y"]=y;
input_line_51:2:3: error: use of undeclared identifier 'r'
 (r["x"] = ((*(std::vector<Double_t>*)0x7fbbac463088)))
  ^
Error in <HandleInterpreterException>: Error evaluating expression (r["x"] = ((*(std::vector<Double_t>*)0x7fbbac463088))).
Execution of your code was aborted.

Do plotting only in non-batch mode

In [4]:
if (!gROOT->IsBatch() )  {

   r<<"dev.new()";//Required to activate new window for plot
   //Plot parameter. Plotting using two rows and one column
   r<<"par(mfrow = c(2,1))";

   //plotting the points
   r<<"plot(x, y, main = 'approx(.) and approxfun(.)')";

   //The function "approx" returns a list with components x and y
   //containing n coordinates which interpolate the given data points according to the method (and rule) desired.
   r<<"points(approx(x, y), col = 2, pch = '*')";
   r<<"points(approx(x, y, method = 'constant'), col = 4, pch = '*')";
}
else {
   r << "print('Interpolated points')";
   r << "print(approx(x,y,n=20))";
}
input_line_53:2:3: error: use of undeclared identifier 'r'
 (r << "print('Interpolated points')")
  ^
Error in <HandleInterpreterException>: Error evaluating expression (r << "print('Interpolated points')").
Execution of your code was aborted.

The function "approxfun" returns a function performing (linear or constant) interpolation of the given data. For a given set of x values, this function will return the corresponding interpolated values.

In [5]:
r<<"f <- approxfun(x, y)";
input_line_55:2:3: error: use of undeclared identifier 'r'
 (r << "f <- approxfun(x, y)")
  ^
Error in <HandleInterpreterException>: Error evaluating expression (r << "f <- approxfun(x, y)").
Execution of your code was aborted.

Using approxfun with const method

In [6]:
r<<"fc <- approxfun(x, y, method = 'const')";

if (!gROOT->IsBatch() ) {
   r<<"curve(f(x), 0, 11, col = 'green2')";
   r<<"points(x, y)";

   r<<"curve(fc(x), 0, 10, col = 'darkblue', add = TRUE)";
   // different interpolation on left and right side :
   r<<"plot(approxfun(x, y, rule = 2:1), 0, 11,col = 'tomato', add = TRUE, lty = 3, lwd = 2)";
}
else {
   r << "x2=x+0.5";
   r << "print('Result of approxfun with default method')";
   r << "print(paste('x = ',x,'  f(x) = ',f(x2)))";
   r << "print('Result of approxfun with const method')";
   r << "print(paste('x = ',x,'  f(x) = ',fc(x2)))";
}
input_line_57:2:3: error: use of undeclared identifier 'r'
 (r << "fc <- approxfun(x, y, method = 'const')")
  ^
Error in <HandleInterpreterException>: Error evaluating expression (r << "fc <- approxfun(x, y, method = 'const')").
Execution of your code was aborted.