(*********************************************************************** Mathematica-Compatible Notebook This notebook can be used on any computer system with Mathematica 4.0, MathReader 4.0, or any compatible application. The data for the notebook starts with the line containing stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info@wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. ***********************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 8482, 209]*) (*NotebookOutlinePosition[ 9597, 244]*) (* CellTagsIndexPosition[ 9553, 240]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["Loess local regression", "Section"], Cell[CellGroupData[{ Cell["Acknowledgements", "Subsubsection"], Cell[TextData[{ "This code was jointly developed by Luci Ellis, RBA, and David Burmaster, \ Alceon Corp, 1997\[Dash]98. See \"Visualising Data\" for more information \ about the procedure. The procedure requires the ", StyleBox["simpleLinRegress[] ", FontWeight->"Bold"], "function developed by Luci Ellis, from the econometric_functions.nb \ notebook." }], "Text"] }, Open ]], Cell[CellGroupData[{ Cell["Startup", "Subsubsection"], Cell[TextData[{ "The cell below just loads in some ", StyleBox["Mathematica", FontSlant->"Italic"], " add-ons." }], "Text"], Cell["\<\ Needs[\"Statistics`NormalDistribution`\"]; Needs[\"Statistics`ContinuousDistributions`\"]; Needs[\"Statistics`DataManipulation`\"]; Needs[\"Statistics`DescriptiveStatistics`\"]; Needs[\"Statistics`MultiDescriptiveStatistics`\"]; Needs[\"Statistics`LinearRegression`\"]; Needs[\"Statistics`DataSmoothing`\"]; Needs[\"Graphics`Graphics`\"];\ \>", "Input", AspectRatioFixed->True], Cell[BoxData[ \(EGloess::usage\ = \ "\"\)], "Input"], Cell[BoxData[ \(EGloessX::usage\ = \ "\"\)], "Input"], Cell[TextData[{ "EGloess[\txypairs:{..{_Real,_Real}},\n\t\t\tnwtfn_\t\t\t/; 1 <= nwtfn,\n\t\ \t\tnpoly_Integer\t/; 1 == npoly || 2 == npoly,\n\t\t\tndiv_Integer\t/; 10 \ <= ndiv,\n\t\t\tswin_\t\t\t/; 0 < swin\t\t] :=\n\t\t\t", StyleBox["(*the conditions for the function to be valid*)", FontColor->GrayLevel[0.500008]], "\nModule[{xvec, yvec, xmin, xmax, wtfn, yloesspt,\n\t\txloessvec, \ yloessvec, wtvecvec,xypairssorted,regressdata},\n\t\t", StyleBox["(* these are the names for the local variables *)\n\t\t", FontColor->GrayLevel[0.500008]], "\nxypairssorted = Sort[xypairs];\n{xvec, yvec}\t=\t\ Transpose[xypairssorted];\n{xmin, xmax}\t=\t{First[xvec], Last[xvec]};\n", StyleBox["(* all pretty straight-forward *)", FontColor->GrayLevel[0.500008]], "\n\nxloessvec =\t\n(N[xmin + # ((xmax-xmin)/(ndiv-1))]&) /@ \ Range[0,ndiv-1];\n", StyleBox["(* these are the points at which the fit is evaluated *)", FontColor->GrayLevel[0.500008]], "\nwtvecvec =\t\n\tTable[N[Exp[-Abs[((xvec\[LeftDoubleBracket]i\ \[RightDoubleBracket]-xloessvec\[LeftDoubleBracket]j\[RightDoubleBracket])/\ swin)^nwtfn]]],\n\t{j, 1, ndiv},{i, 1, Length[xypairs]}];\n", StyleBox[ "(* a matrix where the rows are the vector of weights for each evaluation \ of the WLS fit *)\n", FontColor->GrayLevel[0.500008]], "\nregressdata = If[npoly==1,Reverse /@ \ xypairssorted,Transpose[Join[{yvec},{xvec},{xvec}^2]]];\n", StyleBox[ "(* puts the data in a form appropriate for use in the simpleLinRegress[] \ auxiliary function *)\n", FontColor->GrayLevel[0.500008]], "\nyloessvec=\tTable[(Table[q^k,{k,0,npoly}]. \t\n\t\t\ Evaluate[simpleLinRegress[ regressdata, \t\n\t\tGLSWeights->wtvecvec\ \[LeftDoubleBracket]jj\[RightDoubleBracket] ]]\[LeftDoubleBracket]1\ \[RightDoubleBracket]) /. q->xloessvec\[LeftDoubleBracket]jj\ \[RightDoubleBracket],\n\t\t\t\t {jj, 1, ndiv}];\n", StyleBox["(* finds the WLS-fitted values for y at gridpoints of x *)", FontColor->GrayLevel[0.500008]], "\n\nTranspose[{xloessvec, yloessvec}]\n", StyleBox["(* returns vector of ndiv {x,y} pairs for loess points *)", FontColor->GrayLevel[0.500008]], "\n]" }], "Input"], Cell[TextData[{ "EGloessX[\txypairs:{..{_Real,_Real}},\n\t\t\tnwtfn_\t\t\t/; 1 <= nwtfn,\n\t\ \t\tnpoly_Integer\t/; 1 == npoly || 2 == npoly,\n\t\t\tndiv_Integer /; 10 \ <= ndiv,\n\t\t\tsamplesize_Integer\t/; 20 <= samplesize,\n\t\t\tswin_\t\t\t/; \ 0 < swin\t\t] :=\n\t\t\t", StyleBox["(*the conditions for the function to be valid*)", FontColor->GrayLevel[0.500008]], "\nModule[{xvec, yvec, xmin, xmax, wtfn, yloesspt,\n\t\txloessvec, \ yloessvec, wtvecvec,xypairssorted,baseregressdata,regressdata},\n\t\t", StyleBox["(* these are the names for the local variables *)\n\t\t", FontColor->GrayLevel[0.500008]], "\nxypairssorted = Sort[xypairs];\n{xvec, yvec}\t=\t\ Transpose[xypairssorted];\n{xmin, xmax}\t=\t{First[xvec], Last[xvec]};\n", StyleBox["(* all pretty straight-forward *)", FontColor->GrayLevel[0.500008]], "\n\nxloessvec =\t\n(N[xmin + # ((xmax-xmin)/(ndiv-1))]&) /@ \ Range[0,ndiv-1];\n", StyleBox["(* these are the points at which the fit is evaluated *)", FontColor->GrayLevel[0.500008]], "\n", StyleBox[ "wtvecvec =\t\n\tTable[N[Exp[-Abs[((xvec\[LeftDoubleBracket]i\ \[RightDoubleBracket]-xloessvec\[LeftDoubleBracket]j\[RightDoubleBracket])/\ swin)^nwtfn]]],\n\t{j, 1, ndiv},{i, 1, Length[xypairs]}];", FontColor->RGBColor[0, 0, 1]], "\n", StyleBox[ "(* a matrix where the rows are the vector of weights for each evaluation \ of the WLS fit *)\n", FontColor->GrayLevel[0.500008]], "\nbaseregressdata = If[npoly==1,Reverse /@ xypairssorted, \ Transpose[Join[{yvec},{xvec},{xvec}^2]]];\n", StyleBox[ "(* puts the data in a form appropriate for use in the simpleLinRegress[] \ auxiliary function *)", FontColor->GrayLevel[0.500008]], "\n\nregressdata = If[npoly==1, \n(Take[#,samplesize]&)/@ \ (RotateLeft[baseregressdata, Max[0,Floor[Position[xvec, \ Floor[Select[xvec,Min[Abs[xvec-#]] ]] ]-(samplesize/2)]] ] &/@xloessvec);\n", StyleBox[ "(* Partitions the data into multiple samples of the desired size, \ adjusting for end-sample constraints *)\n", FontColor->GrayLevel[0.500008]], "\nyloessvec=\tTable[(Table[q^k,{k,0,npoly}]. \t\n\t\t\ Evaluate[simpleLinRegress[ regressdata, \t\n\t\tGLSWeights->wtvecvec\ \[LeftDoubleBracket]jj\[RightDoubleBracket] ]]\[LeftDoubleBracket]1\ \[RightDoubleBracket]) /. q->xloessvec\[LeftDoubleBracket]jj\ \[RightDoubleBracket],\n\t\t\t\t {jj, 1, ndiv}];\n", StyleBox["(* finds the WLS-fitted values for y at gridpoints of x *)", FontColor->GrayLevel[0.500008]], "\n\nTranspose[{xloessvec, yloessvec}]\n", StyleBox["(* returns vector of ndiv {x,y} pairs for loess points *)", FontColor->GrayLevel[0.500008]], "\n]" }], "Input"] }, Open ]] }, Open ]] }, FrontEndVersion->"4.0 for Macintosh", ScreenRectangle->{{0, 1152}, {0, 850}}, PrintingStyleEnvironment->"Printout", WindowToolbars->{}, WindowSize->{629, 756}, WindowMargins->{{4, Automatic}, {Automatic, 1}}, PrintingCopies->1, PrintingPageRange->{1, Automatic}, PrintingOptions->{"PrintingMargins"->{{36, 36}, {36, 36}}, "PrintCellBrackets"->False, "PrintRegistrationMarks"->False, "PrintMultipleHorizontalPages"->False}, MacintoshSystemPageSetup->"\<\ 00<0004/0B`000003EL9@?oNomL=TPV60fL5N`?P0080004/0B`000000]P2:001 0000I00000400`<30?l00BL?00400@4EPJ000000000006P801T1T00000008000 00000000004000000000000000000000\>" ] (*********************************************************************** Cached data follows. If you edit this Notebook file directly, not using Mathematica, you must remove the line containing CacheID at the top of the file. The cache data will then be recreated when you save this file from within Mathematica. ***********************************************************************) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[1739, 51, 41, 0, 50, "Section"], Cell[CellGroupData[{ Cell[1805, 55, 41, 0, 42, "Subsubsection"], Cell[1849, 57, 378, 8, 64, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[2264, 70, 32, 0, 42, "Subsubsection"], Cell[2299, 72, 133, 5, 32, "Text"], Cell[2435, 79, 389, 10, 132, "Input"], Cell[2827, 91, 374, 6, 123, "Input"], Cell[3204, 99, 377, 6, 123, "Input"], Cell[3584, 107, 2186, 42, 582, "Input"], Cell[5773, 151, 2681, 54, 717, "Input"] }, Open ]] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)