1
$\begingroup$

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)

$\endgroup$
  • $\begingroup$ Please post actual code rather than pictures of code. You'll likely get more help that way. $\endgroup$ – JimB Oct 31 '18 at 5:32
  • $\begingroup$ @JimB Oh ok sorry, how do I do that? $\endgroup$ – g.a.l.l.e.t.a Oct 31 '18 at 5:33
  • 1
    $\begingroup$ remove MatrixForm from the rhs of definition: use x = Thread[{1,x}]; $\endgroup$ – kglr Oct 31 '18 at 5:34
  • $\begingroup$ @JimB I did it. $\endgroup$ – g.a.l.l.e.t.a Oct 31 '18 at 5:37
  • 1
    $\begingroup$ You'll need to use . rather than * for matrix multiplication. $\endgroup$ – JimB Oct 31 '18 at 5:42
4
$\begingroup$

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`

$\endgroup$
  • $\begingroup$ Thank you kglr, I have a question why do you add y = y[[1]]; ? $\endgroup$ – g.a.l.l.e.t.a Oct 31 '18 at 5:52
  • $\begingroup$ Oh and you also removed the IdentityMatrix line, is y = y[[1]]; somehow equivalent to? $\endgroup$ – g.a.l.l.e.t.a Oct 31 '18 at 5:54
  • $\begingroup$ @user459663, forgot to paste the line with Id =.... Re y[[1]], we need a T by 1 vector for the dependent variable. $\endgroup$ – kglr Oct 31 '18 at 5:57
  • $\begingroup$ I don't understand. Is Transpose[y - Mean[y]].(y - Mean[y]) equivalent to (y - Mean[y]).(y - Mean[y]) + y = y[[1]]; ? $\endgroup$ – g.a.l.l.e.t.a Oct 31 '18 at 6:03
  • $\begingroup$ @user459663, they are not . You need the latter to get the sse in mathematica. Btw, mathematica does not distinguish between column and row vectors. $\endgroup$ – kglr Oct 31 '18 at 6:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.