1
$\begingroup$

Could someone tell me how to find beta1?

I have the following data:

enter image description here

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

Please help me

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?

$\endgroup$
9
  • $\begingroup$ 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. $\endgroup$
    – Bill
    Oct 31, 2018 at 3:04
  • $\begingroup$ @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 $\endgroup$ Oct 31, 2018 at 3:24
  • $\begingroup$ @Bill oh wait I didn't refresh the x's and y's...now it works fine :) $\endgroup$ Oct 31, 2018 at 3:37
  • $\begingroup$ I checked individually and it works fine, thank you @Bill $\endgroup$ Oct 31, 2018 at 4:01
  • 1
    $\begingroup$ Please post the code text instead of screenshot of it. $\endgroup$
    – xzczd
    Oct 31, 2018 at 6:21

2 Answers 2

3
$\begingroup$
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}

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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