Logo ROOT   6.10/09
Reference Guide
Function.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_r
3 /// \notebook -nodraw
4 ///
5 /// \macro_code
6 ///
7 /// \author
8 
9 using namespace ROOT::R;
10 void Function()
11 {
13  r.SetVerbose(1);
14 
15  // Defining functions to be used from R
16  TRFunctionImport c("c");
17  TRFunctionImport rlist("list");
18  TRFunctionImport asformula("as.formula");
19  TRFunctionImport nls("nls");
20  TRFunctionImport confint("confint");
21  TRFunctionImport summary("summary");
22  TRFunctionImport print("print");
23  TRFunctionImport plot("plot");
24  TRFunctionImport lines("lines");
25  TRFunctionImport devnew("dev.new");
26  TRFunctionImport devoff("dev.off");
27  TRFunctionImport devcur("dev.cur");
28  TRFunctionImport rmin("min");
29  TRFunctionImport rmax("max");
30  TRFunctionImport seq("seq");
31  TRFunctionImport predict("predict");
32 
33  r<<"options(device='pdf')";
34 
35  // doing the procedure
36  TRObject xdata = c(-2,-1.64,-1.33,-0.7,0,0.45,1.2,1.64,2.32,2.9);
37  TRObject ydata = c(0.699369,0.700462,0.695354,1.03905,1.97389,2.41143,1.91091,0.919576,-0.730975,-1.42001);
38 
40  data["xdata"]=xdata;
41  data["ydata"]=ydata;
42 
43  // fit = nls(ydata ~ p1*cos(p2*xdata) + p2*sin(p1*xdata), start=list(p1=1,p2=0.2))
44  TRObject fit = nls(asformula("ydata ~ p1*cos(p2*xdata) + p2*sin(p1*xdata)"),Label["data"]=data, Label["start"]=rlist(Label["p1"]=1,Label["p2"]=0.2));
45  print(summary(fit));
46 
47  print(confint(fit));
48 
49  if (!gROOT->IsBatch()) {
50  devnew("Fitting Regression");
51  plot(xdata,ydata);
52 
53  TRObject xgrid=seq(rmin(xdata),rmax(xdata),Label["len"]=10);
54  lines(xgrid,predict(fit,xgrid),Label["col"] = "green");
55  devoff(Label["which"] = devcur() );
56  }
57 }
#define gROOT
Definition: TROOT.h:375
namespace associated R package for ROOT.
Definition: RExports.h:56
This is a class to get ROOT&#39;s objects from R&#39;s objects
Definition: TRObject.h:71
TRandom2 r(17)
Double_t(* Function)(Double_t)
Definition: Functor.C:4
This is a class to pass functions from ROOT to R.
static TRInterface & Instance()
static method to get an TRInterface instance reference
Rcpp::internal::NamedPlaceHolder Label
Definition: RExports.cxx:14
This is a class to create DataFrames from ROOT to R
Definition: TRDataFrame.h:177