Re: Problem with lists as matrices for OLS and statistical inference
- To: mathgroup at smc.vnet.net
- Subject: [mg81676] Re: Problem with lists as matrices for OLS and statistical inference
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Mon, 1 Oct 2007 04:48:54 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <fdnl6l$kg8$1@smc.vnet.net>
Mauricio Esteban Cuak wrote: > 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) --------^^ *NEVER* use an underscore, or any other special characters, in symbol names (functions, variables, constants, parameters, etc.). In Mathematica, names /start/ with a letter and may be followed by one or more letters or digits but no white spaces, dashes, underscores, ... > 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! <snip> And they are not. Indeed, ytow is not a vector (i.e. one dimensional list of atomic expressions) but list of lists (see the parentheses around each individual components). You can check this with the built-in function *Dimension* (see below for an example). You can make it a vector with *Flatten* and your function will work as expected. Here is a corrected version of your code that also checks its arguments. In[1]:= Clear["`*"]; Needs["HypothesisTesting`"]; firstOLS[x_, y_] := (Inverse[Transpose[x].x]).Transpose[x].y estimatedVariance[x_?MatrixQ, y_?VectorQ] /; Length[x] == Length[y] := (Inverse[ Transpose[ x].x])*((y - x.firstOLS[x, y]).(y - x.firstOLS[x, y]))/(Length[ y] - Length[firstOLS[x, y]]) rsquareOne[x_?MatrixQ, y_?VectorQ] /; Length[x] == Length[y] := (x.firstOLS[x, y]).(x.firstOLS[x, y])/(y.y) In[6]:= x = Table[RandomReal[10, 10], {30}]; y = RandomReal[21, 30]; In[7]:= Dimensions /@ {x, y} Out[7]= {{30, 10}, {30}} In[8]:= estimatedVariance[x, y] Out[8]= {{0.10593, -0.0259118, 0.030062, 0.0136462, -0.0118098, -0.0405242, -0.00778524, -0.0395141, \ -0.0153612, -0.00756684}, {-0.0259118, 0.147215, -0.0509466, -0.0486197, -0.0283033, 0.00906289, -0.0618385, 0.0660118, 0.00813397, -0.0120709}, {0.030062, -0.0509466, 0.155962, 0.00406087, -0.0218077, -0.0190581, -0.00382065, -0.0549665, \ -0.0318577, 0.00124514}, {0.0136462, -0.0486197, 0.00406087, 0.11328, -0.0383275, -0.00175796, 0.0063815, -0.0404709, -0.0127213, 0.00564989}, {-0.0118098, -0.0283033, -0.0218077, -0.0383275, 0.220974, -0.00691283, 0.00181065, -0.0835891, -0.041079, 0.0420895}, {-0.0405242, 0.00906289, -0.0190581, -0.00175796, -0.00691283, 0.107841, -0.0314461, -0.0042561, -0.00256467, -0.00196386}, \ {-0.00778524, -0.0618385, -0.00382065, 0.0063815, 0.00181065, -0.0314461, 0.15251, -0.0163103, 0.00566813, -0.0492389}, {-0.0395141, 0.0660118, -0.0549665, -0.0404709, -0.0835891, -0.0042561, \ -0.0163103, 0.179636, 0.0368754, -0.0608885}, {-0.0153612, 0.00813397, -0.0318577, -0.0127213, -0.041079, -0.00256467, 0.00566813, 0.0368754, 0.123511, -0.0704201}, {-0.00756684, -0.0120709, 0.00124514, 0.00564989, 0.0420895, -0.00196386, -0.0492389, -0.0608885, -0.0704201, 0.157391}} In[9]:= rsquareOne[x, y] Out[9]= 0.872819 In[10]:= 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`}}; In[12]:= Dimensions /@ {xtwo, ytwo} Out[12]= {{9, 3}, {9, 1}} In[13]:= estimatedVariance[xtwo, ytwo] Out[13]= estimatedVariance[{{1., 4.35199, 1.88876}, {1., 4.32198, 1.90757}, {1., 4.29085, 1.88777}, {1., 4.29621, 1.99383}, {1., 4.25813, 1.87077}, {1., 4.34352, 1.96779}, {1., 4.33748, 1.88292}, {1., 4.33244, 1.94114}, {1., 4.36106, 1.94225}}, {{3.21116}, {3.22359}, {3.22625}, {3.18356}, {3.15343}, \ {3.18843}, {3.17617}, {3.19074}, {3.3005}}] In[14]:= rsquareOne[xtwo, ytwo] Out[14]= rsquareOne[{{1., 4.35199, 1.88876}, {1., 4.32198, 1.90757}, {1., 4.29085, 1.88777}, {1., 4.29621, 1.99383}, {1., 4.25813, 1.87077}, {1., 4.34352, 1.96779}, {1., 4.33748, 1.88292}, {1., 4.33244, 1.94114}, {1., 4.36106, 1.94225}}, {{3.21116}, {3.22359}, {3.22625}, {3.18356}, {3.15343}, \ {3.18843}, {3.17617}, {3.19074}, {3.3005}}] In[15]:= ytwo = Flatten@ytwo Out[15]= {3.21116, 3.22359, 3.22625, 3.18356, 3.15343, 3.18843, \ 3.17617, 3.19074, 3.3005} In[16]:= Dimensions@ytwo Out[16]= {9} In[17]:= estimatedVariance[xtwo, ytwo] Out[17]= {{3.56526, -0.79527, -0.0668136}, {-0.79527, 0.20147, -0.0392576}, {-0.0668136, -0.0392576, 0.123139}} In[18]:= rsquareOne[xtwo, ytwo] Out[18]= 0.99989 HTH, -- Jean-Marc