Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
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"
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];
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;
}
int Int_t
Definition RtypesCore.h:45
double Double_t
Definition RtypesCore.h:59
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#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
A helper class to smooth TGraph.
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:622
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
Author
Christian Stratowa, Vienna, Austria

Definition in file motorcycle.C.