Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2007

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

Search the Archive

Re: Problem with lists as matrices for OLS and statistical inference

  • To: mathgroup at smc.vnet.net
  • Subject: [mg81660] Re: [mg81638] Problem with lists as matrices for OLS and statistical inference
  • From: DrMajorBob <drmajorbob at bigfoot.com>
  • Date: Mon, 1 Oct 2007 04:40:39 -0400 (EDT)
  • References: <19756584.1191142844936.JavaMail.root@m35>
  • Reply-to: drmajorbob at bigfoot.com

First of all, underscores can't be used in symbol names; rsquare_one is a  
named pattern object with head "one" and name "rsquare".

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

Secondly, your xtwo and ytwo have incompatible shapes for the purpose.

Dimensions /@ {x, y, xtwo, ytwo}

{{30, 10}, {30}, {9, 3}, {9, 1}}

y is a vector, but ytwo is a column matrix.

You can flatten ytwo, and then

ytwo = Flatten@ytwo

{3.21116, 3.22359, 3.22625, 3.18356, 3.15343, 3.18843, 3.17617, \
3.19074, 3.3005}

rsquare[xtwo, ytwo]

0.99989

and so forth.

Bobby

On Sun, 30 Sep 2007 02:58:22 -0500, Mauricio Esteban Cuak  
<cuak2000 at gmail.com> wrote:

> 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:
>
> estimatedvariance[x_,
>  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:
>
> estimated_variance_two[x_,
>  y_] := (Inverse[Transpose[x].x])*(Flatten[
>     Transpose[(y - x.firstOLS[x, y])].(y - x.firstOLS[x,  
> y])])/(Length[y] -
>     Length[firstOLS[x, y]])
>
> rsquare_two[x_,
>  y_] := (Flatten[
>    Transpose[(xdmda.firstOLS[x, y])].(x.firstOLS[x,
>        y])])/Flatten[Transpose[y].y]
>
> 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
> work...
> I think I must be missing something about the way Mathematica works with  
> lists.
> Hope you can help me. Thanks!
>
> Regards,
>
> cd
>
>



-- 

DrMajorBob at bigfoot.com


  • Prev by Date: Re: create a list with x,y,z coordinates
  • Next by Date: Re: Plot3D with NDSolve
  • Previous by thread: Re: create a list with x,y,z coordinates
  • Next by thread: Re: Problem with lists as matrices for OLS and statistical inference