问题
I need help from you guys to decompose my monthly data which have seasonality, but it does not work because NA values are not removed..There may be another problem. Please look at my data and errors as below.
ts.monthly<-ts(monthly$rBC.median, frequency=12, start=c(2006, 4))
ts.monthly
Jan Feb Mar Apr May Jun
2006 5.1656479 6.2847959 19.4833690
2007 1.4252665 2.9127775 2.8912652 7.5326158 8.6182227 23.2129310
2008 NA 1.8200842 1.3488755 2.0700927 5.3541366 8.6916708
2009 1.2531161 1.5075780 2.4955524 10.6724704 10.1367162 16.0362127
2010 0.8850190 2.4974866 1.8459976 9.2297697 3.8203789 7.1492986
2011 2.6990434 0.4570701 1.3787403 5.8739804 4.1669501 13.2228535
2012 NA 2.0670538 1.3758499 11.7306663 4.1248775 12.3604423
Jul Aug Sep Oct Nov Dec
2006 9.8028986 7.8167810 2.1333807 2.5777504 1.9022561 2.7254065
2007 4.2121577 8.8604768 12.0017155 4.0978332 1.6053110 NA
2008 5.7338211 9.7432563 4.6548508 1.3589789 0.9650082 1.2788504
2009 11.7632775 11.2299683 1.6229679 1.0333217 1.0481580 1.0734208
2010 3.5996501 4.3245873 4.4586863 1.6403104 2.8622518 1.2564256
2011 3.0463918 7.1515472 6.5613683 1.3715623 1.9757217 5.4901524
2012 11.1010563 3.6220968 2.2597341
ts.monthly=na.omit(ts.monthly)
Error in na.omit.ts(ts.monthly) : time series contains internal NAs
ts.monthly.com<-decompose(ts.monthly)
Error in na.omit.ts(x) : time series contains internal NAs
ts.monthly$seasonal
Error in ts.monthly$seasonal : $ operator is invalid for atomic vectors
I do not understand why na.omit does not work.. how can I treat this NA??
Finally, after using a function "decompose", I want to take only "trend" without seasonality, and then apply sen's slope estimator to get a slope for linear trend. It will work??
Thanks a lot for your help.
回答1:
Try filling in the missing values using a seasonal Kalman filter by employing na.StructTS
from the zoo package first:
library(zoo)
decompose(na.StructTS(ts.monthly))
zoo has many other na.
functions as well: na.aggregate
, na.approx
, na.fill
, na.locf
, na.spline
, na.StructTS
, na.trim
.
回答2:
The X-13ARIMA-SEATS software, accessible by the R-package seasonal, handles missing values and seasonal decomposition in a single step:
library(seasonal)
# a monthly time series with some missing values
AirPassengersNA <- AirPassengers
AirPassengersNA[c(2, 24)] <- NA
m <- seas(AirPassengersNA, na.action = na.x13)
head(m$data)
final seasonal seasonaladj trend irregular adjustfac
[1,] 122.5860 0.9029705 122.5860 122.6289 0.9996500 0.9136445
[2,] 123.8615 0.9492046 123.8615 123.8656 0.9999671 0.9408045
[3,] 125.0191 1.0701984 125.0191 125.3132 0.9976535 1.0558387
[4,] 127.4633 1.0028864 127.4633 126.6222 1.0066428 1.0120561
[5,] 127.2526 0.9494692 127.2526 126.8592 1.0031006 0.9508650
[6,] 126.0700 1.0771444 126.0700 126.1723 0.9991886 1.0708339
回答3:
I struggled with this for a long time too.
Just use na.locf from the zoo package on your ts object. na.locf returns a ts object, so there are no worries about a changed object type.
Use:
library(zoo)
season_ts <- na.locf(season_ts)
where season_ts is your ts object.
回答4:
The imputeTS package is a R package solely dedicated to replacing missing values in time series. You can use a function of the package before performing your decomposition.
The na.seadec() and the na.kalman() are especially good for replacing missing data in seasonal time series. But there are also other advanced methods available (Link to imputeTS Paper).
You would use it like this for this problem:
library(imputeTS)
x <- decompose(na.seadec(yourTimeSeries))
or if you want to use the na.kalman method instead:
library(imputeTS)
x <- decompose(na.kalman(yourTimeSeries))
来源:https://stackoverflow.com/questions/24694558/seasonal-decompose-of-monthly-data-including-na-in-r