Simple Best-Fit Line

Manipulate[
clr[test_]:=If[MemberQ[includepts,test],RGBColor[.9,.42,.17],Gray];
datalist={ptA,ptB,ptC,ptD,ptE,ptF};
points=Graphics[{PointSize[0.03],{RGBColor[.9,.42,.17],Point[ptA]},{RGBColor[.9,.42,.17],Point[ptB]},{clr[cc],Point[ptC]},{clr[dd],Point[ptD]},{clr[ee],Point[ptE]},{clr[ff],Point[ptF]}}];
labels=Graphics[Table[Text[Style[CharacterRange["A","F"][[i]],Bold,"Label",14],datalist[[i]]+{0.45,0}],{i,1,Length[datalist]}]];
adjustedlist=DeleteCases[{ptA,ptB,If[MemberQ[includepts,cc],ptC,{}],If[MemberQ[includepts,dd],ptD,{}],If[MemberQ[includepts,ee],ptE,{}],If[MemberQ[includepts,ff],ptF,{}]},{}];
fitline=Fit[adjustedlist,{1,x},x];
lineplot=Plot[fitline,{x,-5,5}];

Deploy[Show[lineplot,points,labels,PlotRange->5,AspectRatio->Automatic,ImageSize->300,BaseStyle->{12},GridLines->{Table[{n ,Lighter[Gray,0.7]},{n,-7,7,1}],Table[{n,Lighter[Gray,0.7]},{n,-7,7,1}]},AxesOrigin->{0,0}]],

{{ptA,{1,2}},{-5,-5},{5,5},Locator,Appearance->Style["●",Opacity[0],1]},{{ptB,{-1,-3}},{-5,-5},{5,5},Locator},
{{ptC,{4,5}},{-5,-5},{5,5},Locator},{{ptD,{1,1}},{-5,-5},{5,5},Locator},
{{ptE,{-2,-4}},{-5,-5},{5,5},Locator},{{ptF,{0,-1}},{-5,-5},{5,5},Locator},
"include",
{{includepts,{cc,dd,ee,ff},""},{cc->" C",dd->" D",ee->" E",ff->" F"},ControlType->CheckboxBar,Appearance->"Vertical"},
ControlPlacement->Right,TrackedSymbols:>{ptA,ptB,ptC,ptD,ptE,ptF,includepts}
]

CAPTION

Move the points and see how the line fits those points. You can exclude points from the best-fit calculation by unchecking the appropriate boxes.

THIS NOTEBOOK IS THE SOURCE CODE FROM

"Simple Best-Fit Line" from the Wolfram Demonstrations Project
 http://demonstrations.wolfram.com/SimpleBestFitLine/

Contributed by: Abby Brown

A full-function Wolfram Mathematica system (Version 6 or higher) is required to edit this notebook.
GET WOLFRAM MATHEMATICA »

© Wolfram Demonstrations Project & Contributors   |   Terms of Use   |   Make a new version of this Demonstration »