Logo ROOT   6.10/09
Reference Guide
motorcycle.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_graphs
3 /// \notebook
4 /// Macro to test scatterplot smoothers: ksmooth, lowess, supsmu
5 /// as described in:
6 ///
7 /// Modern Applied Statistics with S-Plus, 3rd Edition
8 /// W.N. Venables and B.D. Ripley
9 /// Chapter 9: Smooth Regression, Figure 9.1
10 ///
11 /// Example is a set of data on 133 observations of acceleration against time
12 /// for a simulated motorcycle accident, taken from Silverman (1985).
13 ///
14 /// \macro_image
15 /// \macro_code
16 ///
17 /// \author Christian Stratowa, Vienna, Austria
18 
19 #include "TString.h"
20 #include "TInterpreter.h"
21 #include <fstream>
22 #include "TH1.h"
23 #include "TGraphSmooth.h"
24 #include "TCanvas.h"
25 #include "TSystem.h"
26 
27 
28 TCanvas *vC1;
29 TGraph *grin, *grout;
30 
31 void DrawSmooth(Int_t pad, const char *title, const char *xt, const char *yt)
32 {
33  vC1->cd(pad);
34  TH1F *vFrame = gPad->DrawFrame(0,-130,60,70);
35  vFrame->SetTitle(title);
36  vFrame->SetTitleSize(0.2);
37  vFrame->SetXTitle(xt);
38  vFrame->SetYTitle(yt);
39  grin->Draw("P");
40  grout->DrawClone("LPX");
41 }
42 
43 void motorcycle()
44 {
45 // data taken from R library MASS: mcycle.txt
46  TString dir = gROOT->GetTutorialDir();
47  dir.Append("/graphs/");
48  dir.ReplaceAll("/./","/");
49 
50 // read file and add to fit object
51  Double_t *x = new Double_t[133];
52  Double_t *y = new Double_t[133];
53  Double_t vX, vY;
54  Int_t vNData = 0;
55  ifstream vInput;
56  vInput.open(Form("%smotorcycle.dat",dir.Data()));
57  while (1) {
58  vInput >> vX >> vY;
59  if (!vInput.good()) break;
60  x[vNData] = vX;
61  y[vNData] = vY;
62  vNData++;
63  }//while
64  vInput.close();
65  grin = new TGraph(vNData,x,y);
66 
67 // draw graph
68  vC1 = new TCanvas("vC1","Smooth Regression",200,10,900,700);
69  vC1->Divide(2,3);
70 
71 // Kernel Smoother
72 // create new kernel smoother and smooth data with bandwidth = 2.0
73  TGraphSmooth *gs = new TGraphSmooth("normal");
74  grout = gs->SmoothKern(grin,"normal",2.0);
75  DrawSmooth(1,"Kernel Smoother: bandwidth = 2.0","times","accel");
76 
77 // redraw ksmooth with bandwidth = 5.0
78  grout = gs->SmoothKern(grin,"normal",5.0);
79  DrawSmooth(2,"Kernel Smoother: bandwidth = 5.0","","");
80 
81 // Lowess Smoother
82 // create new lowess smoother and smooth data with fraction f = 2/3
83  grout = gs->SmoothLowess(grin,"",0.67);
84  DrawSmooth(3,"Lowess: f = 2/3","","");
85 
86 // redraw lowess with fraction f = 0.2
87  grout = gs->SmoothLowess(grin,"",0.2);
88  DrawSmooth(4,"Lowess: f = 0.2","","");
89 
90 // Super Smoother
91 // create new super smoother and smooth data with default bass = 0 and span = 0
92  grout = gs->SmoothSuper(grin,"",0,0);
93  DrawSmooth(5,"Super Smoother: bass = 0","","");
94 
95 // redraw supsmu with bass = 3 (smoother curve)
96  grout = gs->SmoothSuper(grin,"",3);
97  DrawSmooth(6,"Super Smoother: bass = 3","","");
98 
99 // cleanup
100  delete [] x;
101  delete [] y;
102  delete gs;
103 }
TGraph * SmoothKern(TGraph *grin, Option_t *option="normal", Double_t bandwidth=0.5, Int_t nout=100, Double_t *xout=0)
Smooth data with Kernel smoother.
virtual TObject * DrawClone(Option_t *option="") const
Draw a clone of this object in the current selected pad for instance with: gROOT->SetSelectedPad(gPad...
Definition: TObject.cxx:226
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:640
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:679
#define gROOT
Definition: TROOT.h:375
Basic string class.
Definition: TString.h:129
tomato 1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:551
int Int_t
Definition: RtypesCore.h:41
virtual void SetYTitle(const char *title)
Definition: TH1.h:390
A helper class to smooth TGraph.
Definition: TGraphSmooth.h:36
TGraph * SmoothSuper(TGraph *grin, Option_t *option="", Double_t bass=0, Double_t span=0, Bool_t isPeriodic=kFALSE, Double_t *w=0)
Smooth data with Super smoother.
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:745
Double_t x[n]
Definition: legend1.C:17
TGraph * SmoothLowess(TGraph *grin, Option_t *option="", Double_t span=0.67, Int_t iter=3, Double_t delta=0)
Smooth data with Lowess smoother.
TString & Append(const char *cs)
Definition: TString.h:497
char * Form(const char *fmt,...)
The Canvas class.
Definition: TCanvas.h:31
double Double_t
Definition: RtypesCore.h:55
virtual void SetTitleSize(Float_t size=0.02, Option_t *axis="X")
Set the axis&#39; title size.
Definition: Haxis.cxx:365
Double_t y[n]
Definition: legend1.C:17
virtual void SetXTitle(const char *title)
Definition: TH1.h:389
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1135
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
#define gPad
Definition: TVirtualPad.h:284
virtual void SetTitle(const char *title)
Change (i.e.
Definition: TH1.cxx:6028
const char * Data() const
Definition: TString.h:347