Message Boards Message Boards

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

How to plot model with date and numbers array

Posted 11 years ago
Hello
I want to approximate a list with date and numbers with function x(t) = a + b*(t)^0.3
I wrote the following code.
 data22 = {{{2007, 1, 16}, 12582.59`}, {{2007, 1, 17},
 
     12577.15`}, {{2007, 1, 18}, 12567.93`}, {{2007, 1, 19},
     12565.53`}, {{2007, 1, 22}, 12477.17`}, {{2007, 1, 23},
     12533.8`}, {{2007, 1, 24}, 12621.77`}, {{2007, 1, 25},
     12502.56`}, {{2007, 1, 26}, 12487.02`}, {{2007, 1, 29},
     12490.78`}, {{2007, 1, 30}, 12523.31`}, {{2007, 1, 31},
     12621.69`}, {{2007, 2, 1}, 12673.68`}, {{2007, 2, 2},
     12653.49`}, {{2007, 2, 5}, 12661.74`}, {{2007, 2, 6},
    12666.31`}, {{2007, 2, 7}, 12666.87`}, {{2007, 2, 8},
    12637.63`}, {{2007, 2, 9}, 12580.83`}, {{2007, 2, 12},
    12552.55`}, {{2007, 2, 13}, 12654.85`}, {{2007, 2, 14},
    12741.86`}, {{2007, 2, 15}, 12765.01`}, {{2007, 2, 16},
    12767.57`}};

(*Create_Array_With_sequential_number*)
Tech1 = Table[i, {i, Dimensions[data22][[1]]}];
data22 = MapThread[Insert, {data22, Tech1, Table[3, {Length[Tech1]}]}];
data33 = data22[[All, {3, 2}]];

nlm = NonlinearModelFit[data33, a + b*(t)^0.3, {a, b}, t];
p1 = Plot[nlm[t], {t, 1, Dimensions[data33][[1]]},
   PlotStyle -> {Black}];
Show[ListPlot[data33], p1]

That's works well, but it plots a numbers on the X-axis ( 1,2,3,4..... ).
How can I plot the same graphic, but with dates on the X-axis ( {2007, 1, 16},  {2007, 1, 17}, {2007, 1, 18},.... ) ?

I found function DateListPlot, but I don't know, how to use it in this case.
Many thanks !
POSTED BY: Ivan Ivan
3 Replies
Posted 11 years ago
 In[1]:= data22 = {
 {{2007, 1, 16}, 12582.59`}, {{2007, 1, 17}, 12577.15`}, {{2007, 1, 18}, 12567.93`}, {{2007, 1, 19}, 12565.53`},
  {{2007, 1, 22}, 12477.17`}, {{2007, 1, 23}, 12533.8`}, {{2007, 1, 24}, 12621.77`}, {{2007, 1, 25}, 12502.56`},
  {{2007, 1, 26}, 12487.02`}, {{2007, 1, 29}, 12490.78`}, {{2007, 1, 30}, 12523.31`}, {{2007, 1, 31}, 12621.69`},
  {{2007, 2, 1}, 12673.68`}, {{2007, 2, 2}, 12653.49`}, {{2007, 2, 5}, 12661.74`}, {{2007, 2, 6}, 12666.31`},
  {{2007, 2, 7}, 12666.87`}, {{2007, 2, 8}, 12637.63`}, {{2007, 2, 9}, 12580.83`}, {{2007, 2, 12}, 12552.55`},
  {{2007, 2, 13}, 12654.85`}, {{2007, 2, 14}, 12741.86`}, {{2007, 2, 15}, 12765.01`}, {{2007, 2, 16}, 12767.57`}};
 g1 = DateListPlot[data22];
 
In[3]:= data33 = Map[{AbsoluteTime[First[#]], Last[#]} &, data22]

Out[3]= {
{3377894400, 12582.6}, {3377980800, 12577.2}, {3378067200, 12567.9}, {3378153600, 12565.5},
{3378412800, 12477.2}, {3378499200,12533.8}, {3378585600, 12621.8}, {3378672000, 12502.6},
{3378758400, 12487.}, {3379017600, 12490.8}, {3379104000, 12523.3}, {3379190400, 12621.7},
{3379276800, 12673.7}, {3379363200, 12653.5}, {3379622400, 12661.7}, {3379708800, 12666.3},
{3379795200, 12666.9}, {3379881600, 12637.6}, {3379968000, 12580.8}, {3380227200, 12552.6},
{3380313600, 12654.9}, {3380400000, 12741.9}, {3380486400, 12765.}, {3380572800, 12767.6}}

In[4]:= nlm = Normal[NonlinearModelFit[data33, a + b*(t)^0.3, {a, b}, t]]

Out[4]= -745837. + 1050.21 t^0.3

In[5]:= g2 = Plot[nlm, {t, data33[[1, 1]], data33[[-1, 1]]}, PlotStyle -> {Black}];
Show[g1, g2]

Out[6]= ...PlotSnipped...
Since your dates are not sequential, assigning sequential numbers may not be good. Perhaps use Absolute Seconds instead.
Attachments:
POSTED BY: Bill Simpson
Posted 11 years ago
Thanks a lot!
But I want to use days, but not seconds as a time variable. I fix you solution, and get a working code. It looks like very ugly but it works.
 data22 = {
 
  {{2007, 1, 16}, 12582.59`}, {{2007, 1, 17}, 12577.15`}, {{2007, 1, 18}, 12567.93`}, {{2007, 1, 19}, 12565.53`},
 
   {{2007, 1, 22}, 12477.17`}, {{2007, 1, 23}, 12533.8`}, {{2007, 1, 24}, 12621.77`}, {{2007, 1, 25}, 12502.56`},
 
   {{2007, 1, 26}, 12487.02`}, {{2007, 1, 29}, 12490.78`}, {{2007, 1, 30}, 12523.31`}, {{2007, 1, 31}, 12621.69`},
 
   {{2007, 2, 1}, 12673.68`}, {{2007, 2, 2}, 12653.49`}, {{2007, 2, 5}, 12661.74`}, {{2007, 2, 6}, 12666.31`},

  {{2007, 2, 7}, 12666.87`}, {{2007, 2, 8}, 12637.63`}, {{2007, 2, 9}, 12580.83`}, {{2007, 2, 12}, 12552.55`},

  {{2007, 2, 13}, 12654.85`}, {{2007, 2, 14}, 12741.86`}, {{2007, 2, 15}, 12765.01`}, {{2007, 2, 16}, 12767.57`}};

g1 = DateListPlot[data22];

data33 = Map[{AbsoluteTime[First[#]], Last[#]} &, data22];
dat0 = data33[[1, 1]];
dat1 = AbsoluteTime[{1900, 1, 2}];

nlm = Normal[NonlinearModelFit[data33, a + b*((t-dat0)/dat1)^0.3, {a, b}, t]]
g2 = Plot[nlm, {t, data33[[1, 1]], data33[[-1, 1]]},
   PlotStyle -> {Black}];
Show[g1, g2]
Attachments:
POSTED BY: Ivan Ivan
Posted 11 years ago
I am glad you were able to get it to work. Your regression is over 0 to 31 while your DateListPlot is over a month in 2007. I am surprised you were able to get that to work.
To put both over days you might try this.
 In[1]:= data22 = {
 {{2007, 1, 16}, 12582.59`}, {{2007, 1, 17}, 12577.15`}, {{2007, 1, 18}, 12567.93`}, {{2007, 1, 19}, 12565.53`},
 {{2007, 1, 22}, 12477.17`}, {{2007, 1, 23}, 12533.8`}, {{2007, 1, 24}, 12621.77`}, {{2007, 1, 25}, 12502.56`},
 {{2007, 1, 26}, 12487.02`}, {{2007, 1, 29}, 12490.78`}, {{2007, 1, 30}, 12523.31`}, {{2007, 1, 31}, 12621.69`},
 {{2007, 2, 1}, 12673.68`}, {{2007, 2, 2}, 12653.49`}, {{2007, 2, 5}, 12661.74`}, {{2007, 2, 6}, 12666.31`},
 {{2007, 2, 7}, 12666.87`}, {{2007, 2, 8}, 12637.63`}, {{2007, 2, 9}, 12580.83`}, {{2007, 2, 12}, 12552.55`},
 {{2007, 2, 13}, 12654.85`}, {{2007, 2, 14}, 12741.86`}, {{2007, 2, 15}, 12765.01`}, {{2007, 2, 16}, 12767.57`}};
 dat0 = AbsoluteTime[data22[[1, 1]]];
 dat1 = AbsoluteTime[{1900, 1, 2}];
data33 = Map[{(AbsoluteTime[First[#]] - dat0)/dat1, Last[#]} &, data22]

Out[4]= {
{0, 12582.6}, {1, 12577.2}, {2, 12567.9}, {3, 12565.5},
{6, 12477.2}, {7, 12533.8}, {8, 12621.8}, {9, 12502.6},
{10, 12487.}, {13, 12490.8}, {14, 12523.3}, {15, 12621.7},
{16, 12673.7}, {17, 12653.5}, {20, 12661.7}, {21, 12666.3},
{22, 12666.9}, {23, 12637.6}, {24, 12580.8}, {27, 12552.6},
{28, 12654.9}, {29, 12741.9}, {30, 12765.}, {31, 12767.6}}

In[5]:= nlm = Normal[NonlinearModelFit[data33, a + b*(t)^0.3, {a, b}, t]]

Out[5]= 12479. + 60.8924 t^0.3

In[6]:= Show[ListPlot[data33], Plot[nlm, {t, data33[[1, 1]], data33[[-1, 1]]}]]

Out[6]= ...PlotSnipped...
That at least has both the points and the regression on the same axes.
POSTED BY: Bill Simpson
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