Message Boards Message Boards

2
|
9304 Views
|
7 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Fit a curve using dated data formatted for DateListPlot?

Show how to Fit a line or curve to this data (could not find it in Help):

data = {{{2006, 10, 1}, 10}, {{2006, 10, 15}, 12}, {{2006, 10, 30}, 15}, {{2006, 11, 20}, 20}};      
DateListPlot[data, Joined -> False]

Thank you, Jack Calman

POSTED BY: Jack Calman
7 Replies

There are different ways of doing this. One way would be to create a plot with a green line where you want green and make a plot with a red line where you want red. You can then combine the plots together with Show.

POSTED BY: Sean Clarke

Hi Sean, Thanks for your reply, but the intention is simular as this proposal and seems not to work with the fitPlot line:

Module[{dates, vals1, vals2, data1, data2, data3}, dates = Table[{2012, k, 1}, {k, 12}]; vals1 = {1, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2, 1}; vals2 = {4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3}; data1 = Transpose[{dates, vals1}]; data2 = Transpose[{dates, vals2}]; DateListPlot[{data1, data2}, ImageSize -> {150, 93}, Axes -> True, AxesOrigin -> {AbsoluteTime[data1[[1, 1]]], 0}, Filling -> {1 -> {{2}, {Red, Darker[Green]}}}, PlotRange -> Full] // Print;]

Can you help?

Thanks,....Jos

POSTED BY: Jos Klaps

Hi Sean, A further question to your input. Can we have also different collors above and bellow the data points with ListLinePlot and the fitPlot? Example:

Filling->{1->{{2},{Red,Darker[Green]}}}

which are not working.

Thanks,...Jos

POSTED BY: Jos Klaps

Hi Sean,

See above. Sorry, It shoulld be 'Joined->True' instead of ListLinePlot.

Thanks,....Jos

POSTED BY: Jos Klaps

Thanks Sam. And Sean - very nice indeed! Jack Calman

POSTED BY: Jack Calman

Doing this is a bit harder than you'd think at first. Dates are harder to work with than numbers. Normally, I'd suggest converting all dates to numbers using the AbsoluteTime function first, but here I'll show you how I would do this in Mathematica 10.

First, let's use the TimeSeries data structure. This marks your data as a time series and makes sure Mathematica knows to interpret your data as a series of values over time.

data = TimeSeries[{{{2006, 10, 1}, 10}, {{2006, 10, 15}, 12}, {{2006, 10, 30}, 15}, {{2006, 11, 20}, 20}}];
DateListPlot[data, Joined -> False]

enter image description here

Great. It recognizes your data as being dates. Let's use LinearModelFit to get a linear fit and then use normal to extract out the equation:

lm = Normal[LinearModelFit[data, x, x]]

We want to plot it, but ... the plotting function needs numbers.... It has to know what range of values to plot over and those have to be numbers. They can't be dates. The function that converts from dates to numbers appropriately is called AbsoluteTime:

fitPlot = Plot[lm, {x, AbsoluteTime[{2006, 10, 1}],  AbsoluteTime[{2006, 11, 20}]}, PlotStyle -> Red]

Now we can join our plots together:

Show[DateListPlot[data, Joined -> False], fitPlot ]

enter image description here

POSTED BY: Sean Clarke

Try first converting from date format to AbsoluteTime - which is not a list but a number. The rest should be easy.

{AbsoluteTime[#1], #2} & @@@ data

{{3368649600, 10}, {3369859200, 12}, {3371155200, 15}, {3372969600, 20}}

POSTED BY: Sam Carrettie
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