4
$\begingroup$

Context

I am interested in LinearModelFit, and would like to make diagnostic plots using Mathematica. I cannot find them in Mathematica document center. For example, the following R program makes diagnostic plots.

R program

x <- c(5,10,15,20,25,30,35)
y <- c(9.4,18.3,26.2,36.3,37.4,47.3,56.3)
dataset <- data.frame(x, y)
fit <-lm(y ~ x, data = dataset)
par(mfrow=c(2,2))
plot(fit)    

enter image description here

Question

Would you give me a suggestion on how to achieve this in mathematica ?

Attempt

data = {{5, 9.4}, {10, 18.3}, {15, 26.2}, {20, 36.3}, {25, 37.4}, {30, 47.3}, {35, 56.3}};
lm = LinearModelFit[data, x, x];
Show[ListPlot[data], Plot[lm[x], {x, 0, 40}], PlotRange -> All, Frame -> True]
ListPlot[lm["FitResiduals"], Filling -> Axis]
$\endgroup$
  • 1
    $\begingroup$ ?LinearModelFit $\endgroup$ – chris Dec 30 '14 at 15:07
  • $\begingroup$ @user25076, you may find this documentation useful $\endgroup$ – garej Mar 4 '16 at 20:19
7
$\begingroup$

Directly from the documentation of LinearModelFit

X = {5, 10, 15, 20, 25, 30, 35}
Y = {9.4, 18.3, 26.2, 36.3, 37.4, 47.3, 56.3}
fit = LinearModelFit[data = Transpose@{X, Y}, {1, x}, x]

(* FittedModel[1.49929 x+3.04286] *)

Show[ListPlot[data], Plot[fit[x], {x, 0, 35}], Frame -> True]

Mathematica graphics

ListPlot[fit["FitResiduals"], Filling -> Axis]

Mathematica graphics

Note that

fit["Properties"]

returns all the available statistics

Mathematica graphics

so that

{#, fit[#]} & /@ Select[fit["Properties"], # != "Properties" &]

Mathematica graphics

should be an endless source of joy and happiness as far as diagnosis is concerned :-)

$\endgroup$
  • 1
    $\begingroup$ @belisarius well you know: 'lies, damn lies and statistics' $\endgroup$ – chris Dec 30 '14 at 19:30
  • $\begingroup$ @chris, it more looks like a lot of pain... $\endgroup$ – garej Mar 4 '16 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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