如何将数据框导入和转换为时间序列

时间:2016-01-30 09:28:46

标签: r time-series

我从excel

导入这个数据框
test <- readXL("C:/Users/MOUSTAPHA/Desktop/test.xlsx", rownames=TRUE, 
                 header=TRUE, na="", sheet="Feuil1", stringsAsFactors=TRUE)

test

     jan. feb.  mar    ap  may juin. july aug. sep.  oct nov. dec.
2010 62.1 89.0 86.1  59.0 18.8  92.1 15.8 92.2 20.3 22.6 20.8 44.0
2011 20.3 22.6 35.2  14.4 80.5  20.9 15.5 17.2 43.3 20.5 76.7 17.0
2012 28.8 21.4 28.4  31.6 36.3  44.8 53.2 52.6 55.6  0.0 49.4 93.3
2013 11.6  0.0 10.9  40.2 17.3  24.6 29.5 95.1 45.7 23.8 24.1 24.7
2014 10.9 16.2 58.9  14.3 20.7  15.5 14.6 14.4 14.2 44.1 17.7 26.2
2015 53.4 25.1 15.0 155.0  9.1  45.6 32.5 15.0  0.0 15.2 34.3 30.8

我尝试使用数据创建(testts)时间序列:

testts <- ts(test , start = c(2010,1), frequency=12)

testts
     jan feb mar  ap may juin july aug sep oct nov dec
2010  62  89  86  59  19   92   16  92  20  23  21  44
2011  20  23  35  14  81   21   16  17  43  21  77  17
2012  29  21  28  32  36   45   53  53  56   0  49  93
2013  12   0  11  40  17   25   30  95  46  24  24  25
2014  11  16  59  14  21   16   15  14  14  44  18  26
2015  53  25  15 155   9   46   33  15   0  15  34  31

当我尝试使用decompose(testts)对其进行分解时出现此错误:

  

过滤器&#39;比时间序列长

如何导入数据并将其分解,就像示例&#39; AirPassengers&#39; ?

1 个答案:

答案 0 :(得分:0)

data.frame传递给ts时似乎存在问题。但是,在将unlist添加到您正确的代码中时,可以轻松解决此问题,即

testts <- ts(unlist(test) , start = c(2010, 1), frequency = 12)
decompose(testts)

这是与分解的时间序列相关的控制台输出。

$x
       Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov   Dec
2010  62.1  20.3  28.8  11.6  10.9  53.4  89.0  22.6  21.4   0.0  16.2  25.1
2011  86.1  35.2  28.4  10.9  58.9  15.0  59.0  14.4  31.6  40.2  14.3 155.0
2012  18.8  80.5  36.3  17.3  20.7   9.1  92.1  20.9  44.8  24.6  15.5  45.6
2013  15.8  15.5  53.2  29.5  14.6  32.5  92.2  17.2  52.6  95.1  14.4  15.0
2014  20.3  43.3  55.6  45.7  14.2   0.0  22.6  20.5   0.0  23.8  44.1  15.2
2015  20.8  76.7  49.4  24.1  17.7  34.3  44.0  17.0  93.3  24.7  26.2  30.8

$seasonal
            Jan        Feb        Mar        Apr        May        Jun        Jul        Aug        Sep        Oct        Nov        Dec
2010  -2.724722  15.576944   9.364444 -10.520556 -11.089722 -18.260556  36.462778 -15.523056  -5.204722   1.179444 -14.821389  15.561111
2011  -2.724722  15.576944   9.364444 -10.520556 -11.089722 -18.260556  36.462778 -15.523056  -5.204722   1.179444 -14.821389  15.561111
2012  -2.724722  15.576944   9.364444 -10.520556 -11.089722 -18.260556  36.462778 -15.523056  -5.204722   1.179444 -14.821389  15.561111
2013  -2.724722  15.576944   9.364444 -10.520556 -11.089722 -18.260556  36.462778 -15.523056  -5.204722   1.179444 -14.821389  15.561111
2014  -2.724722  15.576944   9.364444 -10.520556 -11.089722 -18.260556  36.462778 -15.523056  -5.204722   1.179444 -14.821389  15.561111
2015  -2.724722  15.576944   9.364444 -10.520556 -11.089722 -18.260556  36.462778 -15.523056  -5.204722   1.179444 -14.821389  15.561111

$trend
          Jan      Feb      Mar      Apr      May      Jun      Jul      Aug      Sep      Oct      Nov      Dec
2010       NA       NA       NA       NA       NA       NA 31.11667 32.73750 33.34167 33.29583 35.26667 35.66667
2011 32.81667 31.22500 31.30833 33.40833 35.00417 40.33750 42.94583 42.02917 44.24583 44.84167 43.51667 41.67917
2012 42.81250 44.46250 45.28333 45.18333 44.58333 40.07500 35.39167 32.55833 30.55417 31.76667 32.02083 32.74167
2013 33.72083 33.57083 33.74167 37.00417 39.89583 38.57500 37.48750 38.83333 40.09167 40.86667 41.52500 40.15417
2014 35.90000 33.13750 31.08333 25.92083 24.18750 25.43333 25.46250 26.87500 28.00833 26.85000 26.09583 27.67083
2015 29.99167 30.73750 34.47917 38.40417 37.69583 37.60000       NA       NA       NA       NA       NA       NA

$random
            Jan        Feb        Mar        Apr        May        Jun        Jul        Aug        Sep        Oct        Nov        Dec
2010         NA         NA         NA         NA         NA         NA  21.420556   5.385556  -6.736944 -34.475278  -4.245278 -26.127778
2011  56.008056 -11.601944 -12.272778 -11.987778  34.985556  -7.076944 -20.408611 -12.106111  -7.441111  -5.821111 -14.395278  97.759722
2012 -21.287778  20.460556 -18.347778 -17.362778 -12.793611 -12.714444  20.245556   3.864722  19.450556  -8.346111  -1.699444  -2.702778
2013 -15.196111 -33.647778  10.093889   3.016389 -14.206111  12.185556  18.249722  -6.110278  17.713056  53.053889 -12.303611 -40.715278
2014 -12.875278  -5.414444  15.152222  30.299722   1.102222  -7.172778 -39.325278   9.148056 -22.803611  -4.229444  32.825556 -28.031944
2015  -6.466944  30.385556   5.556389  -3.783611  -8.906111  14.960556         NA         NA         NA         NA         NA         NA

$figure
 [1]  -2.724722  15.576944   9.364444 -10.520556 -11.089722 -18.260556  36.462778 -15.523056  -5.204722   1.179444 -14.821389  15.561111

$type
[1] "additive"

attr(,"class")
[1] "decomposed.ts"