Message Boards Message Boards

0
|
387 Views
|
3 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Sinusoidal regression on dataset

Posted 26 days ago

Tried NonlinearModelFit, TimeSeriesModelFit, FourierSeries

Have a dataset of daily temperatures for 10 years, need a sinusoidal expression representing it. I'm not sure if it's syntax errors or what, but none of the three above methods have worked. I've tried multiple times with each and none of them give me a function, best fit parameters, or even show up on a graph. Any tips would be appreciated.

POSTED BY: Jen Granato
3 Replies

It can be done as follows:

years = 18;
data = QuantityMagnitude@
   Normal[WeatherData["Philadelphia", 
      "MeanTemperature", {{2007, 1, 1}, {2006 + years, 12, 31}, 
       "Day"}]][[All, 2]];
model = a Sin[b t + c] + d;
fittedModel = 
  NonlinearModelFit[data, {model, Abs[b] <= 1/24}, {a, b, c, d}, t];
ListPlot[data, PlotStyle -> Blue, PlotRange -> All, 
 AxesLabel -> {"Time (days)", "Temperature (°C)"}, 
 PlotLabel -> "Temperature Time Series"]
Show[ListPlot[data, PlotStyle -> Blue], 
 Plot[fittedModel[t], {t, 1, 365*years}, PlotStyle -> Pink], 
 PlotLabel -> "Fitted Sinusoidal Line"]

The graph looks like this: enter image description here

Attachments:
Posted 24 days ago

It would be help (in fact, essential) that you produce the code you used for at least NonlinearModelFit.

Generally when fitting sine waves to data with NonlinearModelFit, there needs to be a reasonable guess at the parameter representing the frequency of the sine wave. The default starting value is 1 for all parameters if you don't give starting values.

If your model is a simple sine wave and the time values are in days, then using 2 Pi/365 will likely be a good starting value for the frequency parameter.

POSTED BY: Jim Baldwin
Posted 26 days ago

I can't quite see from here what it is that you have done.

Does this look anything like what you are trying to do?

data=Table[{t,Sin[t+1/8]+RandomReal[{-1/10,1/10}]},{t,0,6Pi,Pi/2}];
f[t_]=Normal[NonlinearModelFit[data,a*Sin[t+b]+c,{a,b,c},t]]
Show[Plot[f[t],{t,0,6Pi}],ListPlot[data]]

When you run that does that function seem to match those data points well?

Can you put your data into a similar form and try to replicate this with your data?

Perhaps the form of that example is a little too far from the form of your data.

So let's do this a different way

I go here: Denver mean high temperatures and I scrape these month, temperature pairs

JANUARY,47 FEBRUARY,49 MARCH,56 APRIL,62

MAY,72 JUNE,81 JULY,88 AUGUST,86

SEPTEMBER,78 OCTOBER,66 NOVEMBER,54 DECEMBER,46

I scale the dates to match 2 Pi and I fit a sine

data={{0/12*2*Pi,47}, {1/12*2*Pi,49}, {2/12*2*Pi,56}, {3/12*2*Pi,62},
{4/12*2*Pi,72}, {5/12*2*Pi,81}, {6/12*2*Pi,88}, {7/12*2*Pi,86},
{8/12*2*Pi,78}, {9/12*2*Pi,66}, {10/12*2*Pi,54}, {11/12*2*Pi,46}};
f[t_]=Normal[NonlinearModelFit[data,a*Sin[t+b]+c,{a,b,c},t]]
Show[Plot[f[t],{t,0,2Pi}],ListPlot[data]]

And I get

65.4167-20.6073 Sin[7.92249-t]

followed by a plot that doesn't look like too awful a fit. The 65.4 represents the mean temperature, the 20.6 scales the Sin range to the temperature range and the 7.9 translates the temperature peaks to the Sin peaks. And it does all this in a fraction of a second.

POSTED BY: Bill Nelson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract