(******************************************************************* This file was generated automatically by the Mathematica front end. It contains Initialization cells from a Notebook file, which typically will have the same name as this file except ending in ".nb" instead of ".m". This file is intended to be loaded into the Mathematica kernel using the package loading commands Get or Needs. Doing so is equivalent to using the Evaluate Initialization Cells menu command in the front end. DO NOT EDIT THIS FILE. This entire file is regenerated automatically each time the parent Notebook file is saved in the Mathematica front end. Any changes you make to this file will be overwritten. ***********************************************************************) Off[General::spell1] <1||Length[types]>1||First[types]=!=List, Message[WriteDataFile::length];Close[output];Return[], lens=First[lens]]; k=Length[varSymbols]; WriteString[output,"\"",doc,"\"\n"]; (*Do[WriteString[output,varSymbols[[i]]," "],{i,k}]; WriteString[output,"\n"]; w[{x__}]:=Write[output,x]; w/@Transpose[data];*) Write[output, TableForm[Transpose[data],TableHeadings\[Rule]{None,varSymbols}, TableSpacing\[Rule]{0,2}]]; Close[output];] Bin::usage= "Bin[x_List, y_List] returns a nested list of the values of y which share \ a common value of x. The first element in each item of the result is the \ binning value."; BinPercentage::usage= "BinPercentage controls the proportion of data binned by Bin."; BinWidth::usage="BinWidth sets an absolute window size for binning data."; Options[Bin]={BinPercentage\[Rule].2,BinWidth\[Rule]Automatic}; Bin::TooMany="Appear to have too many (`1`) bins."; (*collect y values with common x*) Bin[x_List,y_List]:=With[ {ux=Union[x]}, If[(Length[ux]>.9 Length[x])&&(Length[ux]>10), Message[Bin::TooMany,Length[ux]];{x,y}, (*else*) {#,y[[Flatten[Position[x,#]]]]}&/@ux] ] (*collect values of y with similar values of x*) Bin[x_List,y_List,opts___?OptionQ]:= Module[ {in=If[OrderedQ[x], Range[Length[x]], Sort[Range[Length[x]],(x[[#1]]hi,--i];hiAt=i;hi=sx[[hiAt]]; {{fn[[1]]-.1*iqr,fn[[5]]+.1*iqr}, Join[If[fill,{GrayLevel[0.9], Polygon[{{fn[[2]],center-w},{fn[[4]],center-w},{fn[[4]], center+w},{fn[[2]],center+w}}], GrayLevel[ 0.0]},{}],{Line[{{fn[[2]],center},{lo,center}}],(*whiskers*) Line[{{fn[[4]],center},{hi,center}}], Line[{{fn[[3]],center-w},{fn[[3]],center+w}}],(*median line*) Line[{{fn[[2]],center-w},{fn[[4]],center-w},(*border*){fn[[4]], center+w},{fn[[2]],center+w},{fn[[2]],center-w}}], AbsolutePointSize[4]},Point[{#,center}]&/@sx[[Range[1,lowAt-1]]], Point[{#,center}]&/@sx[[Range[hiAt+1,n]]]]}] \!\(\(\( (*\ \(os\ = \ Sqrt[2]\ Table[ InverseErf[N[2\ \((i/\((n + 1)\))\) - 1]], {i, n}];\)\ *) \)\(\[IndentingNewLine]\)\( (*\ \(os\ = \ Quantile[NormalDistribution[], Range[n]\/\(n + 1\)];\)\ *) \)\)\) \!\(QuantilePlot[x_List] := Module[\[IndentingNewLine]{n = Length[x], \ \[IndentingNewLine]m\ = \ Mean[x], \ \[IndentingNewLine]s\ = \ StandardDeviation[x]}, \[IndentingNewLine]os\ = \@2\ Table[ InverseErf[N[2\ \((i/\((n + 1)\))\) - 1]], {i, n}]; \[IndentingNewLine]QuantilePlot[m\ + \ s\ os, x]\[IndentingNewLine]]\) ScatterPlot::usage="ScatterPlot[x_, y_] displays a scatter plot of y on x."; ScatterPlotFunction::usage= "ScatterplotFunction defines a function to be superimposed on a \ scatterplot."; ScatterPlotMatrix::usage= "ScatterplotMatrix[x_,...] draws a scatter plot matrix ofthe collection \ of input lists."; scat[plotFunction_,data_,opts___] := Module[ {pf,lp,x, dispFunc=DisplayFunction/.{opts}/.Options[ScatterPlot], f=ScatterPlotFunction/.{opts}/.{ScatterPlotFunction\[Rule]None} }, lp=plotFunction[data,PlotRange\[Rule]All,DisplayFunction\[Rule]Identity, FilterOptions[plotFunction,opts]]; If[f===None, Show[lp,DisplayFunction\[Rule]dispFunc], (*else*) x=First/@data; pf=Plot[f[z],{z,Min[x],Max[x]},DisplayFunction\[Rule]Identity]; Show[lp,pf,DisplayFunction\[Rule]dispFunc] ]] Options[ScatterPlot]= Join[{ScatterPlotFunction\[Rule]None},Options[ListPlot]]; ScatterPlot[{x_,y_},opts___?OptionQ]:=ScatterPlot[x,y,opts] ScatterPlot[x_List,opts___?OptionQ]:= scat[ListPlot,Transpose[{Range[Length[x]],x}],opts] ScatterPlot[x_List,y_List,opts___?OptionQ]:= scat[ListPlot,Transpose[{x,y}],opts]; ScatterPlot[x_List,y_List,labels_List,opts___?OptionQ]:= scat[LabeledListPlot,Transpose[{x,y,labels}],opts]; ScatterPlot3D[x_List,y_List,z_List,opts___?OptionQ]:= ListPlot3D[Transpose[{x,y,z}],opts] Attributes[ScatterPlotMatrix] = HoldAll; MakeLabel[x_]:=StringDrop[StringDrop[ToString[x],-1],5] ScatterPlotMatrix[varSeq__]:=Module[ {held=Hold[varSeq], vars={varSeq}, i,j,k,c}, k=Length[held]; Show[GraphicsArray[ Table[ If[j>i, ListPlot[Transpose[{vars[[j]],vars[[i]]}],AxesLabel\[Rule]None, Axes\[Rule]None,DisplayFunction\[Rule]Identity,Frame\[Rule]True, FrameTicks\[Rule]None],(*else show correlation or name*) If[j\[Equal]i, Graphics[Text[MakeLabel[HeldPart[held,i]],{0,0}]], c=Correlation[vars[[i]],vars[[j]]]; Graphics[Text[N[c,2],{0,0}]]]],{i,1,k},{j,1,k}]] ]] Smooth::usage= "Smooth[x_,y_,options_] generates a piecewise linear scatterplot \ smoother."; SmoothSkipPercentage::usage= "SmoothSkipPercentage determines proportion of data skiped between \ evaluations."; SmoothBinPercentage::usage= "SmoothBinPercentage sets the proportion of data used for linear fit."; Options[Smooth]={SmoothSkipPercentage\[Rule].05, SmoothBinPercentage\[Rule].40}; Smooth[x_List,y_List,opts___?OptionQ]:=Module[ {skip,pct,in, sx=x, sy=y, n=Length[x]}, {skip, pct}={SmoothSkipPercentage,SmoothBinPercentage}/.{opts}/.Options[ Smooth]; If[Not[OrderedQ[x]], in=Ordering[x];sx=x\[LeftDoubleBracket]in\[RightDoubleBracket]; sy=y\[LeftDoubleBracket]in\[RightDoubleBracket]]; smthByPct[sx,sy,Ceiling[pct*n],Ceiling[skip*n]] ] fit[x_,y_,at_]:=Module[ {xBar=Mean[x], xDev,b}, xDev=x-xBar; b=First[{xDev.y}/{xDev.xDev}]; If[ListQ[at], Transpose[{at,Mean[y]+b (at-xBar)}], (*else*) {at,Mean[y]+b (at-xBar)}] ] (*assumes sorted on x*) smthByPct[x_,y_,binSize_,skip_]:=Module[ {n=Length[x], half=Floor[binSize/(2 skip)], (*number at each end*) xLo,xHi,max,min}, min=x[[1]];max=x[[n]]; xLo=min+(Mean[x[[Range[binSize]]]]-min) Range[0,half-1]/half; xHi=max+(Mean[x[[n+1-Range[binSize]]]]-max) Reverse[Range[0,half-1]]/ half; (*Print["binSize=",binSize," skip=",skip];*) Interpolation[ Join[ fit[x[[Range[binSize]]],y[[Range[binSize]]],xLo], MapThread[fit[#1,#2,Mean[#1]]&,{Partition[x,binSize,skip], Partition[y,binSize,skip]}], fit[x[[n+1-Range[binSize]]],y[[n+1-Range[binSize]]], xHi]] ]] OLS::usage= "OLS[x_,y_] returns the fitted values and residuals from a least squares \ regression of y on x."; Residuals::usage= "Residuals determines if regression should compute residuals or return \ the fitted parameters."; RobustRegression::usage= "RobustRegression[x_List, y_, opts___] builds a robust regression model \ and prints a summary of the fit. By default, the program uses a biweight \ influence function."; RobustConstant::usage= "RobustConstant sets returns the robustness constant used in estimating \ the coefficients in a robust regression."; RobustTolerance::usage= "RobustTolerance is maximum percentage change permitted for the robust \ regression iterations to converge."; RobustWeightFunction::usage= "RobustWeightFunction is an option of Robust regression used to set the \ weight function in robust regression."; HuberWeightFunction::usage= "HuberWeightFunction is the Huber weight function for a robust \ regression."; BiweightWeightFunction::usage= "BiweightWeightFunction is the biweight weight function for a robust \ regression."; Attributes[getNames] = {HoldFirst}; getNames[n_] :=Table[Extract[Hold[n],{1,i},HoldForm],{i,Length[n]}] ols[x_,y_]:=Block[{q,r},{q,r}=QRDecomposition[x]; Inverse[r].q.y] wls[x_,y_,w_]:=With[{sr=Sqrt[w]},ols[sr x,sr y]] diag[m_]:=Transpose[m,{1,1}] Options[OLS]:={Constant\[Rule]True,Residuals\[Rule]True}; Attributes[OLS]={HoldFirst}; OLS[xNames_?MatrixQ,iny_List,opts___?OptionQ]:=Module[ {q,r,ri,x=xNames,xtxi,xMat,b,z,se,s2, const,resids,k,yx,y,n}, yx=Join[{iny},x]; y=yx[[1]];x=Drop[yx,1]; n=Length[y]; {const,resids}={Constant,Residuals}/.{opts}/.Options[OLS]; If[const, xMat=Prepend[x,Table[1,{n}]], xMat=x]; {q,r}=QRDecomposition[Transpose[xMat]]; ri=Inverse[r]; b=ri.q.y; k=Length[b]; z=y-b.xMat; s2=(z.z)/(n-k); se=N[Sqrt[s2 diag[ri.Transpose[ri]]]]; Print[ TableForm[ Transpose[ {Join[{"Factor"}, If[const,{1},{}], getNames[xNames]],Join[{"Coefficient"},b], Join[{"Std. Err."},se],Join[{"t-Stat"},b/se]}]]]; Print[""]; Print["s = ",Sqrt[s2]," R",Superscript[2]," = ", 1-(z.z)/((n-1)Variance[y])]; If[resids,{y-z,z},b]] OLS[x_,y_,opts___?OptionQ]:=OLS[{x},y,opts] Attributes[HuberWeightFunction]={Listable}; HuberWeightFunction[x_/;(Abs[x]<1)]:=1 HuberWeightFunction[x_]:=1/Abs[x]; Attributes[BiweightWeightFunction]={Listable}; BiweightWeightFunction[x_/;(Abs[x]<1)]:=(1-x^2)^2 BiweightWeightFunction[x_]=0; (*Suggested scaling constants*) RobustConstant[HuberWeightFunction]=1.365; RobustConstant[BiweightWeightFunction]=4.685; (*Convergence test*) RobustTolerance=0.001; convQ[x_,y_]:=Max[Abs[x-y]/x]