motorcycle.C

Go to the documentation of this file.
00001 #include "TString.h"
00002 #include "TInterpreter.h"
00003 #include <fstream>
00004 #include "TH1.h"
00005 #include "TGraphSmooth.h"
00006 #include "TCanvas.h"
00007 #include "TSystem.h"
00008 
00009 
00010 TCanvas *vC1;
00011 TGraph *grin, *grout;
00012 
00013 void DrawSmooth(Int_t pad, const char *title, const char *xt, const char *yt)
00014 {
00015    vC1->cd(pad);
00016    TH1F *vFrame = gPad->DrawFrame(0,-130,60,70);
00017    vFrame->SetTitle(title);
00018    vFrame->SetTitleSize(0.2);
00019    vFrame->SetXTitle(xt);
00020    vFrame->SetYTitle(yt);
00021    grin->Draw("P");
00022    grout->DrawClone("LPX");
00023 }
00024 
00025 
00026 void motorcycle()
00027 {
00028 /******************************************************************************
00029 * Author: Christian Stratowa, Vienna, Austria.                                *
00030 * Created: 26 Aug 2001                            Last modified: 29 Sep 2001  *
00031 ******************************************************************************/
00032 
00033 // Macro to test scatterplot smoothers: ksmooth, lowess, supsmu
00034 // as described in:
00035 //    Modern Applied Statistics with S-Plus, 3rd Edition
00036 //    W.N. Venables and B.D. Ripley
00037 //    Chapter 9: Smooth Regression, Figure 9.1
00038 //
00039 // Example is a set of data on 133 observations of acceleration against time
00040 // for a simulated motorcycle accident, taken from Silverman (1985).
00041 
00042 
00043 // data taken from R library MASS: mcycle.txt
00044    TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
00045    dir.ReplaceAll("motorcycle.C","");
00046    dir.ReplaceAll("/./","/");
00047 
00048 // read file and add to fit object
00049    Double_t *x = new Double_t[133];
00050    Double_t *y = new Double_t[133];
00051    Double_t vX, vY;
00052    Int_t vNData = 0;
00053    ifstream vInput;
00054    vInput.open(Form("%smotorcycle.dat",dir.Data()));
00055    while (1) {
00056       vInput >> vX >> vY;
00057       if (!vInput.good()) break;
00058       x[vNData] = vX;
00059       y[vNData] = vY;
00060       vNData++;
00061    }//while
00062    vInput.close();
00063    grin = new TGraph(vNData,x,y);
00064    
00065 // draw graph
00066    vC1 = new TCanvas("vC1","Smooth Regression",200,10,900,700);
00067    vC1->Divide(2,3);
00068 
00069 // Kernel Smoother
00070 // create new kernel smoother and smooth data with bandwidth = 2.0
00071    TGraphSmooth *gs = new TGraphSmooth("normal");
00072    grout = gs->SmoothKern(grin,"normal",2.0);
00073    DrawSmooth(1,"Kernel Smoother: bandwidth = 2.0","times","accel");
00074 
00075 // redraw ksmooth with bandwidth = 5.0
00076    grout = gs->SmoothKern(grin,"normal",5.0);
00077    DrawSmooth(2,"Kernel Smoother: bandwidth = 5.0","","");
00078 
00079 // Lowess Smoother
00080 // create new lowess smoother and smooth data with fraction f = 2/3
00081    grout = gs->SmoothLowess(grin,"",0.67);
00082    DrawSmooth(3,"Lowess: f = 2/3","","");
00083 
00084 // redraw lowess with fraction f = 0.2
00085    grout = gs->SmoothLowess(grin,"",0.2);
00086    DrawSmooth(4,"Lowess: f = 0.2","","");
00087 
00088 // Super Smoother
00089 // create new super smoother and smooth data with default bass = 0 and span = 0
00090    grout = gs->SmoothSuper(grin,"",0,0);
00091    DrawSmooth(5,"Super Smoother: bass = 0","","");
00092 
00093 // redraw supsmu with bass = 3 (smoother curve)
00094    grout = gs->SmoothSuper(grin,"",3);
00095    DrawSmooth(6,"Super Smoother: bass = 3","","");
00096 
00097 // cleanup
00098    delete [] x;
00099    delete [] y;
00100    delete gs;
00101 }
00102 

Generated on Tue Jul 5 15:44:22 2011 for ROOT_528-00b_version by  doxygen 1.5.1