{ "cells": [ { "cell_type": "markdown", "id": "08425e2a", "metadata": {}, "source": [ "# gr015_smooth\n", "as described in:\n", " Modern Applied Statistics with S-Plus, 3rd Edition\n", " W.N. Venables and B.D. Ripley\n", " Chapter 9: Smooth Regression, Figure 9.1\n", "\n", "Example is a set of data on 133 observations of acceleration against time\n", "for a simulated motorcycle accident, taken from Silverman (1985). The data\n", "are read from motorcycle.dat (2 columns of floats)\n", "\n", "\n", "\n", "**Author:** Christian Stratowa, Vienna, Austria \n", "This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:38 PM." ] }, { "cell_type": "code", "execution_count": null, "id": "8cd3a4b3", "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"TString.h\"\n", "#include \"TInterpreter.h\"\n", "#include \n", "#include \"TH1.h\"\n", "#include \"TGraphSmooth.h\"\n", "#include \"TCanvas.h\"\n", "#include \"TSystem.h\"\n", "\n", "\n", "TCanvas *vC1;\n", "TGraph *grin, *grout;" ] }, { "cell_type": "markdown", "id": "2675d69b", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": null, "id": "0097ff48", "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%cpp -d\n", "void DrawSmooth(Int_t pad, const char *title, const char *xt, const char *yt)\n", "{\n", " vC1->cd(pad);\n", " TH1F *vFrame = gPad->DrawFrame(0,-130,60,70);\n", " vFrame->SetTitle(title);\n", " vFrame->SetTitleSize(0.2);\n", " vFrame->SetXTitle(xt);\n", " vFrame->SetYTitle(yt);\n", " grin->Draw(\"P\");\n", " grout->DrawClone(\"LPX\");\n", "}" ] }, { "cell_type": "markdown", "id": "89d04f35", "metadata": {}, "source": [ "data taken from R library MASS: mcycle.txt" ] }, { "cell_type": "code", "execution_count": null, "id": "78909859", "metadata": { "collapsed": false }, "outputs": [], "source": [ "TString dir = gROOT->GetTutorialDir();\n", "dir.Append(\"/visualisation/graphs/\");\n", "dir.ReplaceAll(\"/./\",\"/\");" ] }, { "cell_type": "markdown", "id": "e2843938", "metadata": {}, "source": [ "read file and add to fit object" ] }, { "cell_type": "code", "execution_count": null, "id": "453349bc", "metadata": { "collapsed": false }, "outputs": [], "source": [ "Double_t *x = new Double_t[133];\n", "Double_t *y = new Double_t[133];\n", "Double_t vX, vY;\n", "Int_t vNData = 0;\n", "ifstream vInput;\n", "vInput.open(Form(\"%smotorcycle.dat\",dir.Data()));\n", "while (1) {\n", " vInput >> vX >> vY;\n", " if (!vInput.good()) break;\n", " x[vNData] = vX;\n", " y[vNData] = vY;\n", " vNData++;\n", "}//while\n", "vInput.close();\n", "grin = new TGraph(vNData,x,y);" ] }, { "cell_type": "markdown", "id": "5140e461", "metadata": {}, "source": [ "draw graph" ] }, { "cell_type": "code", "execution_count": null, "id": "97ed5c49", "metadata": { "collapsed": false }, "outputs": [], "source": [ "vC1 = new TCanvas(\"vC1\",\"Smooth Regression\",200,10,900,700);\n", "vC1->Divide(2,3);" ] }, { "cell_type": "markdown", "id": "9ae7fe86", "metadata": {}, "source": [ "Kernel Smoother\n", "create new kernel smoother and smooth data with bandwidth = 2.0" ] }, { "cell_type": "code", "execution_count": null, "id": "c59ab862", "metadata": { "collapsed": false }, "outputs": [], "source": [ "TGraphSmooth *gs = new TGraphSmooth(\"normal\");\n", "grout = gs->SmoothKern(grin,\"normal\",2.0);\n", "DrawSmooth(1,\"Kernel Smoother: bandwidth = 2.0\",\"times\",\"accel\");" ] }, { "cell_type": "markdown", "id": "6cbe27e5", "metadata": {}, "source": [ "redraw ksmooth with bandwidth = 5.0" ] }, { "cell_type": "code", "execution_count": null, "id": "1ad30cce", "metadata": { "collapsed": false }, "outputs": [], "source": [ "grout = gs->SmoothKern(grin,\"normal\",5.0);\n", "DrawSmooth(2,\"Kernel Smoother: bandwidth = 5.0\",\"\",\"\");" ] }, { "cell_type": "markdown", "id": "0cf41487", "metadata": {}, "source": [ "Lowess Smoother\n", "create new lowess smoother and smooth data with fraction f = 2/3" ] }, { "cell_type": "code", "execution_count": null, "id": "4a8eaf58", "metadata": { "collapsed": false }, "outputs": [], "source": [ "grout = gs->SmoothLowess(grin,\"\",0.67);\n", "DrawSmooth(3,\"Lowess: f = 2/3\",\"\",\"\");" ] }, { "cell_type": "markdown", "id": "b9acb700", "metadata": {}, "source": [ "redraw lowess with fraction f = 0.2" ] }, { "cell_type": "code", "execution_count": null, "id": "74fc0386", "metadata": { "collapsed": false }, "outputs": [], "source": [ "grout = gs->SmoothLowess(grin,\"\",0.2);\n", "DrawSmooth(4,\"Lowess: f = 0.2\",\"\",\"\");" ] }, { "cell_type": "markdown", "id": "9b9fb727", "metadata": {}, "source": [ "Super Smoother\n", "create new super smoother and smooth data with default bass = 0 and span = 0" ] }, { "cell_type": "code", "execution_count": null, "id": "a122c352", "metadata": { "collapsed": false }, "outputs": [], "source": [ "grout = gs->SmoothSuper(grin,\"\",0,0);\n", "DrawSmooth(5,\"Super Smoother: bass = 0\",\"\",\"\");" ] }, { "cell_type": "markdown", "id": "0c92e8dc", "metadata": {}, "source": [ "redraw supsmu with bass = 3 (smoother curve)" ] }, { "cell_type": "code", "execution_count": null, "id": "9a8e2c84", "metadata": { "collapsed": false }, "outputs": [], "source": [ "grout = gs->SmoothSuper(grin,\"\",3);\n", "DrawSmooth(6,\"Super Smoother: bass = 3\",\"\",\"\");" ] }, { "cell_type": "markdown", "id": "acc8eef9", "metadata": {}, "source": [ "cleanup" ] }, { "cell_type": "code", "execution_count": null, "id": "8f25c7c5", "metadata": { "collapsed": false }, "outputs": [], "source": [ "delete [] x;\n", "delete [] y;\n", "delete gs;" ] }, { "cell_type": "markdown", "id": "7e815ee3", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": null, "id": "fa8e5621", "metadata": { "collapsed": false }, "outputs": [], "source": [ "gROOT->GetListOfCanvases()->Draw()" ] } ], "metadata": { "kernelspec": { "display_name": "ROOT C++", "language": "c++", "name": "root" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".C", "mimetype": " text/x-c++src", "name": "c++" } }, "nbformat": 4, "nbformat_minor": 5 }