ets() error in R - r

I have a time serie and when applying ets I get an error which I dont know where is generated. My timeserie is not as big or has no such big values.
Do you spot anything wrong?
> ts
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2005 22.84 21.58 21.93 25.25 28.69 30.63 34.47 28.72 36.49 34.77 40.65
2006 36.73 31.55 38.07 34.77 36.91 39.16 36.07 35.37 39.34 35.62 27.58 33.37
2007 37.11 33.32 34.09 34.64 21.05 41.60 36.52 37.63 42.66 38.17 39.26 39.95
2008 35.33 38.63 38.04 36.90 33.56 35.14 33.82 36.18 29.30 25.65 20.71 21.63
2009 17.12 19.02 22.48 24.42 16.94 19.75 24.56 22.55 16.68 17.86 20.83 18.41
2010 14.74 16.49 19.75 22.88 24.11 27.02 27.46 26.47 26.81 26.59 23.56 18.88
> fit = ets (ts)
Error in `-.default`(y, e$e) : non-numeric argument to binary operator
In addition: Warning message:
In ets(ts) :
Very large numbers which may cause numerical problems. Try scaling the data first
>
Thanks.
Update with a traceback:
Traceback:
7: NextMethod(.Generic)
6: Ops.ts(y, e$e)
5: etsmodel(y, errortype[i], trendtype[j], seasontype[k], damped[l],
alpha, beta, gamma, phi, lower = lower, upper = upper, opt.crit = opt.crit,
nmse = nmse, bounds = bounds)
4: ets(ts)
3: eval(expr, envir, enclos)
2: eval(i, envir)
1: sys.source(file = "1.R", envir = .rAenv)

It appears that my ts variable was a wrong format.
mode(ts) // should be "numerical" not "character"
After casting it with as.numeric it worked.

Related

How to find AR,MA,ARIMA.ARMA,SARIMA,SARMA of a time series data in r

library(readxl)
export1 <- read_excel("C:/Users/Hazeeb/OneDrive/Desktop/data/export1.xlsx")
View(export1)
class(export1)
#> [1] "tbl_df" "tbl" "data.frame"
#> export1t=ts(export1$`CURRENT TOTAL EXPORT`, frequency = 12, start = c(2007, 1),end = c(2019,12))
#> export1t
#> Jan Feb Mar Apr May Jun Jul Aug Sep
#> 2007 21.97 52.46 53.69 67.32 58.01 53.44 78.83 41.49 37.05
#> 2008 60.26 73.61 51.58 52.95 51.85 86.90 27.57 72.81 42.29
#> 2009 32.80 49.09 53.57 84.30 50.06 46.05 44.04 38.87 47.03
#> 2010 30.46 54.54 48.64 56.41 32.14 48.26 36.17 41.42 39.25
#> 2011 50.17 32.29 45.84 38.02 59.10 46.73 65.44 42.21 64.97
#> 2012 39.99 47.38 77.35 37.34 57.62 46.82 37.32 38.87 75.89
#> 2013 42.36 32.51 88.73 78.91 42.73 55.51 53.42 49.05 82.50
#> 2014 789.81 115.17 39.49 38.70 46.60 33.98 46.87 44.69 406.71
#> 2015 50.74 38.98 41.21 69.58 49.10 46.40 49.03 61.16 91.44
#> 2016 48.08 41.32 50.20 86.05 45.18 60.36 64.85 60.35 187.72
#> 2017 136.71 121.77 152.54 109.91 83.52 120.64 131.19 59.21 110.85
#> 2018 104.66 88.19 98.40 86.78 61.65 61.22 96.28 70.20 107.98
#> 2019 93.99 78.26 57.61 72.30 89.50 138.46 99.57 71.32 81.77
#> Oct Nov Dec
#> 2007 74.55 41.66 32.44
#> 2008 145.88 132.63 136.22
#> 2009 49.55 36.44 43.18
#> 2010 42.14 32.13 46.10
#> 2011 59.12 57.85 67.52
#> 2012 72.72 58.14 37.51
#> 2013 72.06 601.60 43.00
#> 2014 61.96 67.01 54.34
#> 2015 82.08 89.70 71.91
#> 2016 227.43 106.45 100.47
#> 2017 134.85 145.95 118.22
#> 2018 139.42 117.76 99.14
#> 2019 136.79 146.66 197.65
class(export1t)
#> [1] "ts"
library(tseries)
library(forecast)
plot(export1t,xlab="YEAR",ylab="CURRENT TOTAL EXPORT")
#> Error in plot.new() : figure margins too large
plot.ts(export1t,xlab="YEAR",ylab="CURRENT TOTAL EXPORT")
#> Error in plot.new() : figure margins too large
acf(export1t)
#> Error in plot.new() : figure margins too large
pacf(export1t)
#> Error in plot.new() : figure margins too large
Your syntax isn't quite right here. When using ts, you should set a frequency (here 12, to represent months). Then, the start argument needs two integers - the starting year and starting month.
You are trying to pass the first item of DATE to start, but a) that's not the type of argument start takes, b) you didn't have quotes around the character string, and c) even if you did, this is not a date format recognised by R.
You need only do:
ts(export1$`CURRENT TOTAL EXPORT`, frequency = 12, start = c(2007, 1))
#> Jan Feb Mar Apr May Jun Jul Aug Sep Oct
#> 2007 22.0 52.5 53.7 67.3 58.0 53.4 78.8 41.5 37.0 74.6

r - Plotting monthly time series data in R - cannot plot more than 10 series

I'm having a lot of trouble plotting my time series data in R Studio. My data is laid out as follows:
tsf
Time Series:
Start = 1995
End = 2021
Frequency = 1
Jan Feb Mar Apr May Jun July Aug Sep Oct Nov Dec
1995 10817 8916 9697 10314 9775 7125 9007 6000 4155 3692 2236 996
1996 12773 12562 13479 14280 13839 9168 10959 6582 5162 4815 3768 1946
1997 14691 12982 13545 14131 14162 10415 11420 7870 6340 6869 6777 6637
1998 17192 15480 14703 16903 15921 13381 13779 9127 6676 6511 5419 3447
1999 13578 19470 23411 18190 18979 17296 16588 12561 10405 8537 7304 4003
2000 20100 29419 30125 27147 27832 23874 19728 15847 11477 9301 6933 3486
2001 16528 22258 22146 19027 19436 15688 14558 10609 6799 6563 4816 2480
2002 14724 19424 21391 17215 18775 13017 14385 10044 7649 6598 4497 2766
2003 17051 20182 18564 18484 15365 12180 13313 8859 6830 6371 3781 2012
2004 16875 20084 21150 19057 16153 13619 14144 9599 7390 5830 3763 2033
2005 20002 24153 23160 20864 18331 14950 14149 11086 7475 6290 3779 2134
2006 24605 26384 24858 20634 18951 15048 14905 10749 7259 5479 3074 1509
2007 29281 26495 25974 21427 20232 15465 15738 10006 6674 5301 2857 1304
2008 32961 24290 20190 17587 12172 7369 16175 6822 4364 2699 1174 667
2009 10996 8793 7345 5558 4840 4833 4355 2422 2272 1596 948 474
2010 10469 11707 12379 9599 8893 8314 7018 5310 4683 3742 2146 647
2011 13624 13470 12390 11171 9359 9240 6953 3653 2861 2216 1398 597
2012 14507 10993 10581 9388 7986 5481 6164 3736 2783 2442 1421 774
2013 10735 9671 10596 8113 7095 3293 9306 4504 3257 2832 1307 639
2014 15975 11906 11485 11757 7767 3390 14037 6201 4376 3082 1465 920
2015 20105 15384 17054 13166 9027 3924 21290 8572 5924 3943 1874 847
2016 27106 21173 20096 14847 10125 4143 22462 9781 5842 3831 1846 679
2017 26668 16905 17180 13427 9581 3585 21316 8105 4828 3255 1594 601
2018 25813 16501 16088 11557 9362 3716 20743 7681 4397 2874 1647 778
2019 22279 14178 14404 13794 9126 3858 18741 7202 4104 3214 1676 729
2020 20665 13263 10239 1338 1490 2189 15329 7360 5747 4189 1468 1032
2021 16948 11672 10672 8214 7337 4980 20232 8563 6354 3882 2167 832
When I attempt rudimentary code to plot the data I get the following
plot(tsf)
'Error in plotts(x = x, y = y, plot.type = plot.type, xy.labels = xy.labels, :
cannot plot more than 10 series as "multiple"'
My data is monthly and therefore 12 months exceed this apparent limit of 10 graphs.I've been able to make some plot by excluding two months but this is not practical for me.
I've looked at lots of answers on this, many of which recommending ggplot() {ggplot2}
The link below had data most closely resembling my data but I still wasn't able to apply it.
issues plotting multivariate time series in R
Any help greatly appreciated.
I think the problem is with the shape of your data. It's indicating Frequency = 1, showing that it thinks the monthly columns are separate yearly time series, rather than a continuous time series across months. To plot the whole time length you can reshape your time series to match monthly frequency (from a simulated dataset of values):
tsf_switched <- ts(as.vector(t(tsf)), start = 1995, frequency = 12)
plot(tsf)
Created on 2022-05-07 by the reprex package (v2.0.1)
one solution with {ggplot2} and two convenience libraries:
library(dplyr)
library(tsbox) ## for convenient ts -> dataframe conversion
library(lubridate) ## time and date manipulation
## example monthly data for twelve years:
example_ts <- ts(runif(144), start = 2010, end = 2021, frequency = 12)
ts_data.frame(example_ts) %>% ## {tsbox}
mutate(year = year(time),
day_month = paste0(day(time),'/', month(time))
) %>%
ggplot() +
geom_line(aes(day_month,
value,
group = year
)
)
ways to convert time series to dataframes (required as ggplot input): Transforming a time-series into a data frame and back

How can i forecast with same accuracy in GRETL and R

My timeseries comes from the datasets package. It is called "USAccDeaths".
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1973 9007 8106 8928 9137 10017 10826 11317 10744 9713 9938 9161 8927
1974 7750 6981 8038 8422 8714 9512 10120 9823 8743 9129 8710 8680
1975 8162 7306 8124 7870 9387 9556 10093 9620 8285 8466 8160 8034
1976 7717 7461 7767 7925 8623 8945 10078 9179 8037 8488 7874 8647
1977 7792 6957 7726 8106 8890 9299 10625 9302 8314 8850 8265 8796
1978 7836 6892 7791 8192 9115 9434 10484 9827 9110 9070 8633 9240
When I make an out of sample forcast in GRETL i get the follwoing:
However, when i make the same forecast in R, my results differ subtantially.
This is my r code:
library(forecast)
fit <- arima(USAccDeaths[1:60], order = c(1,1,0), method = "ML")
preds <- as.vector(forecast(fit, h = 12))$mean
RMSE <- sqrt(mean((preds - as.vector(USAccDeaths[61:72])) ^ 2))
RMSE
I get an RMSE of 946.024. This is my predictions in R:
[1] 8803.104 8803.199 8803.201 8803.201 8803.201 8803.201 8803.201 8803.201 8803.201
[10] 8803.201 8803.201 8803.201here
What is the problem? How can I get the same results in both programs?

Error plotting a zoo class series data with R

I am new to the community of stackoverflow and this is the first question I ask. Please let me know if I did something wrong.
Here is the situation with my problem. I am dealing with Australian electricity prices, and my time series look like this. This is a high frequency data that is sampled every 30 minutes.
> head(t)
VIC NSW QLD SNOWY SA
1999-01-01 00:00:00 26.84 24.29 26.52 26.20 29.87
1999-01-01 00:30:00 30.52 27.64 19.34 29.74 36.01
1999-01-01 01:00:00 28.74 26.64 17.47 28.34 35.70
1999-01-01 01:30:00 27.94 25.81 17.08 27.43 31.67
1999-01-01 02:00:00 20.90 19.94 15.84 20.86 22.42
1999-01-01 02:30:00 20.26 19.48 15.68 20.28 21.38
> tail(t)
VIC NSW QLD SNOWY SA
2006-12-31 21:00:00 14.59 15.10 13.72 15.35 29.60
2006-12-31 21:30:00 14.77 15.42 14.12 15.61 28.79
2006-12-31 22:00:00 14.12 15.01 13.54 15.06 20.59
2006-12-31 22:30:00 15.15 16.19 15.10 16.21 17.44
2006-12-31 23:00:00 15.17 16.14 15.48 16.18 17.84
2006-12-31 23:30:00 16.96 17.14 16.37 17.63 20.20
> class(t)
[1] "xts" "zoo"
I was trying to come up with mean prices for each time point across a day. So I did this:
> half.hourly.means <- aggregate(t$VIC, list(format(index(t), "%H:%M")), FUN = mean)
> head(half.hourly.means)
00:00 26.99938
00:30 24.67273
01:00 21.78190
01:30 26.46662
02:00 21.27931
02:30 18.57727
> tail(half.hourly.means)
21:00 27.86881
21:30 26.65468
22:00 23.51793
22:30 25.68527
23:00 23.26385
23:30 30.01726
> class(half.hourly.means)
[1] "zoo"
This outcome is average prices of each time point across the sample period. Everything worked fine by far. But when I tried to plot it, an error occurred.
> plot(half.hourly.means)
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf
When I tried this
> plot(as.numeric(half.hourly.means), type = "l")
(I failed to post a image for lack of reputation, sorry)
It yields the correct plot, but with meaningless x axis values. So my question is: what could be done to produce the above graph with x axis values being "00:00", "00:30", "01:00", ... ?
Thanks for your patience!
Best regards,
Wei

Does ETS smoothing model work for more than one year forecast in R?

I am currently using ets() to forecast future values based on historic time series data in R. I used forecast() function to predict next 24 data points. However, the output gives same numbers for the first 12 and the last 12 data points. For example, the forecast-ed value of May 2012 is replicated in May 2013.
Following Data passed:
2005.04.30 87.6
2005.05.31 95.4
2005.06.30 97.7
2005.07.31 101.3
2005.08.31 100.6
2005.09.30 97
2005.10.31 91.1
2005.11.30 92.1
2005.12.31 112
2006.01.31 113.9
2006.02.28 103.9
2006.03.31 115.1
2006.04.30 100
2006.05.31 107.5
2006.06.30 110
2006.07.31 114.2
2006.08.31 109.4
2006.09.30 108.9
2006.10.31 114.6
2006.11.30 113
2006.12.31 116.5
2007.01.31 120.2
2007.02.28 112.6
2007.03.31 124.1
2007.04.30 113.4
2007.05.31 121
2007.06.30 117.9
2007.07.31 118.4
2007.08.31 119.5
2007.09.30 113.5
2007.10.31 117.8
2007.11.30 118.2
2007.12.31 120.6
2008.01.31 126.1
2008.02.29 121.2
2008.03.31 127.4
2008.04.30 119.5
2008.05.31 121.5
2008.06.30 125.7
2008.07.31 131.4
2008.08.31 123.5
2008.09.30 122.8
2008.10.31 125.3
2008.11.30 119.4
2008.12.31 121.2
2009.01.31 123.7
2009.02.28 118.1
2009.03.31 128.7
2009.04.30 112.2
2009.05.31 115.4
2009.06.30 119.8
2009.07.31 117.4
2009.08.31 127.8
2009.09.30 124.4
2009.10.31 131
2009.11.30 118.9
2009.12.31 124
2010.01.31 127.4
2010.02.28 116.3
2010.03.31 126.4
2010.04.30 115.7
2010.05.31 117.7
2010.06.30 122.4
2010.07.31 121.9
2010.08.31 116.7
2010.09.30 110.9
2010.10.31 120.7
2010.11.30 116.7
2010.12.31 131.2
2011.01.31 137.1
2011.02.28 118.7
2011.03.31 128.5
2011.04.30 123.5
2011.05.31 126.1
2011.06.30 127.7
2011.07.31 125.3
2011.08.31 126.7
2011.09.30 114
2011.10.31 116.5
2011.11.30 128
2011.12.31 130.6
Code:
ETSfit <- ets(data.ts)
data.ets <- forecast(ETSfit, level=70, h=24)
Output:
Point Forecast Lo 70 Hi 70
Jan 2012 133.6314 129.3483 137.9145
Feb 2012 123.5998 118.7221 128.4775
Mar 2012 133.1607 127.7534 138.5681
Apr 2012 121.0877 115.1982 126.9773
May 2012 125.4991 119.1639 131.8342
Jun 2012 127.5913 120.8399 134.3427
Jul 2012 128.4923 121.3489 135.6358
Aug 2012 127.2225 119.7074 134.7376
Sep 2012 122.1938 114.3247 130.0630
Oct 2012 125.5382 117.3302 133.7462
Nov 2012 123.3347 114.8012 131.8682
Dec 2012 129.9972 121.1503 138.8441
Jan 2013 133.6314 124.4818 142.7810
Feb 2013 123.5998 114.1572 133.0424
Mar 2013 133.1607 123.4340 142.8875
Apr 2013 121.0877 111.0849 131.0906
May 2013 125.4991 115.2275 135.7706
Jun 2013 127.5913 117.0579 138.1246
Jul 2013 128.4923 117.7035 139.2812
Aug 2013 127.2225 116.1841 138.2609
Sep 2013 122.1938 110.9114 133.4763
Oct 2013 125.5382 114.0169 137.0595
Nov 2013 123.3347 111.5793 135.0901
Dec 2013 129.9972 118.0123 141.9821
Kindly help.
Look at the fitted model:
ETS(A,N,A)
Call:
ets(y = x)
Smoothing parameters:
alpha = 0.5449
gamma = 1e-04
Initial states:
l = 95.8994
s=6.3817 -3.1792 6.8525 3.218 -3.4445 -1.2408
-4.5852 0.4434 1.7133 0.8123 -1.28 -5.6914
sigma: 4.1325
AIC AICc BIC
613.8103 620.1740 647.3326
So there is no trend selected. Therefore the forecasts will have only seasonal pattern and no trend, which is exactly what you've got.

Resources