GeoTest
A program which implements F, Wald, LR and LM tests geometrically

Enis SINIKSARAN
saran@istanbul.edu.tr
Istanbul Universitesi , Iktisat Fak.,
Ekonometri Bolumu, Beyazit-Istanbul
Turkey

Version : 5.2 
Last revision : January 2006

GeoTest Function

Off[∞::"indet"]
Off[
      General::"stop"]
Off[
      General::"spell1"]
Off[
      General::"dbyz"]
Needs["Statistics`ContinuousDistributions`"]
Needs["Statistics`LinearRegression`"]
Needs["DiscreteMath`Combinatorica`"]
GeoTest[data_, list_List, alpha_]:=
Module[{length, cosangle, angle, dep, rr, regUnRes, 
              UnRestrictedresiduals, regressors, os, r, resdata, regressRes, 
              Restrictedresiduals, theta, rere, ee, cF, F, Wald, LR, LM, 
              cWald, cLR, cLM, qW, qF, f, FPDF, pvalueF, g, ChiPDF, pvalueW, 
              pvalueLR, pvalueLM, index, beta, ind, betas, inds, all, 
              resbeta, allres, str, model, null},
length[vect_] := N[Sqrt[vect . vect]];
cosangle[vect1_, 
                  vect2_ ] := (vect1 . vect2)/(length[vect1]*
                      length[vect2]);
angle[vect1_, vect2_ ] := 
                ArcCos[cosangle[vect1, vect2]]*(180/Pi);
n=Length[data];
k = Length[Transpose[data]];
hh = 
                Select[list, # > (k - 1) || # == 0  &];
hyp::"urp" = "indices of \
parameters in the list are probably wrong";
hyp::"tmp" = "number of \
parameters in list must be smaller than the number of independent variables \
";
hyp::"rep" = "there is a \
replication in the list ";
alp::"mho" = "level of \
significance alpha must be a number between zero and one ";
If[Length[hh] ≠ 0, 
                Message[hyp::"urp"]];
If[k - 1 ≤ Length[list], 
                Message[hyp::"tmp"]];
If[
                Length[list] != Length[Union[list]], 
                Message[hyp::"rep"]];
If[
                Length[list] != Length[Flatten[list]], 
                Message[hyp::"urp"]];
If[
                alpha ≥ 1 || alpha ≤ 0, 
                Message[alp::"mho"]];
If[Length[hh] ≠ 0 || 
                    k - 1 ≤ Length[list] || 
                    Length[list] != Length[Union[list]] || 
                    Length[list] != Length[Flatten[list]] || 
                    alpha ≥ 1 || alpha ≤ 0,Abort[],
dep = Transpose[
                          data][[Length[Transpose[data]]]];
rr = 
                      Table[(Subscript[u, i]), {i, 1, Length[Transpose[data]] - 1}];
regUnRes = 
                      Regress[data, Flatten[Insert[{1}, {rr}, 2]], rr, 
                        RegressionReport -> {FitResiduals}];
UnRestrictedresiduals = 
                      FitResiduals /. regUnRes;
regressors = 
                      Table[i, {i, 1, Length[Transpose[data]] - 1}];
os = Complement[regressors, list];
r = Length[list];
resdata = 
                      Transpose[
                        Insert[
                          Table[Transpose[data][[os[[i]]]], {i, 
                              1, Length[os]}], dep, Length[os] + 1]];
rr = 
                      Table[(Subscript[u, i]), {i, 1, Length[Transpose[resdata]] - 1}];
regressRes = 
                      Regress[resdata, Flatten[Insert[{1}, {rr}, 2]], rr, 
                        RegressionReport -> {FitResiduals}];
Restrictedresiduals = 
                      FitResiduals /. regressRes;
theta = 
                      angle[UnRestrictedresiduals, Restrictedresiduals];
rere = 
                      Restrictedresiduals . Restrictedresiduals;
ee = 
                      UnRestrictedresiduals . UnRestrictedresiduals;
 F = ((n - k)/r)*(rere - ee)/ee;
Wald = n*(rere - ee)/ee;
LR = n*Log[rere/ee]];
  LM = n*(rere - ee)/rere;
cF = 
                Re[ArcTan[
                      Sqrt[Quantile[FRatioDistribution[r, n - k], 1 - alpha]*
                          r/(n - k)]]*180/Pi];
cWald = 
                Re[ArcTan[
                      Sqrt[Quantile[ChiSquareDistribution[r], 1 - alpha]/
                          n]]*180/Pi];
cLR = 
                Re[ArcSec[(E^(Quantile[ChiSquareDistribution[r], 1 - alpha]/(2 n)))]*180/Pi];
cLM = 
                Re[ArcSin[
                      Sqrt[Quantile[ChiSquareDistribution[r], 1 - alpha]/
                          n]]*180/Pi];
qW = 
                Quantile[ChiSquareDistribution[r], 1 - alpha];
         qF = 
                Quantile[FRatioDistribution[r, n - k], 1 - alpha];
f = FRatioDistribution[r, n - k];
FPDF = PDF[f, y];
pvalueF = pvalueW = 1 - CDF[f, F];
g = ChiSquareDistribution[r];
ChiPDF = PDF[g, y];
pvalueW = 1 - CDF[g, Wald];
pvalueLR = 1 - CDF[g, LR];
pvalueLM = 1 - CDF[g, LM];
index = Table[i, {i, 1, Length[Transpose[data]]}];
beta = 
                Table[β, {i, 1, Length[Transpose[data]]}];
ind = 
                Table[x, {i, 1, Length[Transpose[data]]}];
betas = 
                Table[SequenceForm[beta[[i]], index[[i]]], {i, 1, 
                    Length[Transpose[data]]}];
inds = 
                Table[SequenceForm[ind[[i]], index[[i]]], {i, 1, 
                    Length[Transpose[data]]}];
all = 
                Table[SequenceForm[betas[[i]], inds[[i]]], {i, 1, 
                    Length[Transpose[data]]}];
n = Length[all];
resbeta = 
                Table[β, {i, 1, Length[list]}];
allres = 
                Table[SequenceForm[
                    resbeta[[i]], Sort[list][[i]]], {i, 1, 
                    Length[list]}];
str = "y=β0+";
For [i = 1, 
                i ≤ n - 1, i++, 
str = 
                  StringJoin[str, 
                    ToString[all[[i]]], "+"]
];
model = 
                StringJoin[str, "ϵ"];
n = Length[allres];
\(str = \*""\!\(H\_0\):""\);
For [i = 1, 
                i ≤ n, i++, 
str = 
                  StringJoin[str, 
                    ToString[allres[[i]]], "="]
];
null = 
                StringJoin[str, "0"];
\(Print[""]

              
              Print[StyleForm["********** F, Wald, LR and LM Tests **********", FontFamily -> CourierBold, FontSize -> 14]]

              
              Print[StyleForm["Model: ", FontFamily -> CourierBold, 
                  FontSize -> 12], model]\);
Print[
                StyleForm["Null hypothesis: ", 
                  FontFamily -> CourierBold, FontSize -> 12], 
                null];
\(Print[
                StyleForm["*** Geometric Process  ***", 
                  FontFamily -> CourierBold, FontSize -> 14]]

              
              Print[StyleForm["Calculated Angel : ", 
                  FontFamily -> CourierBold, FontSize -> 12], 
                SetPrecision[theta, 4]]

              
              Print[TableForm[{{StyleForm["Angular Statistics", 
                        FontFamily -> CourierBold, FontSize -> 12], 
                      StyleForm["Critical Angels", 
                        FontFamily -> CourierBold, 
                        FontSize -> 
                          12], 
StyleForm["p-values", 
                        FontFamily -> CourierBold, 
                        FontSize -> 12]
}, {"F", 
                      SetPrecision[cF, 4], 
                      SetPrecision[pvalueF, 5]}, {"Wald", 
                      SetPrecision[cWald, 4], 
                      SetPrecision[pvalueW, 5]}, {"LR", 
                      SetPrecision[cLR, 4], 
                      SetPrecision[pvalueLR, 5]}, {"LM", 
                      SetPrecision[cLM, 4], SetPrecision[pvalueLM, 5]}}]]\);
Print[
                StyleForm["*** Traditional Process  ***", 
                  FontFamily -> CourierBold, FontSize -> 14]];
Print[
                TableForm[{{StyleForm["Statistics", 
                        FontFamily -> CourierBold, 
                        FontSize -> 
                          12], 
StyleForm["Calculated \
Values", FontFamily -> CourierBold, FontSize -> 12], 
                      StyleForm["Critical Values", 
                        FontFamily -> CourierBold, 
                        FontSize -> 
                          12], 
StyleForm["p-values", 
                        FontFamily -> CourierBold, 
                        FontSize -> 12]
}, {"F", 
                      SetPrecision[F, 4], SetPrecision[qF, 4], 
                      SetPrecision[pvalueF, 5]}, {"Wald", 
                      SetPrecision[Wald, 4], SetPrecision[qW, 4], 
                      SetPrecision[pvalueW, 5]}, {"LR", 
                      SetPrecision[LR, 4], SetPrecision[qW, 4], 
                      SetPrecision[pvalueLR, 5]}, {"LM", 
                      SetPrecision[LM, 4], SetPrecision[qW, 4], 
                      SetPrecision[pvalueLM, 5]}}]];Print[
                StyleForm["*** Results of the Tests  ***", 
                  FontFamily -> CourierBold, FontSize -> 14]];
Which[theta > cF,\(Print[\*""(Subscript[H, 0]) rejected by F-Test""]\),theta < cF,\(Print[\*""(Subscript[H, 0]) failed to reject by F-Test""]\)];
Which[theta > cWald,\(Print[\*""(Subscript[H, 0]) rejected by Wald-Test""]\),theta < cWald,\(Print[\*""(Subscript[H, 0]) failed to reject by Wald-Test""]\)];
Which[theta > cLR,\(Print[\*""(Subscript[H, 0]) rejected by LR-Test""]\),theta < cLR,\(Print[\*""(Subscript[H, 0]) failed to reject by LR-Test""]\)];
Which[theta > cLM,\(Print[\*""(Subscript[H, 0]) rejected by LM-Test""]\),theta < cLM,\(Print[\*""(Subscript[H, 0]) failed to reject by LM-Test""]\)];
];

Please,read the instructions

To use the function  GeoTest[data, list, alpha],you must enter your data,list and alpha.The data must be a list of vectors . Each vector consists of the observed  
 \(values\ of\ the\ independent\ variables\ and\ the\ associated\ response . \ 
              Write\ your\ data\ as\ \(follows : 
data\) = {{x\_11, x\_12, \(\(...\)\(.\)\), y\_1}, {x\_21, x\_22, \(\(...\)\(.\)\), 
              y\_2}, \(\(...\)\(.\)\)}\) 
where (Subscript[x, ik]) is the value of the (i^th) case of    the (k^th) independent variable and (Subscript[y, i]) is the he value of the (i^th) case of the response variable . 
The list is a simple list .  It consists of the integers showing the indices of the parameters in the null hypothesis .  For instance,   
  let our model be y=(Subscript[β, 0])+(Subscript[β, 1]) (Subscript[x, 1])+(Subscript[β, 2]) (Subscript[x, 2])+(Subscript[β, 3]) (Subscript[x, 3])+(Subscript[β, 4]) (Subscript[x, 4])+ϵ and assume that we want to test the null hypothesis (Subscript[H, 0]):(Subscript[β, 3])=(Subscript[β, 4])=0.   Let the sample data be
y=(({{3}, {8}, {12}, {9}, {6}, {24}, {21}})),(Subscript[x, 1])=(({{4}, {8}, {13}, {7}, {8}, {19}, {16}})),  (Subscript[x, 2])=(({{13}, {9}, {6}, {13}, {2}, {1}, {2}})),(Subscript[x, 3])=(({{10}, {13}, {9}, {16}, {6}, {11}, {9}})),(Subscript[x, 4])=(({{15}, {16}, {19}, {12}, {18}, {24}, {30}}))  
In this case, 
      the list becomes  
         list = {3, 4}  
\(and\ the\ data\ \(becomes : \ 
data\) = \ {{4, 13, 10, 15, 3}, {8, 9, 13, 16, 8}, {13, 6, 
              9, 19, 12}, {7, 13, 16, 12, 9}, {8, 2, 6, 18, 6}, {19, 1, 11, 
              24, 24}, {16, 2, 9, 30, 21}};\)
The alpha is the level of significance .  You can choose  any level of significance . After entering your  data,list and alpha , type  GeoTest[data, list, alpha] and  evaluate the cell .  It  returns a table which shows the model,the null hypothesis and   some summary
\(statistics\ dealing\ with\ \ the\ F, Wald, \ 
      LR\ \ and\ LM\ tests . \ 
          It\ gives\ the\ results\ of\ \ both\ the\ traditional\ and\ the\ geometric\ \(\(approach\)\(.\)\)\)

Testing the program

mydata = {{4, 13, 10, 15, 3}, {8, 9, 13, 16, 8}, {13, 6, 9, 19, 
            12}, {7, 13, 16, 12, 9}, {8, 2, 6, 18, 6}, {19, 1, 11, 24, 
            24}, {16, 2, 9, 30, 21}};
mylist = {3, 4};
myalpha =  .05;
GeoTest[mydata, mylist, myalpha]