1

Why do I get this error? I just clicked shift+enter in the y and it appeared.

https://i.stack.imgur.com/IAKs8.png

This is the data

y = {{4.9176, 5.0208, 4.5429, 4.5573, 5.0597, 3.8910, 5.8980, 5.6039, 
5.8282, 5.3003, 6.2712, 5.9592, 5.0500, 8.2464, 6.6969, 7.7841, 
9.0384, 5.9894, 7.5422, 8.7951, 6.0831, 8.3607, 8.1400, 9.1416}};

x = {25.9, 29.5, 27.9, 25.9, 29.9, 29.9, 30.9, 28.9, 35.9, 31.5, 31, 
30.9, 30, 36.9, 41.9, 40.5, 43.9, 37.5, 37.9, 44.5, 37.9, 38.9, 
36.9, 45.8};
Id = IdentityMatrix[24];
x = MatrixForm[Thread[{1, x}]];
M = x*Inverse[Transpose[x]*x]*Transpose[x];


SSE = Transpose[y]*(Id - M)*y

SST = Transpose[y - Mean[y]]*(y - Mean[y])

I just wanted the transpose of y (first, in the end I want to know SSE).

How come unequal length error appeared?

Help me please

(I apologize for many questions)

m_goldberg
  • 107,779
  • 16
  • 103
  • 257

1 Answers1

4

You need Dot:

ClearAll[x, y]
y = {{4.9176, 5.0208, 4.5429, 4.5573, 5.0597, 3.8910, 5.8980, 5.6039, 
    5.8282, 5.3003, 6.2712, 5.9592, 5.0500, 8.2464, 6.6969, 7.7841, 
    9.0384, 5.9894, 7.5422, 8.7951, 6.0831, 8.3607, 8.1400, 9.1416}};
x = {25.9, 29.5, 27.9, 25.9, 29.9, 29.9, 30.9, 28.9, 35.9, 31.5, 31, 
   30.9, 30, 36.9, 41.9, 40.5, 43.9, 37.5, 37.9, 44.5, 37.9, 38.9, 
   36.9, 45.8};

x = Thread[{1, x}];
y = y[[1]];
Id = IdentityMatrix[24];
M = x. Inverse[Transpose[x] . x] . Transpose[x];
SSE = y . (Id - M) . y

13.392961097651924

SST = (y - Mean[y]).(y - Mean[y])

57.56312745333333`

kglr
  • 394,356
  • 18
  • 477
  • 896
  • Thank you kglr, I have a question why do you add y = y[[1]]; ? – g.a.l.l.e.t.a Oct 31 '18 at 05:52
  • Oh and you also removed the IdentityMatrix line, is y = y[[1]]; somehow equivalent to? – g.a.l.l.e.t.a Oct 31 '18 at 05:54
  • @user459663, forgot to paste the line with Id =.... Re y[[1]], we need a T by 1 vector for the dependent variable. – kglr Oct 31 '18 at 05:57
  • I don't understand. Is Transpose[y - Mean[y]].(y - Mean[y]) equivalent to (y - Mean[y]).(y - Mean[y]) + y = y[[1]]; ? – g.a.l.l.e.t.a Oct 31 '18 at 06:03
  • @user459663, they are not . You need the latter to get the sse in mathematica. Btw, mathematica does not distinguish between column and row vectors. – kglr Oct 31 '18 at 06:07
  • ok so that's why you removed Transpose from Transpose[y - Mean[y]].(y - Mean[y]) right? – g.a.l.l.e.t.a Oct 31 '18 at 06:19
  • Though I still don't get the line $y=y[[1]];$ – g.a.l.l.e.t.a Oct 31 '18 at 06:20
  • @user459663, it is to remove the extra braces {{...}} in your original definition of y = {{ ...}}. – kglr Oct 31 '18 at 06:29
  • oh ok, I tested your code with other different data for x's and y's and just when I clicked shift+enter in y=y[[1]] this error pop up "Nonrectangular tensor encountered." – g.a.l.l.e.t.a Oct 31 '18 at 06:33
  • @user459663, (1) you need y=y[[1]] only when y is defined as y={{...}} (For example, in the picture in your other question this is not the case so you don't need to use y= y[[1]]). (2) you need to use ClearAll[x,y] before the lines that define x and y . – kglr Oct 31 '18 at 06:38
  • mmh that's so strange I did it all and now gives like million of vectors – g.a.l.l.e.t.a Oct 31 '18 at 06:48
  • wait..I got it, the Identity matrix had different size.. – g.a.l.l.e.t.a Oct 31 '18 at 06:50