I am trying to de-trend a time series that I know has some seasonality. So I thought I would try to use TimeSeries and DateObject functionality to do it. Here's what I have so far :
input = {{"Date",
"Count"}, {DateObject[{2009, 8, 24},
TimeObject[{0, 0, 0.`}, TimeZone -> 0.`], TimeZone -> 0.`],
327.29`}, {DateObject[{2009, 8, 25},
TimeObject[{0, 0, 0.`}, TimeZone -> 0.`], TimeZone -> 0.`],
347.6`}, {DateObject[{2009, 8, 26},
TimeObject[{0, 0, 0.`}, TimeZone -> 0.`], TimeZone -> 0.`],
294.`}, {DateObject[{2009, 8, 27},
TimeObject[{0, 0, 0.`}, TimeZone -> 0.`], TimeZone -> 0.`],
311.`}, {DateObject[{2009, 8, 28},
TimeObject[{0, 0, 0.`}, TimeZone -> 0.`], TimeZone -> 0.`],
786.`}, {DateObject[{2009, 8, 29},
TimeObject[{0, 0, 0.`}, TimeZone -> 0.`], TimeZone -> 0.`],
867.`}, {DateObject[{2009, 8, 30},
TimeObject[{0, 0, 0.`}, TimeZone -> 0.`], TimeZone -> 0.`],
760.`}, {DateObject[{2009, 8, 31},
TimeObject[{0, 0, 0.`}, TimeZone -> 0.`], TimeZone -> 0.`],
643.`}, {DateObject[{2009, 9, 1},
TimeObject[{0, 0, 0.`}, TimeZone -> 0.`], TimeZone -> 0.`],
435.`}, {DateObject[{2009, 9, 2},
TimeObject[{0, 0, 0.`}, TimeZone -> 0.`], TimeZone -> 0.`],
456.`}, {DateObject[{2009, 9, 3},
TimeObject[{0, 0, 0.`}, TimeZone -> 0.`], TimeZone -> 0.`],
235.`}, {DateObject[{2009, 9, 4},
TimeObject[{0, 0, 0.`}, TimeZone -> 0.`], TimeZone -> 0.`],
346.`}};
assoc = AssociationThread[First[input] -> #] & /@ Rest[input];
v = Flatten[Values[assoc[[All, {"Count"}]]]];
t = Flatten[Values[assoc[[All, {"Date"}]]]];
ts = TimeSeries[v, {t}];
tsmodel = TimeSeriesModelFit[ts]
fitmodel = Fit[ts, {1, x, x^2}, x]
Questions
Is the Flatten really the best way to go from an association to a TS? I tried a number of ways but that was the only one that worked.
How do I subtract the fitted model(s) from the Time series? I have tried the technique here but no luck.
TimeSeries
directly, e.g.DateListPlot@TimeSeries[Rest@input]
. $\endgroup$DateListPlot[{TimeSeries[Rest@input] - TimeSeries[tsmodel /@ input[[2 ;; -1, 1]], {input[[2 ;; -1, 1]]}]}
$\endgroup$