Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Considering I have some data, and I want to fit the data to a linear fit and find the average error from the linear fit, how can I do that using Mathematica?

I have

temp = WeatherData[location, "MeanTemperature", {start, end, "Day"}];
Show[ListPlot@Partition[Reverse@temp["Values"], 2, 1], Plot[line, {x, 0, 40}, 
  PlotStyle -> Red]]

Which generates

enter image description here

How can I find the average error of this linear fit?

share|improve this question
What about error? – Mike Aug 15 '14 at 9:56
I don't believe we do need a room, not that I don't want to be in a room with you ;o) But just provide the data or the code to create them. – Öskå Aug 15 '14 at 10:11
If, for example, dat = Table[{x, Sin[x] + RandomReal[{-.2, .2}]}, {x, 0, 10, .1}]; (because the code you gave needs a location and other parameters and I don't know, nor want to learn, how to set that) then eg mdl = NonlinearModelFit[dat, a*Sin[b*x] + c, {a, b, c}, x] finds a model and you can find for example the variance like so Variance[dat[[All, 2]] - mdl /@ dat[[All, 1]]]. – acl Aug 15 '14 at 12:06
@Mike, fit["EstimatedVariance"] gives the variance. The documentation refers to many other properties and measures that are available, if you're after something else. – Michael E2 Aug 15 '14 at 12:27
I believe those displacements are called residuals and are given by fit["FitResiduals"] or by the formula acl used inside Variance in his first comment. – Michael E2 Aug 15 '14 at 14:19
up vote 6 down vote accepted

The answer is 0, if the question is what is the value of Mean[fit["FitResiduals"]] and fit is a linear, least-squares fitted model.

data = WeatherData["London", "Temperature", {{2004, 1, 1}, {2013, 12, 31}, "Day"}];
normaldata = Partition[Reverse[data["Values"][[All, 1]]], 2, 1];
fit = LinearModelFit[SetPrecision[normaldata, Infinity], x, x, WorkingPrecision -> 20]
lfit["FitResiduals"] // Mean

Other possibilities:

Mean of the absolute residuals.

lfit["FitResiduals"] // Abs // Mean
(* 1.455345158021296804 *)

Standard error (root-mean-square of the residuals).

lfit["EstimatedVariance"] // Sqrt
(* 1.862702601613853043 *)
share|improve this answer

Your Answer


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.