Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

In the help for LinearModelFit, the regression line can be added to the scatter plot like this:

Show[ListPlot[data], Plot[lm[x], {x, 0, 5}]]

Is it possible to fill from the points to the regression line? This can illustrate the distance whose sum of squares is being minimized. I tried variations on this:

Filling -> Table[ lm[data[[i]][[2]]],  {i, Length[data]}]]

Here the Table will be a list of predicted values for y at each observation of x, but this results in the error "not a valid filling specification."

share|improve this question
add comment

5 Answers 5

up vote 17 down vote accepted

Maybe something like this:

data = Table[{x, .5 + .5 x + RandomReal[{-.5, .5}]}, {x, 
    RandomReal[7, 100]}];
lm = LinearModelFit[data, x, x];
ListPlot[{data, Table[{x,lm[x]}, {x, 0, 7, .1}]}, Joined -> {False, True}, 
    Filling -> 1 -> {2}]

enter image description here

share|improve this answer
add comment

Another possibility, using ErrorBar :

Needs["ErrorBarPlots`"]

data = Table[{x, .5 + .5 x + RandomReal[{-.5, .5}]}, {x, 
RandomReal[{-5, 5}, 20]}];lm = LinearModelFit[data, x, x];

dataAndError = {#, ErrorBar[(-#[[2]] + lm[#[[1]]]) {Boole[#[[2]] >= lm[# [[1]]]] , Boole[#[[2]] < lm[#[[1]]]] }]} & /@ data;

Show[Plot[lm[x], {x, -5, 5}, PlotStyle -> {Red}], ErrorListPlot[dataAndError]]

plot

share|improve this answer
add comment

Another way, using the properties of LinearModelFit, and a convoluted way for constructing lines (note that all the plot is done with only one ListLinePlot)

verba = Table[{x, .5 + .5 x + RandomReal[{-.5, .5}]}, {x, RandomReal[7, 100]}];
lm = LinearModelFit[verba, x, x];
res = Transpose@{verba[[All, 1]], lm["PredictedResponse"]};
ListLinePlot[Flatten[Transpose@{res, verba, res}, 1], Mesh -> All, Frame -> True]

Mathematica graphics

share|improve this answer
    
(+1) could also use lm["PredictedResponse"] instead of #[[2]] - lm["FitResiduals"] when defining res. –  kguler Sep 30 '12 at 22:13
    
@kguler thanks! Edited. –  belisarius Sep 30 '12 at 22:23
add comment

Yet another possibility, using Arrow[]+Arrowheads[]:

(* for reproducibility *)
BlockRandom[SeedRandom[42, Method -> "Legacy"];
            data = Sort[Table[{x, .5 + .5 x + RandomVariate[NormalDistribution[]]/5},
                              {x, RandomReal[7, 100]}]]];

lm = LinearModelFit[data, x, x];

Show[Graphics[{ColorData[1, 1], 
               Arrowheads[{{Automatic, Automatic, Graphics[Point[{0, 0}]]}}], 
               MapThread[Arrow[{##}] &,
                         {Transpose[{data[[All, 1]], lm["PredictedResponse"]}], data}]}],
     Plot[lm[x], {x, 0, 7}], Frame -> True]

regression line and data points

share|improve this answer
add comment

One may simply draw them as follows. This is the list of data:

lst = Table[{i, 0.5 + 0.5*i^(1/2) + RandomReal[{-.5, .5}]}, {i, 1, 10}] 

This is the fitting:

ft = FindFit[lst, a*x^(1/2) + b, {{a, 0.4}, b}, x]

And this draws the fitting function together with the list and draws lines from the points specified by the list to the curve:

f[x_] := a*x^(1/2) + b /. ft
lst1 = Transpose[lst][[1]];

Show[{ListPlot[lst, PlotStyle -> Red], Plot[f[x] /. ft, {x, 0, 10}],


Graphics[   
     {Pink, Line[Transpose[{lst, Transpose[{lst1, Map[f, lst1]}]
                                              }]]} ]}]

That is the outcome: enter image description here

share|improve this answer
2  
lst1 = Transpose[lst][[1]]; can also be done this way: lst1 = lst[[All, 1]]; –  J. M. Oct 1 '12 at 12:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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