Logo ROOT  
Reference Guide
motorcycle.C File Reference

Detailed Description

View in nbviewer Open in SWAN Macro to test scatterplot smoothers: ksmooth, lowess, supsmu as described in:

 Modern Applied Statistics with S-Plus, 3rd Edition
 W.N. Venables and B.D. Ripley
 Chapter 9: Smooth Regression, Figure 9.1

Example is a set of data on 133 observations of acceleration against time for a simulated motorcycle accident, taken from Silverman (1985).

#include "TString.h"
#include "TInterpreter.h"
#include <fstream>
#include "TH1.h"
#include "TGraphSmooth.h"
#include "TCanvas.h"
#include "TSystem.h"
TCanvas *vC1;
TGraph *grin, *grout;
void DrawSmooth(Int_t pad, const char *title, const char *xt, const char *yt)
{
vC1->cd(pad);
TH1F *vFrame = gPad->DrawFrame(0,-130,60,70);
vFrame->SetTitle(title);
vFrame->SetTitleSize(0.2);
vFrame->SetXTitle(xt);
vFrame->SetYTitle(yt);
grin->Draw("P");
grout->DrawClone("LPX");
}
void motorcycle()
{
// data taken from R library MASS: mcycle.txt
TString dir = gROOT->GetTutorialDir();
dir.Append("/graphs/");
dir.ReplaceAll("/./","/");
// read file and add to fit object
Double_t *x = new Double_t[133];
Double_t *y = new Double_t[133];
Double_t vX, vY;
Int_t vNData = 0;
ifstream vInput;
vInput.open(Form("%smotorcycle.dat",dir.Data()));
while (1) {
vInput >> vX >> vY;
if (!vInput.good()) break;
x[vNData] = vX;
y[vNData] = vY;
vNData++;
}//while
vInput.close();
grin = new TGraph(vNData,x,y);
// draw graph
vC1 = new TCanvas("vC1","Smooth Regression",200,10,900,700);
vC1->Divide(2,3);
// Kernel Smoother
// create new kernel smoother and smooth data with bandwidth = 2.0
TGraphSmooth *gs = new TGraphSmooth("normal");
grout = gs->SmoothKern(grin,"normal",2.0);
DrawSmooth(1,"Kernel Smoother: bandwidth = 2.0","times","accel");
// redraw ksmooth with bandwidth = 5.0
grout = gs->SmoothKern(grin,"normal",5.0);
DrawSmooth(2,"Kernel Smoother: bandwidth = 5.0","","");
// Lowess Smoother
// create new lowess smoother and smooth data with fraction f = 2/3
grout = gs->SmoothLowess(grin,"",0.67);
DrawSmooth(3,"Lowess: f = 2/3","","");
// redraw lowess with fraction f = 0.2
grout = gs->SmoothLowess(grin,"",0.2);
DrawSmooth(4,"Lowess: f = 0.2","","");
// Super Smoother
// create new super smoother and smooth data with default bass = 0 and span = 0
grout = gs->SmoothSuper(grin,"",0,0);
DrawSmooth(5,"Super Smoother: bass = 0","","");
// redraw supsmu with bass = 3 (smoother curve)
grout = gs->SmoothSuper(grin,"",3);
DrawSmooth(6,"Super Smoother: bass = 3","","");
// cleanup
delete [] x;
delete [] y;
delete gs;
}
Author
Christian Stratowa, Vienna, Austria

Definition in file motorcycle.C.

TGraph::Draw
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:760
TString::Data
const char * Data() const
Definition: TString.h:369
Form
char * Form(const char *fmt,...)
TObject::DrawClone
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:221
Int_t
int Int_t
Definition: RtypesCore.h:45
x
Double_t x[n]
Definition: legend1.C:17
TPad::Divide
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:1166
TCanvas.h
TH1::SetXTitle
virtual void SetXTitle(const char *title)
Definition: TH1.h:410
TString
Definition: TString.h:136
TH1::SetYTitle
virtual void SetYTitle(const char *title)
Definition: TH1.h:411
TString.h
TString::ReplaceAll
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:692
TH1::SetTitleSize
virtual void SetTitleSize(Float_t size=0.02, Option_t *axis="X")
Set the axis' title size.
Definition: Haxis.cxx:365
TH1::SetTitle
virtual void SetTitle(const char *title)
See GetStatOverflows for more information.
Definition: TH1.cxx:6344
TSystem.h
TGraphSmooth::SmoothSuper
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.
Definition: TGraphSmooth.cxx:482
TCanvas::cd
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:704
TString::Append
TString & Append(const char *cs)
Definition: TString.h:564
TGraphSmooth::SmoothKern
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.
Definition: TGraphSmooth.cxx:117
TGraphSmooth.h
y
Double_t y[n]
Definition: legend1.C:17
TGraphSmooth::SmoothLowess
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.
Definition: TGraphSmooth.cxx:223
Double_t
double Double_t
Definition: RtypesCore.h:59
TGraph
Definition: TGraph.h:41
TCanvas
Definition: TCanvas.h:23
TInterpreter.h
TH1F
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:572
gPad
#define gPad
Definition: TVirtualPad.h:287
TGraphSmooth
Definition: TGraphSmooth.h:36
TH1.h
gROOT
#define gROOT
Definition: TROOT.h:406