ROOT logo

From $ROOTSYS/tutorials/graphs/motorcycle.C

#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()
{
/******************************************************************************
* Author: Christian Stratowa, Vienna, Austria.                                *
* Created: 26 Aug 2001                            Last modified: 29 Sep 2001  *
******************************************************************************/

// 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).


// data taken from R library MASS: mcycle.txt
   TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
   dir.ReplaceAll("motorcycle.C","");
   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;
}

 motorcycle.C:1
 motorcycle.C:2
 motorcycle.C:3
 motorcycle.C:4
 motorcycle.C:5
 motorcycle.C:6
 motorcycle.C:7
 motorcycle.C:8
 motorcycle.C:9
 motorcycle.C:10
 motorcycle.C:11
 motorcycle.C:12
 motorcycle.C:13
 motorcycle.C:14
 motorcycle.C:15
 motorcycle.C:16
 motorcycle.C:17
 motorcycle.C:18
 motorcycle.C:19
 motorcycle.C:20
 motorcycle.C:21
 motorcycle.C:22
 motorcycle.C:23
 motorcycle.C:24
 motorcycle.C:25
 motorcycle.C:26
 motorcycle.C:27
 motorcycle.C:28
 motorcycle.C:29
 motorcycle.C:30
 motorcycle.C:31
 motorcycle.C:32
 motorcycle.C:33
 motorcycle.C:34
 motorcycle.C:35
 motorcycle.C:36
 motorcycle.C:37
 motorcycle.C:38
 motorcycle.C:39
 motorcycle.C:40
 motorcycle.C:41
 motorcycle.C:42
 motorcycle.C:43
 motorcycle.C:44
 motorcycle.C:45
 motorcycle.C:46
 motorcycle.C:47
 motorcycle.C:48
 motorcycle.C:49
 motorcycle.C:50
 motorcycle.C:51
 motorcycle.C:52
 motorcycle.C:53
 motorcycle.C:54
 motorcycle.C:55
 motorcycle.C:56
 motorcycle.C:57
 motorcycle.C:58
 motorcycle.C:59
 motorcycle.C:60
 motorcycle.C:61
 motorcycle.C:62
 motorcycle.C:63
 motorcycle.C:64
 motorcycle.C:65
 motorcycle.C:66
 motorcycle.C:67
 motorcycle.C:68
 motorcycle.C:69
 motorcycle.C:70
 motorcycle.C:71
 motorcycle.C:72
 motorcycle.C:73
 motorcycle.C:74
 motorcycle.C:75
 motorcycle.C:76
 motorcycle.C:77
 motorcycle.C:78
 motorcycle.C:79
 motorcycle.C:80
 motorcycle.C:81
 motorcycle.C:82
 motorcycle.C:83
 motorcycle.C:84
 motorcycle.C:85
 motorcycle.C:86
 motorcycle.C:87
 motorcycle.C:88
 motorcycle.C:89
 motorcycle.C:90
 motorcycle.C:91
 motorcycle.C:92
 motorcycle.C:93
 motorcycle.C:94
 motorcycle.C:95
 motorcycle.C:96
 motorcycle.C:97
 motorcycle.C:98
 motorcycle.C:99
 motorcycle.C:100
 motorcycle.C:101
 motorcycle.C:102
 motorcycle.C:103