Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
27TCanvas *vC1;
28TGraph *grin, *grout;
29
30void DrawSmooth(Int_t pad, const char *title, const char *xt, const char *yt)
31{
32 vC1->cd(pad);
33 TH1F *vFrame = gPad->DrawFrame(0, -130, 60, 70);
34 vFrame->SetTitle(title);
35 vFrame->SetTitleSize(0.2);
36 vFrame->SetXTitle(xt);
37 vFrame->SetYTitle(yt);
38 grin->Draw("P");
39 grout->DrawClone("LPX");
40}
41
42void motorcycle()
43{
44 // data taken from R library MASS: mcycle.txt
45 TString dir = gROOT->GetTutorialDir();
46 dir.Append("/visualisation/graphs/");
47 dir.ReplaceAll("/./", "/");
48
49 // read file and add to fit object
50 Double_t *x = new Double_t[133];
51 Double_t *y = new Double_t[133];
52 Double_t vX, vY;
53 Int_t vNData = 0;
54 ifstream vInput;
55 vInput.open(Form("%smotorcycle.dat", dir.Data()));
56 while (1) {
57 vInput >> vX >> vY;
58 if (!vInput.good())
59 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}
int Int_t
Definition RtypesCore.h:45
double Double_t
Definition RtypesCore.h:59
#define gROOT
Definition TROOT.h:406
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2489
#define gPad
The Canvas class.
Definition TCanvas.h:23
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
Definition TCanvas.cxx:719
A helper class to smooth TGraph.
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.
TGraph * SmoothSuper(TGraph *grin, Option_t *option="", Double_t bass=0, Double_t span=0, Bool_t isPeriodic=kFALSE, Double_t *w=nullptr)
Smooth data with Super smoother.
TGraph * SmoothKern(TGraph *grin, Option_t *option="normal", Double_t bandwidth=0.5, Int_t nout=100, Double_t *xout=nullptr)
Smooth data with Kernel smoother.
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
void Draw(Option_t *chopt="") override
Draw this graph with its current attributes.
Definition TGraph.cxx:833
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:623
virtual void SetTitleSize(Float_t size=0.02, Option_t *axis="X")
Set the axis' title size.
Definition Haxis.cxx:365
void SetTitle(const char *title) override
Change/set the title.
Definition TH1.cxx:6747
virtual void SetXTitle(const char *title)
Definition TH1.h:420
virtual void SetYTitle(const char *title)
Definition TH1.h:421
virtual TObject * DrawClone(Option_t *option="") const
Draw a clone of this object in the current selected pad with: gROOT->SetSelectedPad(c1).
Definition TObject.cxx:317
void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0) override
Automatic pad generation by division.
Definition TPad.cxx:1249
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:704
TString & Append(const char *cs)
Definition TString.h:572
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17