# Evaluating a formula that uses summations and products of vectors

Could someone tell me how to find beta1?

I have the following data:

I don't know how to write beta1, and I don't find anything related on google

### Edit

I tried

beta1 =
(n Total[x.y] - (Total[x])(Total[y]))/(n Total[x^2] - (Total[x])^2)


but I got the unexpected result

 -6.41769 + 0.00120621 Total[5511.93]


Why?

• beta1 = (n*Total[x*y]-Total[x]*Total[y])/(n*Total[x^2]-Total[x]^2) Check that carefully with several different shorter lists to try to confirm the result is correct. – Bill Oct 31 '18 at 3:04
• @Bill it doesn't work, I tried with n=1,x={1,2},y={1,1} and it gives .23 when the real result it's 3/4 – g.a.l.l.e.t.a Oct 31 '18 at 3:24
• @Bill oh wait I didn't refresh the x's and y's...now it works fine :) – g.a.l.l.e.t.a Oct 31 '18 at 3:37
• I checked individually and it works fine, thank you @Bill – g.a.l.l.e.t.a Oct 31 '18 at 4:01
• Please post the code text instead of screenshot of it. – xzczd Oct 31 '18 at 6:21

ClearAll[betahat]
betahat[x_, y_] := Module[{d = Thread[{1, x}]}, Inverse[Transpose[d].d].Transpose[d].y]


Example:

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};

betahat[x, y]


{-1.58437, 0.230821}

Compare to the result from LinearModelFit:

LinearModelFit[Thread[{x, y}], t, t]["BestFitParameters"]


{-1.58437, 0.230821}

• betahat[x,y] gives as result $\{\hat\beta_0,\hat\beta_1\}$? – g.a.l.l.e.t.a Nov 1 '18 at 0:39
• @user459663, yes. – kglr Nov 1 '18 at 0:42
• ah I see, thank you for your help kglr – g.a.l.l.e.t.a Nov 1 '18 at 0:52
• I tested the first line and it says Module needs another parameter, why? – g.a.l.l.e.t.a Nov 1 '18 at 1:14
• @user459663, a cut/paste error (a comma was missing). Fixed now. – kglr Nov 1 '18 at 2:42
β1[x_, y_] := Module[{n = Length[x]},
(n x.y - Total[x] Total[y])/(n x.x - Total[x]^2)
]