Mathematica TimeSeries将每个月除以平均值

时间:2016-01-05 14:39:06

标签: wolfram-mathematica

我有2年的气候数据集。我想将每个月除以其平均值。第一部分是每个日期后跟两个值。

作为输出我想要一个表,其中每个值除以月平均值。

我已经尝试过TimeSeriesAggregate和TimeSeriesWindow但我无法完成它。我可以通过monat = TimeSeriesAggregate[UNI, "Month"]获得一行的月份平均值。

感谢您的帮助。

    {{{2012, 5, 2}, 560.352, 569.852}, {{2012, 5, 3}, 468.519, 
  359.593}, {{2012, 5, 4}, 227.648, 236.704}, {{2012, 5, 5}, 640.056, 
  505.833}, {{2012, 5, 6}, 538.426, 537.907}, {{2012, 5, 7}, 312.389, 
  318.519}, {{2012, 5, 8}, 732.574, 706.852}, {{2012, 5, 9}, 725.167, 
  692.926}, {{2012, 5, 10}, 679.852, 672.481}, {{2012, 5, 11}, 
  657.389, 640.148}, {{2012, 5, 12}, 399.463, 
  382.519}, {{2012, 5, 13}, 247.815, 322.981}, {{2012, 5, 14}, 
  319.093, 337.444}, {{2012, 5, 15}, 742.333, 
  716.889}, {{2012, 5, 16}, 182.296, 179.444}, {{2012, 5, 17}, 
  693.963, 672.5}, {{2012, 5, 18}, 755.222, 736.778}, {{2012, 5, 19}, 
  740.667, 728.667}, {{2012, 5, 20}, 716.778, 
  701.722}, {{2012, 5, 21}, 161.167, 147.778}, {{2012, 5, 22}, 64.463,
   69.6111}, {{2012, 5, 23}, 527.222, 482.741}, {{2012, 5, 24}, 
  578.648, 611.981}, {{2012, 5, 25}, 524.093, 
  520.685}, {{2012, 5, 26}, 516.704, 562.704}, {{2012, 5, 27}, 
  448.093, 403.296}, {{2012, 5, 28}, 590.444, 
  610.741}, {{2012, 5, 29}, 621.074, 712.556}, {{2012, 5, 30}, 
  553.481, 525.167}, {{2012, 5, 31}, 495.093, 477.907}, {{2012, 6, 1},
   554.315, 544.}, {{2012, 6, 2}, 267.907, 315.556}, {{2012, 6, 3}, 
  724.815, 695.444}, {{2012, 6, 4}, 276.426, 283.981}, {{2012, 6, 5}, 
  727.185, 708.926}, {{2012, 6, 6}, 578.185, 581.056}, {{2012, 6, 7}, 
  677.056, 655.481}, {{2012, 6, 8}, 613.537, 621.685}, {{2012, 6, 9}, 
  83.0926, 81.7593}, {{2012, 6, 10}, 136.481, 97.963}, {{2012, 6, 11},
   194.944, 136.167}, {{2012, 6, 12}, 239.722, 
  226.537}, {{2012, 6, 13}, 436.833, 414.611}, {{2012, 6, 14}, 609.37,
   736.704}, {{2012, 6, 15}, 756.037, 722.259}, {{2012, 6, 16}, 
  763.907, 746.167}, {{2012, 6, 17}, 748.407, 
  733.685}, {{2012, 6, 18}, 740.981, 732.759}, {{2012, 6, 19}, 571., 
  554.759}, {{2012, 6, 20}, 709.037, 696.685}, {{2012, 6, 21}, 
  608.481, 612.315}, {{2012, 6, 22}, 412.963, 421.5}, {{2012, 6, 23}, 
  509.963, 516.574}, {{2012, 6, 24}, 740.148, 
  712.093}, {{2012, 6, 25}, 81.1481, 83.9259}, {{2012, 6, 26}, 
  655.222, 676.944}, {{2012, 6, 27}, 581.074, 
  696.667}, {{2012, 6, 28}, 694.519, 701.407}, {{2012, 6, 29}, 
  599.111, 552.722}, {{2012, 6, 30}, 720.204, 709.278}, {{2012, 7, 1},
   689.556, 677.407}, {{2012, 7, 2}, 697.333, 675.333}, {{2012, 7, 3},
   534.37, 517.704}, {{2012, 7, 4}, 589.5, 601.63}, {{2012, 7, 5}, 
  545.667, 518.889}, {{2012, 7, 6}, 680.13, 661.759}, {{2012, 7, 7}, 
  725.13, 715.759}, {{2012, 7, 8}, 732.278, 661.333}, {{2012, 7, 9}, 
  471.741, 581.333}, {{2012, 7, 10}, 680.944, 
  654.741}, {{2012, 7, 11}, 564.63, 520.037}, {{2012, 7, 12}, 316.667,
   306.019}, {{2012, 7, 13}, 98.2037, 79.2222}, {{2012, 7, 14}, 
  550.407, 508.222}, {{2012, 7, 15}, 199.87, 223.667}, {{2012, 7, 16},
   520.426, 595.185}, {{2012, 7, 17}, 544.778, 
  563.426}, {{2012, 7, 18}, 608.741, 679.333}, {{2012, 7, 19}, 
  685.907, 672.389}, {{2012, 7, 20}, 411.704, 
  425.296}, {{2012, 7, 21}, 62.5926, 57.0185}, {{2012, 7, 22}, 
  444.907, 527.093}, {{2012, 7, 23}, 592.759, 
  604.741}, {{2012, 7, 24}, 535.13, 488.315}, {{2012, 7, 25}, 389.481,
   358.444}, {{2012, 7, 26}, 564.537, 516.481}, {{2012, 7, 27}, 
  618.074, 608.278}, {{2012, 7, 28}, 665.167, 
  653.278}, {{2012, 7, 29}, 428.87, 439.778}, {{2012, 7, 30}, 496.485,
   450.1}, {{2012, 7, 31}, 693.241, 683.222}, {{2012, 8, 1}, 618.278, 
  658.963}, {{2012, 8, 2}, 645.741, 654.537}, {{2012, 8, 3}, 427.519, 
  396.648}, {{2012, 8, 4}, 681.074, 661.63}, {{2012, 8, 5}, 656.963, 
  649.167}, {{2012, 8, 6}, 666.722, 641.907}, {{2012, 8, 7}, 565.463, 
  501.407}, {{2012, 8, 8}, 298.056, 389.444}, {{2012, 8, 9}, 335.704, 
  271.796}, {{2012, 8, 10}, 441.278, 403.722}, {{2012, 8, 11}, 357.5, 
  338.5}, {{2012, 8, 12}, 704.889, 690.537}, {{2012, 8, 13}, 542.389, 
  672.407}, {{2012, 8, 14}, 660.611, 653.167}, {{2012, 8, 15}, 
  644.519, 602.593}, {{2012, 8, 16}, 582.056, 
  557.444}, {{2012, 8, 17}, 557.796, 607.815}, {{2012, 8, 18}, 
  580.556, 609.056}, {{2012, 8, 19}, 633.019, 
  622.981}, {{2012, 8, 20}, 643.87, 629.13}, {{2012, 8, 21}, 544.889, 
  520.5}, {{2012, 8, 22}, 414.111, 480.13}, {{2012, 8, 23}, 478.167, 
  471.019}, {{2012, 8, 24}, 581.222, 576.5}, {{2012, 8, 25}, 599.481, 
  585.519}, {{2012, 8, 26}, 47.0926, 45.2963}, {{2012, 8, 27}, 
  624.722, 545.204}, {{2012, 8, 28}, 625.333, 
  611.778}, {{2012, 8, 29}, 579.389, 569.13}, {{2012, 8, 30}, 562.315,
   537.667}, {{2012, 8, 31}, 77.2963, 74.2593}, {{2012, 9, 1}, 
  65.3148, 64.8889}, {{2012, 9, 2}, 528.056, 530.963}, {{2012, 9, 3}, 
  455.519, 463.741}, {{2012, 9, 4}, 294.667, 252.019}, {{2012, 9, 5}, 
  490.537, 454.574}, {{2012, 9, 6}, 397.444, 387.185}, {{2012, 9, 7}, 
  542.981, 530.333}, {{2012, 9, 8}, 586.278, 571.222}, {{2012, 9, 9}, 
  570.037, 555.5}, {{2012, 9, 10}, 536.056, 527.556}, {{2012, 9, 11}, 
  531.815, 516.37}, {{2012, 9, 12}, 352.296, 324.222}, {{2012, 9, 13},
   111.704, 110.407}, {{2012, 9, 14}, 213.148, 
  201.278}, {{2012, 9, 15}, 433.611, 373.389}, {{2012, 9, 16}, 
  349.463, 328.481}, {{2012, 9, 17}, 535.296, 
  522.037}, {{2012, 9, 18}, 526.5, 510.759}, {{2012, 9, 19}, 97.9259, 
  105.074}, {{2012, 9, 20}, 486.704, 477.944}, {{2012, 9, 21}, 
  492.204, 486.}, {{2012, 9, 22}, 449.148, 419.241}, {{2012, 9, 23}, 
  451.167, 444.407}, {{2012, 9, 24}, 352.333, 
  345.111}, {{2012, 9, 25}, 477.574, 467.444}, {{2012, 9, 26}, 
  234.944, 197.093}, {{2012, 9, 27}, 218.5, 199.5}, {{2012, 9, 28}, 
  429.481, 415.37}, {{2012, 9, 29}, 117.63, 114.185}, {{2012, 9, 30}, 
  79.0556, 71.6852}, {{2012, 10, 1}, 427.167, 
  353.704}, {{2012, 10, 2}, 141.87, 143.519}, {{2012, 10, 3}, 466.907,
   450.796}, {{2012, 10, 4}, 415.796, 391.648}, {{2012, 10, 5}, 
  282.889, 267.333}, {{2012, 10, 6}, 436.852, 
  422.574}, {{2012, 10, 7}, 326.667, 322.519}, {{2012, 10, 8}, 
  423.019, 398.463}, {{2012, 10, 9}, 290.444, 
  268.87}, {{2012, 10, 10}, 61.5185, 65.3519}, {{2012, 10, 11}, 
  117.093, 104.667}, {{2012, 10, 12}, 59.8889, 
  55.463}, {{2012, 10, 13}, 86.0556, 74.463}, {{2012, 10, 14}, 331.87,
   389.111}, {{2012, 10, 15}, 324.926, 332.148}, {{2012, 10, 16}, 
  65.7778, 61.5185}, {{2012, 10, 17}, 360.574, 
  336.389}, {{2012, 10, 18}, 373.741, 363.093}, {{2012, 10, 19}, 
  402.833, 386.87}, {{2012, 10, 20}, 379.204, 
  355.463}, {{2012, 10, 21}, 370.204, 356.889}, {{2012, 10, 22}, 
  353.056, 338.537}, {{2012, 10, 23}, 347.074, 
  306.259}, {{2012, 10, 24}, 52.2407, 49.4815}, {{2012, 10, 25}, 
  42.537, 40.7778}, {{2012, 10, 26}, 46.5556, 
  40.1852}, {{2012, 10, 27}, 91.4074, 95.4259}, {{2012, 10, 28}, 
  118.37, 107.704}, {{2012, 10, 29}, 231.87, 
  217.667}, {{2012, 10, 30}, 246.722, 235.389}, {{2012, 10, 31}, 
  318.611, 305.667}, {{2012, 11, 1}, 62.963, 71.3704}, {{2012, 11, 2},
   56.0741, 47.2407}, {{2012, 11, 3}, 125.704, 
  104.574}, {{2012, 11, 4}, 177.778, 159.241}, {{2012, 11, 5}, 
  29.6481, 28.1667}, {{2012, 11, 6}, 153.13, 133.833}, {{2012, 11, 7},
   220.796, 221.648}, {{2012, 11, 8}, 275.444, 
  265.704}, {{2012, 11, 9}, 260.204, 256.852}, {{2012, 11, 10}, 
  129.37, 109.704}, {{2012, 11, 11}, 212.352, 
  203.722}, {{2012, 11, 12}, 17.4259, 18.2963}, {{2012, 11, 13}, 
  70.8889, 70.7037}, {{2012, 11, 14}, 87.0556, 
  97.5185}, {{2012, 11, 15}, 210.056, 197.167}, {{2012, 11, 16}, 
  59.3704, 59.6296}

2 个答案:

答案 0 :(得分:0)

{DateObject[#[[1, 1, 1 ;; 2]]],
    Mean[Flatten[#[[All, 2 ;; 3]]]]} & /@ 
           GatherBy[data, #[[1, 1 ;; 2]] &]
DateListPlot@%

enter image description here

如果您希望每天使用两个值的单独方法,请忽略Flatten

使用TimeSeriesAggregate

编辑..
DateListPlot[TimeSeriesAggregate[{#[[1]],
     Mean[#[[2 ;; 3]] ]} & /@ data, "Month"]]

结果略有不同,因为TimeSeriesAggregate平均超过一个月的间隔,但不一定与日历月对齐。

答案 1 :(得分:0)

TimeSeriesAggregate可用于获取每月均值,TimeSeriesMapThread可将每日价值除以其月平均值。我已将您的值分配给变量dat

首先我们创建一个TimeSeries。我注意到您的数据不是在本月1日开始的。 TimeSeriesAggreate将从第一天的那天开始选择每月窗口。我添加了2012年5月1日的值,以便在月份的第1天开始汇总。

ts = TimeSeriesInsert[
  TimeSeries[{#[[1]], #[[2 ;; 3]]} & /@ dat], {{2012, 5, 1}, dat[[1, 2 ;; 3]]}];

使用ts计算月平均值。我将"DatePath"用于映射函数。

mthTs = TimeSeriesAggregate[ts, {"Month", Left}]["DatePath"];

映射函数将同时使用日期和值来决定要通过哪种方法进行标准化。

stdByMean[date_, values_] := 
 values/mthTs[[LengthWhile[mthTs, DateObject@date >= First@# &], 2]]

stdByMean贯穿时间序列以产生所需的结果。

TimeSeriesMapThread[stdByMean, ts]

请注意,在您的数据中,上个月不是整月,因此标准化并不严格正确。

希望这会有所帮助。