I am trying to perform a linear regression on some weather data, but I can't seem to figure out how to perform the operation. What I have right now is this:

temperature = WeatherData["Chicago", "Temperature", {{1990},{2010},"Month"}]
model = LinearModelFit[temperature,x,x]

Any ideas on what I am doing wrong?

  • $\begingroup$ I think you should tell us for which version you need this to work. The problem is that the format of the return value of WeatherData has changed from version 9 to version 10 and both are not valid input for LinearModelFit. Belisarius answer shows how to solve the problem for version 9, but you'd need a different approach for version 10. $\endgroup$ Nov 22, 2014 at 19:15

1 Answer 1

temp = WeatherData["Chicago", "Temperature", {{1990}, {2010}, "Month"}];
data = temp /. {{y_, m_, d_}, x_} :> {m, x};
lm = LinearModelFit[data, {1, x, x^2, x^3, x^4}, x]
Show[ListPlot[data, PlotRange -> All, Frame -> True], 
         Plot[lm[x], {x, 0, 13}]]

Mathematica graphics

But I would personally go for something like this:

data1 = Join[data, {12, 0} + # & /@ data];
model = a Sin[k x + p] + b;
ff = FindFit[data1, model, {a, k, p, b}, x];
modelf = Function[{t}, Evaluate[model /. ff]]
Show[ListPlot@data, Plot[modelf[t], {x, 0, 12}]]

Mathematica graphics


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.