MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Problem with lists as matrices for OLS and statistical inference

  • To: mathgroup at
  • Subject: [mg81638] Problem with lists as matrices for OLS and statistical inference
  • From: "Mauricio Esteban Cuak" <cuak2000 at>
  • Date: Sun, 30 Sep 2007 03:58:22 -0400 (EDT)

Hello everyone.
I tried making some functions for OLS, R square, etc. (yes, I'm aware
these functions already exist). As a newcomer to Mathematica, I'm a
bit confused with using lists instead of matrices.

Here was my first attempt:

Clear["`*"]; Needs["HypothesisTesting`"];
firstOLS[x_, y_] := (Inverse[Transpose[x].x]).Transpose[x].y

And I did also one for estimated variance for the parameters and one
for R square:

 y_] :=(Inverse[Transpose[x].x])*((y - x.firstOLS[x, y]).(y -
x.firstOLS[x, y]))/(Length[y] -
    Length[firstOLS[x, y]])

rsquare_one[x_, y_] := (x.firstOLS[x, y]).(x.firstOLS[x, y])/(y.y)

I tried both with this matrices and it worked fine:

x = Table[RandomReal[10,
10], {30}];y = RandomReal[21, 30];

So far so good. When I tried using data imported from excel, all hell
broke loose. Here are the matrices I used for x and y :

xtwo = {{1.`, 4.351993`, 1.888761`}, {1.`, 4.321984`,
  1.907567`}, {1.`, 4.290852`, 1.88777`}, {1.`, 4.296212`,
  1.99383`}, {1.`, 4.258126`, 1.870771`}, {1.`, 4.343519`,
  1.967788`}, {1.`, 4.337479`, 1.882922`}, {1.`, 4.332442`,
  1.94114`}, {1.`, 4.36106`, 1.942251`}}

ytwo = {{3.211156`}, {3.223586`}, {3.226253`}, {3.183556`},
{3.15343`}, {3.18843`}, {3.17617`}, {3.190737`}, {3.300504`}}

Now my functions don't work even though they look the same to me! (the
output gives an error saying the matrices can't be divided) I tried
correcting them in this way:

 y_] := (Inverse[Transpose[x].x])*(Flatten[
    Transpose[(y - x.firstOLS[x, y])].(y - x.firstOLS[x, y])])/(Length[y] -
    Length[firstOLS[x, y]])

 y_] := (Flatten[
   Transpose[(xdmda.firstOLS[x, y])].(x.firstOLS[x,

Now they seem to work fine with the last x and y matrices, but when I
use this functions with the original "test" matrices, they don't
I think I must be missing something about the way Mathematica works with lists.
Hope you can help me. Thanks!



  • Prev by Date: Re: Flat colour in RegionPlot; millions of little triangles
  • Next by Date: Re: Exporting the Notebook expression corresponding to a DocumentNotebook
  • Previous by thread: Palette Size