0

I'm trying to do a simple STL Decomposition but the function accuses that is not an univariate series. It does not make sense to me. I searched for answers elsewhere, but none helped me solve this problem. Can you help me?

Time series and stl in R: Error only univariate series are allowed

stl() decomposition won't accept univariate ts object?

pdtv <- ts(Prod, start=c(2006,9), freq=12)
pdtv
          Jan      Feb      Mar      Apr      May      Jun
2006                                                      
2007 2.957986 3.076377 3.032362 3.002684 2.998301 2.998301
2008 3.037797 3.058511 3.024955 3.003206 3.024000 3.024000
2009 2.465526 2.511370 2.460660 2.352522 2.337496 2.337496
2010 3.044934 3.092652 3.126031 3.126108 3.152243 3.152243
2011 3.066405 3.096732 3.259540 3.305672 3.364590 3.373816
2012 2.661912 2.450209 2.441621 2.447879 2.442005 2.442005
2013 3.274132 3.262601 3.305515 3.334852 3.338704 3.348022
2014 3.338500 2.932178 2.931838 2.929874 2.929874 2.950362
2015 3.253992 3.276724 3.266712 3.293284 3.293284 3.292635
2016 3.405285 3.305412 3.159213 3.140374 3.140374 3.116621
2017 3.462615 3.512385 3.592744 3.651052 3.719270 3.734618
2018 3.489051 3.494139 3.478237 3.469897                  
          Jul      Aug      Sep      Oct      Nov      Dec
2006                   2.380385 2.380385 2.379928 2.379928
2007 2.963497 2.963497 2.963497 2.963497 2.963497 2.963497
2008 2.991359 2.991359 2.991359 2.991359 2.991359 2.991359
2009 2.340137 2.340137 2.340137 2.340137 2.340137 2.340137
2010 3.139452 3.139452 3.139452 3.139452 3.139452 3.139452
2011 3.373816 3.373816 3.373816 3.373816 3.373816 3.373816
2012 2.442005 2.452547 2.452547 2.452547 2.452547 2.452547
2013 3.348022 3.348022 3.348022 3.348022 3.348022 3.348022
2014 2.950362 2.950362 2.950362 2.950362 2.950362 2.950362
2015 3.292635 3.292635 3.292635 3.292635 3.292635 3.292635
2016 3.116621 3.116621 3.116621 3.116621 3.116621 3.087348
2017 3.735368 3.735368 3.759308 3.759308 3.759308 3.759308
2018                                                      
pdtv.stl = stl(pdtv)
Error in stl(pdtv) : only univariate series are allowed
Muradim
  • 101
  • 3

1 Answers1

1

I just encountered this problem myself and tried a workaround. Using my example...

"mtrain" is a time-series object for monthly data starting January 2008.

str(mtrain)
 Time-Series [1:105, 1] from 2008 to 2017: 344 263 234 222 287 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr "GOOG.Open"

The dimnames part is the one that apparently causes problems with stl()

stl(mtrain)
Error in stl(mtrain) : only univariate series are allowed

The workaround:

mtrain = ts(as.numeric(mtrain), start=c(2008,1), frequency = 12)

str(mtrain)
 Time-Series [1:105] from 2008 to 2017: 344 263 234 222 287 ...

stl.mtrain = stl(mtrain, s.window = "periodic")

head(stl.mtrain$time.series)
           seasonal    trend remainder
Jan 2008 18.4055569 297.7198  28.07094
Feb 2008  6.1883614 287.9555 -31.51698
Mar 2008  4.2087952 278.1911 -48.16837
Apr 2008 -0.7705219 267.8488 -44.65494
May 2008 -8.4452575 257.5065  38.22522
Jun 2008 -6.3193790 246.9789  48.70846

So converting mtrain back into a simple numeric vector and re-applying the time attributes to it. The dimnames portion is all gone and causes no hassle with stl().

Hope this helps!

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
enkay
  • 31
  • 4