Error in NextMethod(.Generic) : cannot assign 'tsp' to a vector of length 0

Hello everyone, I am trying to do a rolling forecast with Arima. Looking at my data I need to do a box-box transformation.
But if I add lamda="auto", biasadj=TRUE to any formula, either auto.arima or forecast

library(forecast)
sample <- c(997.791666085149, 999.366182253824, 1000.46381043618, 993.705371491957, 
            996.740285547083, 1003.4906422465, 1005.34156334679, 997.604304813648, 
            1009.6643663447, 993.128043433675, 1005.53114457311, 1004.714338528, 
            1017.72437048901, 1007.94598223837, 988.226025380339, 994.785531675548, 
            1011.97940179699, 998.636119804733, 994.062327383755, 993.979060157697, 
            1003.93555180002, 1001.70926154818, 997.933817810478, 979.693838855987, 
            992.823376865983, 997.838580296017, 1006.21213228311, 991.977377944057, 
            1003.77529414578, 1002.67024480564, 989.082946231366, 989.680523087659, 
            990.781349841785, 1005.75606421591, 997.035360314707, 994.928511836986, 
            996.012597263487, 994.249532642942, 1005.95070720716, 988.48958339183, 
            997.279197474874, 993.176463294942, 1007.25434521494, 1003.7052774576, 
            999.078187434345, 1004.91757624002, 1008.88416631965, 1007.04637128748, 
            1003.12247450407, 1015.17660495696, 1001.97021341098, 997.376118405193, 
            992.973077430376, 994.981878550185, 1003.15585020888, 1007.42839140189, 
            1008.82349023285, 1009.50815752809, 998.249912313893, 996.130572881505, 
            998.1908524304, 1004.27183684979, 1000.25755061327, 999.546581914197, 
            999.586563241259, 987.790001893712, 999.151672336352, 1001.78375990166, 
            999.719712907908, 1006.48174402213, 1003.59120454781, 998.954072595588, 
            1008.72600016238, 1006.72562410066, 1006.89970982311, 1005.21317048907, 
            1003.9871376226, 1007.50194110916, 995.716626103823, 994.981330040876, 
            1000.95853471046, 1000.34298358716, 1004.89210023244, 997.67098695885, 
            999.613667330844, 992.356606521014, 997.889751087205,999.572103909505, 
            1011.73124586553, 1000.92379332441, 998.47164223236,993.927783786705, 
            990.932274895557, 995.044718031122, 995.990214970003, 996.671678002519, 
            1015.14438283045, 997.18825887884, 991.291688264167,1012.20878626223, 
            997.791666085149, 999.366182253824, 1000.46381043618,993.705371491957, 
            996.740285547083, 1003.4906422465, 1005.34156334679, 997.604304813648, 
            1009.6643663447, 993.128043433675, 1005.53114457311,1004.714338528, 
            1017.72437048901, 1007.94598223837, 988.226025380339, 994.785531675548, 
            1011.97940179699, 998.636119804733, 994.062327383755, 993.979060157697, 
            1003.93555180002, 1001.70926154818, 997.933817810478,979.693838855987, 
            992.823376865983, 997.838580296017, 1006.21213228311, 991.977377944057, 
            1003.77529414578, 1002.67024480564, 989.082946231366, 989.680523087659, 
            990.781349841785, 1005.75606421591, 997.035360314707,994.928511836986, 
            996.012597263487, 994.249532642942, 1005.95070720716, 988.48958339183, 
            997.279197474874, 993.176463294942, 1007.25434521494, 1003.7052774576, 
            999.078187434345, 1004.91757624002, 1008.88416631965,1007.04637128748, 
            1003.12247450407, 1015.17660495696, 1001.97021341098,997.376118405193, 
            992.973077430376, 994.981878550185, 1003.15585020888,1007.42839140189, 
            1008.82349023285, 1009.50815752809, 998.249912313893,996.130572881505, 
            998.1908524304, 1004.27183684979, 1000.25755061327, 999.546581914197, 
            999.586563241259, 987.790001893712, 999.151672336352,1001.78375990166, 
            999.719712907908, 1006.48174402213, 1003.59120454781, 998.954072595588, 
            1008.72600016238, 1006.72562410066, 1006.89970982311,1005.21317048907, 
            1003.9871376226, 1007.50194110916, 995.716626103823,994.981330040876, 
            1000.95853471046, 1000.34298358716, 1004.89210023244,997.67098695885, 
            999.613667330844, 992.356606521014, 997.889751087205, 999.572103909505, 
            1011.73124586553, 1000.92379332441, 998.47164223236,993.927783786705, 
            990.932274895557, 995.044718031122, 995.990214970003,996.671678002519, 
            1015.14438283045, 997.18825887884, 991.291688264167,1012.20878626223, 
            997.791666085149, 999.366182253824, 1000.46381043618,993.705371491957, 
            996.740285547083, 1003.4906422465, 1005.34156334679,997.604304813648) 
sample_ts <- ts(sample, start=c(2016,1),end =c(2019,52), frequency = 52)  


x <- window(sample_ts, end=c(2018, 52) + (4-1)/52) # The error occurs only if you use 4,8 or 12. For all other numbers the whole code runs perfectly
refit <- auto.arima (x,
                       ic = c("bic"),
                       stepwise = FALSE,
                       approximation = TRUE,
                       lambda="auto",
                       biasadj = TRUE)
arima_mat <- forecast(refit, h=1, 
                            lambda=refit$lambda)$mean #It works for h=2 but not for h=1; h=1 is needed because this is a part of several different loops
  

I keep receiving the following error message:

Error in NextMethod(.Generic) : 
  cannot assign 'tsp' to a vector of length 0
Additional: Warning message:
In .cbind.ts(list(e1, e2)), c(deparse(substitute(e1))[1L], deparse(substitute(e2))[1L]), :
  non-intersecting series 

I tried to run the loop row by row. The only row where the error occurs is line 4,8,12 (i=4,8,12). The other lines work perfectly. Do you have any idea why that is and how can I solve the error?

Thank you in advance. I am really desperate!

I predict that for all

i%%2 == 0

the identical error will occur, except for i==2

I receive the error for i=4,8,12 but not for the rest
Do you have any idea how to solve this problem?

What libraries are you using?

I work with:

library(pacman, readxl, ggplot2, dplyr, tidyverse, forecast, 
       tsfeatures, smooth, car, psych, astsa, writexl, Hmisc,
       tsoutliers, leaps, fabletools, FactoMineR, TSA)

I as well tried forecast::forecast, fabletools::forecast, smooth::forecast but none of these worked in my case and I received the same error for all of them.

1 Like

I cannot reproduce the error with the code provided because it throws a different error. See the function signature for window: the object ts, shown at the end of the reprex is not itself an object of class ts, which window requires, it is an object of class function.

# choose a package
suppressPackageStartupMessages({
  library(fpp2)
})

# sample data; use name that does not conflict with built-in

samp <- ts(rnorm(100, mean = 1000, sd = 7), start = c(2015, 1), end = c(2019, 52), frequency = 52)

# confirm it is a time object of doubles

head(samp)
#> Time Series:
#> Start = c(2015, 1) 
#> End = c(2015, 6) 
#> Frequency = 52 
#> [1]  993.0705  989.1815 1015.0940  999.2947 1003.0209 1004.7411


# Define h --> One-step ahead (for a start, later to be increased)
# h is never incremented; the only place it appears at the end of
# the loop when it is set equal to itself

h <- 1

# specify length to forecast

# avoid leading zeros except for formating strings

test <- window(samp, start = c(2018, 1), end = c(2019, 52), frequency = 52)

# inspect

head(test)
#> Time Series:
#> Start = c(2018, 1) 
#> End = c(2018, 6) 
#> Frequency = 52 
#> [1] 1001.2984 1005.5698  998.8478 1003.0511  985.5226  997.8527

# -1 + 1 == 0 adds nothing to length(test)

n <- length(test) - h + 1

# evaluate

n
#> [1] 104

# Create Matrix  good practice to preallocate outside loop

arima_mat <- matrix(0, nrow = n, ncol = h)

# inspect

head(arima_mat)
#>      [,1]
#> [1,]    0
#> [2,]    0
#> [3,]    0
#> [4,]    0
#> [5,]    0
#> [6,]    0

# Loop THIS IS RUN FOR 104 ITERATIONS, EQUAL TO length(test)
# SO MAKE EXPLICIT

# isolate for testing

for (i in 1:length(test)) x <- window(ts, end = c(2018, 52) + (i - 1) / 52)
#> Error in attr(x, "tsp") <- c(1, NROW(x), 1): object is not a matrix

ts
#> function (data = NA, start = 1, end = numeric(), frequency = 1, 
#>     deltat = 1, ts.eps = getOption("ts.eps"), class = if (nseries > 
#>         1) c("mts", "ts", "matrix") else "ts", names = if (!is.null(dimnames(data))) colnames(data) else paste("Series", 
#>         seq(nseries))) 
#> {
#>     if (is.data.frame(data)) 
#>         data <- data.matrix(data)
#>     if (is.matrix(data)) {
#>         nseries <- ncol(data)
#>         ndata <- nrow(data)
#>         dimnames(data) <- list(NULL, names)
#>     }
#>     else {
#>         nseries <- 1
#>         ndata <- length(data)
#>     }
#>     if (ndata == 0) 
#>         stop("'ts' object must have one or more observations")
#>     if (missing(frequency)) 
#>         frequency <- 1/deltat
#>     else if (missing(deltat)) 
#>         deltat <- 1/frequency
#>     if (frequency > 1 && 0 < (d <- abs(frequency - round(frequency))) && 
#>         d < ts.eps) 
#>         frequency <- round(frequency)
#>     if (!missing(start)) 
#>         start <- as.numeric(start)
#>     if (length(start) > 1L) {
#>         start <- start[1L] + (start[2L] - 1)/frequency
#>     }
#>     if (!missing(end)) 
#>         end <- as.numeric(end)
#>     if (length(end) > 1L) {
#>         end <- end[1L] + (end[2L] - 1)/frequency
#>     }
#>     if (missing(end)) 
#>         end <- start + (ndata - 1)/frequency
#>     else if (missing(start)) 
#>         start <- end - (ndata - 1)/frequency
#>     if (start > end) 
#>         stop("'start' cannot be after 'end'")
#>     cycles <- as.numeric((end - start) * frequency)
#>     if (abs(round(cycles) - cycles) > ts.eps * max(cycles, 1)) 
#>         stop("'end' must be a whole number of cycles after 'start'")
#>     nobs <- floor(cycles + 1.01)
#>     if (nobs != ndata) 
#>         data <- if (NCOL(data) == 1) {
#>             if (ndata < nobs) 
#>                 rep_len(data, nobs)
#>             else if (ndata > nobs) 
#>                 data[1L:nobs]
#>         }
#>         else {
#>             if (ndata < nobs) 
#>                 data[rep_len(1L:ndata, nobs), ]
#>             else if (ndata > nobs) 
#>                 data[1L:nobs, ]
#>         }
#>     attr(data, "tsp") <- c(start, end, frequency)
#>     if (!is.null(class) && class[[1]] != "none") 
#>         attr(data, "class") <- class
#>     data
#> }
#> <bytecode: 0x55de7495c7f0>
#> <environment: namespace:stats>
# see discussion in text

Created on 2020-11-03 by the reprex package (v0.3.0.9001)

I corrected the code in my first post. If I now run the loop seperately and insert for example i=4 I keep receiving the error message. I aswell tested all your suggestions and got the same results. So I think that my ts is fine. I therefore changed the test length. Sorry for the inconvenience.

nothing will work without fixing this. ts is a function not a time series object

oh that is what you meant. I am sorry. It is suppose to be sampleinstead of ts.

Then to advance the discussion, post a complete reprex that will produce the identical error. Start from a fresh R session to avoid contamination of namespace.

I just fixed the code. The code which gives me the following error:

Error in NextMethod(.Generic) : 
  cannot assign 'tsp' to a vector of length 0
Additional: Warning message:
In .cbind.ts(list(e1, e2)), c(deparse(substitute(e1))[1L], deparse(substitute(e2))[1L]), :
  non-intersecting series

is

library(forecast)
# sample data
sample <- ts(rnorm(100, mean = 1000, sd=7), start = c(2015,1), end = c(2019,52), frequency = 52)

head(sample)
#Time Series:
#Start = c(2015, 1) 
#End = c(2015, 6) 
#Frequency = 52 
#[1]  997.6134  990.8194  999.8653 1005.6171  992.8228  988.5997

#Define h --> One-step ahead (for a start, later to be increased)
h <- 1
#specify length to forecast
test  <- window(sample, start = c(2019,01), end = c(2019,52), frequency = 52)
head(test)
#Time Series:
#Start = c(2019, 1) 
#End = c(2019, 6) 
#Frequency = 52 
#[1]  994.1473 1008.5955 1001.2610 1008.6236 1004.6126  995.7376

n <- length(test) 
# n=52
# Create Matrix
arima_mat <- matrix(0, nrow=n, ncol=h)
#     [,1]
#[1,]    0
#[2,]    0
#[3,]    0
#[4,]    0
#[5,]    0

#Loop
for(i in 1:n)
{  
  x <- window(sample, end=c(2018, 52) + (i-1)/52)
  refit <- auto.arima (x,
                       ic = c("bic"),
                       stepwise = FALSE,
                       approximation = TRUE,
                       lambda="auto",
                       biasadj = TRUE)
  arima_mat[i,] <- forecast(refit, h=h,
                            lambda=refit$lambda
  )$mean
}

I receive the error still, even if I run the loop separately inserting i=4,8,12
Hope this gives you the same error now

Look. This is still not a reprex, which should be cut and paste to run. It is unreasonably difficult to retrace a problem if it is necessary to edit the example code before running it. Perhaps someone else may take up the question.

I added the library which is necessary above, please have a look at it again cause it seems like no one else is able to help me atm.

Help me to help you. One post in the reprex cut and paste form, following these instructions. Don't update an earlier post. Cut-and-paste the reprex into your post.

I hope this works now. My reprex:

``` r
library(forecast)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
sample <- c(997.791666085149, 999.366182253824, 1000.46381043618, 993.705371491957, 
            996.740285547083, 1003.4906422465, 1005.34156334679, 997.604304813648, 
            1009.6643663447, 993.128043433675, 1005.53114457311, 1004.714338528, 
            1017.72437048901, 1007.94598223837, 988.226025380339, 994.785531675548, 
            1011.97940179699, 998.636119804733, 994.062327383755, 993.979060157697, 
            1003.93555180002, 1001.70926154818, 997.933817810478, 979.693838855987, 
            992.823376865983, 997.838580296017, 1006.21213228311, 991.977377944057, 
            1003.77529414578, 1002.67024480564, 989.082946231366, 989.680523087659, 
            990.781349841785, 1005.75606421591, 997.035360314707, 994.928511836986, 
            996.012597263487, 994.249532642942, 1005.95070720716, 988.48958339183, 
            997.279197474874, 993.176463294942, 1007.25434521494, 1003.7052774576, 
            999.078187434345, 1004.91757624002, 1008.88416631965, 1007.04637128748, 
            1003.12247450407, 1015.17660495696, 1001.97021341098, 997.376118405193, 
            992.973077430376, 994.981878550185, 1003.15585020888, 1007.42839140189, 
            1008.82349023285, 1009.50815752809, 998.249912313893, 996.130572881505, 
            998.1908524304, 1004.27183684979, 1000.25755061327, 999.546581914197, 
            999.586563241259, 987.790001893712, 999.151672336352, 1001.78375990166, 
            999.719712907908, 1006.48174402213, 1003.59120454781, 998.954072595588, 
            1008.72600016238, 1006.72562410066, 1006.89970982311, 1005.21317048907, 
            1003.9871376226, 1007.50194110916, 995.716626103823, 994.981330040876, 
            1000.95853471046, 1000.34298358716, 1004.89210023244, 997.67098695885, 
            999.613667330844, 992.356606521014, 997.889751087205,999.572103909505, 
            1011.73124586553, 1000.92379332441, 998.47164223236,993.927783786705, 
            990.932274895557, 995.044718031122, 995.990214970003, 996.671678002519, 
            1015.14438283045, 997.18825887884, 991.291688264167,1012.20878626223, 
            997.791666085149, 999.366182253824, 1000.46381043618,993.705371491957, 
            996.740285547083, 1003.4906422465, 1005.34156334679, 997.604304813648, 
            1009.6643663447, 993.128043433675, 1005.53114457311,1004.714338528, 
            1017.72437048901, 1007.94598223837, 988.226025380339, 994.785531675548, 
            1011.97940179699, 998.636119804733, 994.062327383755, 993.979060157697, 
            1003.93555180002, 1001.70926154818, 997.933817810478,979.693838855987, 
            992.823376865983, 997.838580296017, 1006.21213228311, 991.977377944057, 
            1003.77529414578, 1002.67024480564, 989.082946231366, 989.680523087659, 
            990.781349841785, 1005.75606421591, 997.035360314707,994.928511836986, 
            996.012597263487, 994.249532642942, 1005.95070720716, 988.48958339183, 
            997.279197474874, 993.176463294942, 1007.25434521494, 1003.7052774576, 
            999.078187434345, 1004.91757624002, 1008.88416631965,1007.04637128748, 
            1003.12247450407, 1015.17660495696, 1001.97021341098,997.376118405193, 
            992.973077430376, 994.981878550185, 1003.15585020888,1007.42839140189, 
            1008.82349023285, 1009.50815752809, 998.249912313893,996.130572881505, 
            998.1908524304, 1004.27183684979, 1000.25755061327, 999.546581914197, 
            999.586563241259, 987.790001893712, 999.151672336352,1001.78375990166, 
            999.719712907908, 1006.48174402213, 1003.59120454781, 998.954072595588, 
            1008.72600016238, 1006.72562410066, 1006.89970982311,1005.21317048907, 
            1003.9871376226, 1007.50194110916, 995.716626103823,994.981330040876, 
            1000.95853471046, 1000.34298358716, 1004.89210023244,997.67098695885, 
            999.613667330844, 992.356606521014, 997.889751087205, 999.572103909505, 
            1011.73124586553, 1000.92379332441, 998.47164223236,993.927783786705, 
            990.932274895557, 995.044718031122, 995.990214970003,996.671678002519, 
            1015.14438283045, 997.18825887884, 991.291688264167,1012.20878626223, 
            997.791666085149, 999.366182253824, 1000.46381043618,993.705371491957, 
            996.740285547083, 1003.4906422465, 1005.34156334679,997.604304813648) 
sample_ts <- ts(sample, start=c(2016,1),end =c(2019,52), frequency = 52)  


x <- window(sample_ts, end=c(2018, 52) + (4-1)/52) # The error occurs only if you use 4,8 or 12. For all other numbers the whole code runs perfectly
refit <- auto.arima (x,
                       ic = c("bic"),
                       stepwise = FALSE,
                       approximation = TRUE,
                       lambda="auto",
                       biasadj = TRUE)
arima_mat <- forecast(refit, h=1, 
                            lambda=refit$lambda)$mean #It works for h=2 but not for h=1; h=1 is needed because this is a part of several different loops
#> Warning in .cbind.ts(list(e1, e2), c(deparse(substitute(e1))[1L],
#> deparse(substitute(e2))[1L]), : non-intersecting series
#> Error in NextMethod(.Generic): kann 'tsp' nicht einem Vektor der Länge 0 zuordnen

Created on 2020-11-05 by the reprex package (v0.3.0)

Translation of the error:

#> Error in NextMethod(.Generic): cannot assign 'tsp' to a vector of length 0
1 Like

Thanks for working with me. I've posted an edited and heavily commented reprex based on yours. I can reproduce the error. I have an idea what causes it, but have found no fix for the h = 1 argument. The tedious details follow.

Let's approach this from a more abstract stance: f(x) = y. In R terms these are three objects, one of which is of class function.

x is what is at hand: my_example_ts
y is what is desired: this is unclear because we are stuck on the error
f is the function (which may be a combination of functions) that turns x into y

An intermediate object seems to be something that can be used as a beginning index for a loop.

In my experience, however, questions about what to do are much more fruitful than how to do. In this case, what is y? What are you trying to change my_sample_ts, the original time series into?

#### practice tip--always start testing in a new session
#### after saving code (preferably into a version control
#### system). This avoids having namespace confusion

#### code reformatted for readability
#### using styler package and start-up 
#### messages omitted; this  is a matter
#### of style, but consistency in coding
#### is recommended for ease of debugging
 
suppressPackageStartupMessages({
  library(forecast)
  library(dplyr) #### added for debugging
  library(lubridate) #### added for debugging
  library(purrr) #### added for debugging
  })

##### DO NOT USE BUILT-IN FUNCTION NAMES
##### my_sample, df, data and others may be treated as closures
##### in some operations; not a problem here but it is something
##### to avoid

my_sample <- c(
997.791666085149, 999.366182253824, 1000.46381043618, 993.705371491957,
996.740285547083, 1003.4906422465, 1005.34156334679, 997.604304813648,
1009.6643663447, 993.128043433675, 1005.53114457311, 1004.714338528,
1017.72437048901, 1007.94598223837, 988.226025380339, 994.785531675548,
1011.97940179699, 998.636119804733, 994.062327383755, 993.979060157697,
1003.93555180002, 1001.70926154818, 997.933817810478, 979.693838855987,
992.823376865983, 997.838580296017, 1006.21213228311, 991.977377944057,
1003.77529414578, 1002.67024480564, 989.082946231366, 989.680523087659,
990.781349841785, 1005.75606421591, 997.035360314707, 994.928511836986,
996.012597263487, 994.249532642942, 1005.95070720716, 988.48958339183,
997.279197474874, 993.176463294942, 1007.25434521494, 1003.7052774576,
999.078187434345, 1004.91757624002, 1008.88416631965, 1007.04637128748,
1003.12247450407, 1015.17660495696, 1001.97021341098, 997.376118405193,
992.973077430376, 994.981878550185, 1003.15585020888, 1007.42839140189,
1008.82349023285, 1009.50815752809, 998.249912313893, 996.130572881505,
998.1908524304, 1004.27183684979, 1000.25755061327, 999.546581914197,
999.586563241259, 987.790001893712, 999.151672336352, 1001.78375990166,
999.719712907908, 1006.48174402213, 1003.59120454781, 998.954072595588,
1008.72600016238, 1006.72562410066, 1006.89970982311, 1005.21317048907,
1003.9871376226, 1007.50194110916, 995.716626103823, 994.981330040876,
1000.95853471046, 1000.34298358716, 1004.89210023244, 997.67098695885,
999.613667330844, 992.356606521014, 997.889751087205, 999.572103909505,
1011.73124586553, 1000.92379332441, 998.47164223236, 993.927783786705,
990.932274895557, 995.044718031122, 995.990214970003, 996.671678002519,
1015.14438283045, 997.18825887884, 991.291688264167, 1012.20878626223,
997.791666085149, 999.366182253824, 1000.46381043618, 993.705371491957,
996.740285547083, 1003.4906422465, 1005.34156334679, 997.604304813648,
1009.6643663447, 993.128043433675, 1005.53114457311, 1004.714338528,
1017.72437048901, 1007.94598223837, 988.226025380339, 994.785531675548,
1011.97940179699, 998.636119804733, 994.062327383755, 993.979060157697,
1003.93555180002, 1001.70926154818, 997.933817810478, 979.693838855987,
992.823376865983, 997.838580296017, 1006.21213228311, 991.977377944057,
1003.77529414578, 1002.67024480564, 989.082946231366, 989.680523087659,
990.781349841785, 1005.75606421591, 997.035360314707, 994.928511836986,
996.012597263487, 994.249532642942, 1005.95070720716, 988.48958339183,
997.279197474874, 993.176463294942, 1007.25434521494, 1003.7052774576,
999.078187434345, 1004.91757624002, 1008.88416631965, 1007.04637128748,
1003.12247450407, 1015.17660495696, 1001.97021341098, 997.376118405193,
992.973077430376, 994.981878550185, 1003.15585020888, 1007.42839140189,
1008.82349023285, 1009.50815752809, 998.249912313893, 996.130572881505,
998.1908524304, 1004.27183684979, 1000.25755061327, 999.546581914197,
999.586563241259, 987.790001893712, 999.151672336352, 1001.78375990166,
999.719712907908, 1006.48174402213, 1003.59120454781, 998.954072595588,
1008.72600016238, 1006.72562410066, 1006.89970982311, 1005.21317048907,
1003.9871376226, 1007.50194110916, 995.716626103823, 994.981330040876,
1000.95853471046, 1000.34298358716, 1004.89210023244, 997.67098695885,
999.613667330844, 992.356606521014, 997.889751087205, 999.572103909505,
1011.73124586553, 1000.92379332441, 998.47164223236, 993.927783786705,
990.932274895557, 995.044718031122, 995.990214970003, 996.671678002519,
1015.14438283045, 997.18825887884, 991.291688264167, 1012.20878626223,
997.791666085149, 999.366182253824, 1000.46381043618, 993.705371491957,
996.740285547083, 1003.4906422465, 1005.34156334679, 997.604304813648
)

#### practice tip: when debugging inspect the output of each step

str(my_sample)
#>  num [1:208] 998 999 1000 994 997 ...

#### this confirms we have a numeric vector
#### help("ts") shows that a numeric vector is a permitted argument

##### frequency is the unit of observation; 52 in this case signifies
##### 52 observations per day
##### that is slightly more frequent than every half-hour
##### is that what you really want?

my_sample_ts <- ts(my_sample, start = c(2016, 1), end = c(2019, 52), frequency = 52)

#### inspect to see if it is as intended 
head(my_sample_ts)
#> Time Series:
#> Start = c(2016, 1) 
#> End = c(2016, 6) 
#> Frequency = 52 
#> [1]  997.7917  999.3662 1000.4638  993.7054  996.7403 1003.4906
length(my_sample_ts)
#> [1] 208

##### style suggestion: reserve the names x and y for 
##### argument names within functions, as they are often
##### used for plotting, for example

##### begin debugging
##### practice tip: comment in function signature

#############################################################
    # help("window")
    # ## S3 replacement method for class 'ts'
    # window(x, start, end, frequency, deltat, ...) <- value
    # 
    # Arguments
    # x a time-series (or other object if not replacing values).
    # 
    # start the start time of the period of interest.
    # 
    # end   the end time of the period of interest.
    # 
    # frequency, deltat the new frequency can be specified by either (or both if they are consistent).
    
#### when working in RStudio, keep the help page open, alternatively
#### and, in any event, check the details and value sections

my_x <- window(my_sample_ts, end = c(2018, 52) + (4 - 1) / 52) 

#### check output

head(my_x)
#> Time Series:
#> Start = c(2016, 1) 
#> End = c(2016, 6) 
#> Frequency = 52 
#> [1]  997.7917  999.3662 1000.4638  993.7054  996.7403 1003.4906
length(my_x)
#> [1] 159

#### this returns a time series object starting c(2016,1) and
#### ending c(2019,3)
#### does this make sense?
#### the starting point is what date?
#### is it consistent with the frequency?
#### evaluate each part; use a function since
#### we'll be doing this again for testing

test_fun <- function(x) {
  (x - 1) / 52
}

test_fun(4)
#> [1] 0.05769231

#### what impact does a number this small have?
#### what impact is it intended to have

#### how this expression changes as the first number varies

seq(1:53) %>% map(., test_fun) %>% unlist() -> test_out
test_out
#>  [1] 0.00000000 0.01923077 0.03846154 0.05769231 0.07692308 0.09615385
#>  [7] 0.11538462 0.13461538 0.15384615 0.17307692 0.19230769 0.21153846
#> [13] 0.23076923 0.25000000 0.26923077 0.28846154 0.30769231 0.32692308
#> [19] 0.34615385 0.36538462 0.38461538 0.40384615 0.42307692 0.44230769
#> [25] 0.46153846 0.48076923 0.50000000 0.51923077 0.53846154 0.55769231
#> [31] 0.57692308 0.59615385 0.61538462 0.63461538 0.65384615 0.67307692
#> [37] 0.69230769 0.71153846 0.73076923 0.75000000 0.76923077 0.78846154
#> [43] 0.80769231 0.82692308 0.84615385 0.86538462 0.88461538 0.90384615
#> [49] 0.92307692 0.94230769 0.96153846 0.98076923 1.00000000

#### visualize

plot(seq(1:53), test_out)

#### assessment
#### the expression linearly increases to 1 in the range 1:53
#### with the range 1:521, the expression maximizes to 10

##### IDENTIFICATION OF THE ERROR
##### CAUTION: GETTING THE QUESTION RIGHT MAY 
##### MAKE THE ERROR IRRELEVANT

# The error occurs only if you use 4,8 or 12. For all other numbers the whole code runs perfectly

#### practice suggestion: convert to a function; when more 
#### options are to be tried, can be modified--for ic, for
#### example; minimizes syntax errors once working

#### Note the function signature in help
#### there are many more than used here
#### probably not helpful to cut-and-paste
#### here
#### CRUCIAL: auto.arima takes a univariate time series
#### as a mandatory argument

refit <- function(x) {
    auto.arima(x,
    ic = c("bic"),
    stepwise = FALSE,
    approximation = TRUE,
    lambda = "auto",
    biasadj = TRUE
  )
}

#### note: this is long running

refitted <- refit(my_x) 

#### inspect

class(refitted)
#> [1] "forecast_ARIMA" "ARIMA"          "Arima"

#### signature check
#### a time series or ***time series model*** for which forecasts are required

#### another function suggestion

mk_arima <- function(x,y) {
  forecast(x,
  h = y,
  lambda = x$lambda)
}

### testing with "forecast_ARIMA" "ARIMA" "Arima" object
### and an argument h = 1
 
mk_arima(refitted,1)$mean
#> Warning in .cbind.ts(list(e1, e2), c(deparse(substitute(e1))[1L],
#> deparse(substitute(e2))[1L]), : non-intersecting series
#> Error in NextMethod(.Generic): cannot assign 'tsp' to zero-length vector

  ### SUCCESS! error for h=1 reproduced

### retest with h=2

mk_arima(refitted,2)$mean -> a

### SUCCESS! no error
### QUESTION: is a time series of two values desired?

### retest with another odd number

mk_arima(refitted,3)$mean
#> Time Series:
#> Start = c(2019, 4) 
#> End = c(2019, 6) 
#> Frequency = 52 
#> [1]  999.4212 1000.1001 1000.1001

### QUESTION: is there a reason to discard the confidence intervals?

mk_arima(refitted,3)
#>          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
#> 2019.058       999.4212 990.6408 1008.171 985.9486 1012.761
#> 2019.077      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.096      1000.1001 991.1562 1009.013 986.3757 1013.686

### test for next 10 values of h

mk_arima(refitted,4)
#>          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
#> 2019.058       999.4212 990.6408 1008.171 985.9486 1012.761
#> 2019.077      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.096      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.115      1000.1001 991.1562 1009.013 986.3757 1013.686
mk_arima(refitted,5)
#>          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
#> 2019.058       999.4212 990.6408 1008.171 985.9486 1012.761
#> 2019.077      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.096      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.115      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.135      1000.1001 991.1562 1009.013 986.3757 1013.686
mk_arima(refitted,6)
#>          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
#> 2019.058       999.4212 990.6408 1008.171 985.9486 1012.761
#> 2019.077      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.096      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.115      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.135      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.154      1000.1001 991.1562 1009.013 986.3757 1013.686
mk_arima(refitted,7)
#>          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
#> 2019.058       999.4212 990.6408 1008.171 985.9486 1012.761
#> 2019.077      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.096      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.115      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.135      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.154      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.173      1000.1001 991.1562 1009.013 986.3757 1013.686
mk_arima(refitted,8)
#>          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
#> 2019.058       999.4212 990.6408 1008.171 985.9486 1012.761
#> 2019.077      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.096      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.115      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.135      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.154      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.173      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.192      1000.1001 991.1562 1009.013 986.3757 1013.686
mk_arima(refitted,9)
#>          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
#> 2019.058       999.4212 990.6408 1008.171 985.9486 1012.761
#> 2019.077      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.096      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.115      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.135      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.154      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.173      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.192      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.212      1000.1001 991.1562 1009.013 986.3757 1013.686
mk_arima(refitted,10)
#>          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
#> 2019.058       999.4212 990.6408 1008.171 985.9486 1012.761
#> 2019.077      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.096      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.115      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.135      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.154      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.173      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.192      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.212      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.231      1000.1001 991.1562 1009.013 986.3757 1013.686
mk_arima(refitted,11)
#>          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
#> 2019.058       999.4212 990.6408 1008.171 985.9486 1012.761
#> 2019.077      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.096      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.115      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.135      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.154      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.173      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.192      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.212      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.231      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.250      1000.1001 991.1562 1009.013 986.3757 1013.686
mk_arima(refitted,12)
#>          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
#> 2019.058       999.4212 990.6408 1008.171 985.9486 1012.761
#> 2019.077      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.096      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.115      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.135      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.154      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.173      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.192      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.212      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.231      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.250      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.269      1000.1001 991.1562 1009.013 986.3757 1013.686
mk_arima(refitted,13)
#>          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
#> 2019.058       999.4212 990.6408 1008.171 985.9486 1012.761
#> 2019.077      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.096      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.115      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.135      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.154      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.173      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.192      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.212      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.231      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.250      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.269      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.288      1000.1001 991.1562 1009.013 986.3757 1013.686

#### No errors

#### Why does h = 1 this row the error?

#### begin with the question: what role does this argument play
#### in the forecast function?
#### "Number of periods for forecasting"
#### Let's use an example from help("forecast")

fit <- ets(window(WWWusage, end=60))
forecast(WWWusage, model=fit)
#> Model is being refit with current smoothing parameters but initial states are being re-estimated.
#> Set 'use.initial.values=TRUE' if you want to re-use existing initial values.
#>     Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
#> 101       218.3960 213.9345 222.8575 211.5727 225.2193
#> 102       217.1137 207.9338 226.2936 203.0743 231.1532
#> 103       216.0879 201.8561 230.3197 194.3223 237.8536
#> 104       215.2673 195.8853 234.6493 185.6251 244.9095
#> 105       214.6107 190.1127 239.1088 177.1442 252.0773
#> 106       214.0855 184.5805 243.5905 168.9615 259.2095
#> 107       213.6654 179.3037 248.0271 161.1137 266.2170
#> 108       213.3292 174.2817 252.3767 153.6112 273.0472
#> 109       213.0603 169.5059 256.6147 146.4496 279.6710
#> 110       212.8452 164.9630 260.7274 139.6157 286.0746

### Observation: h is not a mandatory argument
### What happens if h = 1 is added to the example?

forecast(WWWusage, model=fit, h = 1)
#> Model is being refit with current smoothing parameters but initial states are being re-estimated.
#> Set 'use.initial.values=TRUE' if you want to re-use existing initial values.
#>     Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
#> 101        218.396 213.9345 222.8575 211.5727 225.2193
class(WWWusage)
#> [1] "ts"
class(fit)
#> [1] "ets"

### No error
### Conclusion the argument h =1, by itself, does not throw an
### error when used in conjunction with a ts and ets object
### as the sole additional, non-default arguments

#### Let's set aside the possibility that an object of class
#### "forecast_ARIMA" "ARIMA" "Arima" makes a difference

# It works for h=2 but not for h=1; h=1 is needed because this is a part of several different loops
# > Warning in .cbind.ts(list(e1, e2), c(deparse(substitute(e1))[1L],
# > deparse(substitute(e2))[1L]), : non-intersecting series
# > Error in NextMethod(.Generic): kann 'tsp' nicht einem Vektor der Länge 0 zuordnen

#### let's also set aside the warning and focus on the error
#### the function that is throwing the error is from within forecast
#### the function tsp which takes

    ### a vector or matrix or univariate or multivariate time-series

#### and returns

    ### a numeric vector of length 3 or NULL

#### let's run some tests 

#### using time series

tsp(my_sample_ts)
#> [1] 2016.000 2019.981   52.000
tsp(WWWusage)
#> [1]   1 100   1

#### using models

tsp(fit)
#> NULL
tsp(refitted)
#> NULL

#### using matrix

mdat <- matrix(c(1, 2, 3, 11, 12, 13),
  nrow = 2, ncol = 3, byrow = TRUE,
  dimnames = list(
    c("row1", "row2"),
    c("C.1", "C.2", "C.3")
  )
)

tsp(mdat)
#> NULL

#### using vector

tsp(c(seq(1:100)))
#> NULL

#### unclear: why matrix and vector return null

#### What is calling tsp?
#### the function mk_arima created here?
#### that is just a convenience wrapper over forecast

mk_arima <- function(x,y) {
  forecast(x,
           h = y,
           lambda = x$lambda)
}

### What happens if we take lambda out of the picture?

mk_arima2 <- function(x,y) forecast(x,h = y)

mk_arima2(refitted,1)
#> Warning in .cbind.ts(list(e1, e2), c(deparse(substitute(e1))[1L],
#> deparse(substitute(e2))[1L]), : non-intersecting series
#> Error in NextMethod(.Generic): cannot assign 'tsp' to zero-length vector
mk_arima2(refitted,2)
#>          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
#> 2019.058       999.4212 990.6408 1008.171 985.9486 1012.761
#> 2019.077      1000.1001 991.1562 1009.013 986.3757 1013.686

### no difference

### What happens if we dispense with h altogether?

forecast(refitted)
#>          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
#> 2019.058       999.4212 990.6408 1008.171 985.9486 1012.761
#> 2019.077      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.096      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.115      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.135      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.154      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.173      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.192      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.212      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.231      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.250      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.269      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.288      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.308      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.327      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.346      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.365      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.385      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.404      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.423      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.442      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.462      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.481      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.500      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.519      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.538      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.558      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.577      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.596      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.615      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.635      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.654      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.673      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.692      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.712      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.731      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.750      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.769      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.788      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.808      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.827      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.846      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.865      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.885      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.904      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.923      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.942      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.962      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2019.981      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.000      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.019      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.038      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.058      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.077      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.096      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.115      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.135      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.154      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.173      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.192      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.212      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.231      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.250      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.269      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.288      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.308      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.327      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.346      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.365      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.385      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.404      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.423      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.442      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.462      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.481      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.500      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.519      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.538      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.558      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.577      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.596      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.615      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.635      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.654      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.673      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.692      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.712      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.731      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.750      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.769      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.788      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.808      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.827      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.846      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.865      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.885      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.904      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.923      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.942      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.962      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2020.981      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2021.000      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2021.019      1000.1001 991.1562 1009.013 986.3757 1013.686
#> 2021.038      1000.1001 991.1562 1009.013 986.3757 1013.686

### the result is identical to h = 2, 3 ... in a series

### what dates are these?

the_dates <- c(2019.058,2019.077,2019.096,2019.115,2019.135,2019.154,2019.173,2019.192,2019.212,2019.231,2019.250,2019.269,2019.288,2019.308,2019.327,2019.346,2019.365,2019.385,2019.404,2019.423,2019.442,2019.462,2019.481,2019.500,2019.519,2019.538,2019.558,2019.577,2019.596,2019.615,2019.635,2019.654,2019.673,2019.692,2019.712,2019.731,2019.750,2019.769,2019.788,2019.808,2019.827,2019.846,2019.865,2019.885,2019.904,2019.923,2019.942,2019.962,2019.981,2020.000,2020.019,2020.038,2020.058,2020.077,2020.096,2020.115,2020.135,2020.154,2020.173,2020.192,2020.212,2020.231,2020.250,2020.269,2020.288,2020.308,2020.327,2020.346,2020.365,2020.385,2020.404,2020.423,2020.442,2020.462,2020.481,2020.500,2020.519,2020.538,2020.558,2020.577,2020.596,2020.615,2020.635,2020.654,2020.673,2020.692,2020.712,2020.731,2020.750,2020.769,2020.788,2020.808,2020.827,2020.846,2020.865,2020.885,2020.904,2020.923,2020.942,2020.962,2020.981,2021.000,2021.019,2021.038)

format(date_decimal(the_dates), "%Y-%m-%d")
#>   [1] "2019-01-22" "2019-01-29" "2019-02-05" "2019-02-11" "2019-02-19"
#>   [6] "2019-02-26" "2019-03-05" "2019-03-12" "2019-03-19" "2019-03-26"
#>  [11] "2019-04-02" "2019-04-09" "2019-04-16" "2019-04-23" "2019-04-30"
#>  [16] "2019-05-07" "2019-05-14" "2019-05-21" "2019-05-28" "2019-06-04"
#>  [21] "2019-06-11" "2019-06-18" "2019-06-25" "2019-07-02" "2019-07-09"
#>  [26] "2019-07-16" "2019-07-23" "2019-07-30" "2019-08-06" "2019-08-13"
#>  [31] "2019-08-20" "2019-08-27" "2019-09-03" "2019-09-10" "2019-09-17"
#>  [36] "2019-09-24" "2019-10-01" "2019-10-08" "2019-10-15" "2019-10-22"
#>  [41] "2019-10-29" "2019-11-05" "2019-11-12" "2019-11-20" "2019-11-26"
#>  [46] "2019-12-03" "2019-12-10" "2019-12-18" "2019-12-25" "2020-01-01"
#>  [51] "2020-01-07" "2020-01-14" "2020-01-22" "2020-01-29" "2020-02-05"
#>  [56] "2020-02-12" "2020-02-19" "2020-02-26" "2020-03-04" "2020-03-11"
#>  [61] "2020-03-18" "2020-03-25" "2020-04-01" "2020-04-08" "2020-04-15"
#>  [66] "2020-04-22" "2020-04-29" "2020-05-06" "2020-05-13" "2020-05-20"
#>  [71] "2020-05-27" "2020-06-03" "2020-06-10" "2020-06-18" "2020-06-25"
#>  [76] "2020-07-02" "2020-07-08" "2020-07-15" "2020-07-23" "2020-07-30"
#>  [81] "2020-08-06" "2020-08-13" "2020-08-20" "2020-08-27" "2020-09-03"
#>  [86] "2020-09-10" "2020-09-17" "2020-09-24" "2020-10-01" "2020-10-08"
#>  [91] "2020-10-15" "2020-10-22" "2020-10-29" "2020-11-05" "2020-11-12"
#>  [96] "2020-11-19" "2020-11-26" "2020-12-03" "2020-12-10" "2020-12-18"
#> [101] "2020-12-25" "2021-01-01" "2021-01-07" "2021-01-14"

#### What would be the date before 2019-058, 2019-01-22, a week earlier
#### BIG QUESTION: why can't that date be calculated?
#### It's being calculated from our variable refitted, but what part?

forecast(refitted$fitted)
#>          Point Forecast     Lo 80     Hi 80    Lo 95    Hi 95
#> 2019.058      1000.7522  999.3853 1002.1192 998.6617 1002.843
#> 2019.077       999.9741  998.6071 1001.3410 997.8835 1002.065
#> 2019.096      1001.2251  999.8582 1002.5921 999.1346 1003.316
#****************omitted to remain within space limits*********************
#> 2021.038      1000.4975  999.1305 1001.8644 998.4069 1002.588

#### NOTE: this displays the same FORM of output, but note that
#### Unlike forecast(refitted), which has identical values after
#### the first row, refitted$fitted has different values for each
#### row

#### another interesting twist

class(refitted$fitted)
#> [1] "ts"
forecast(refitted$fitted, h = 1)
#>          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
#> 2019.058       1000.752 999.3853 1002.119 998.6617 1002.843

class(my_sample_ts)
#> [1] "ts"
forecast(my_sample_ts, h = 1)
#>      Point Forecast    Lo 80    Hi 80    Lo 95   Hi 95
#> 2020       999.3768 992.0252 1006.728 988.1336 1010.62

#### time series CAN handle h = 1

#### splitting the question
#### 1. why does forecast choke with h = 1 on the non-ts object?
#### 2. why is it necessary to use a non-ts object?

Created on 2020-11-05 by the reprex package (v0.3.0.9001)

Thanks. The reprex helped me track down where the problem was arising in the forecast package. I've now fixed the github version of the package here: https://github.com/robjhyndman/forecast/commit/1bde4d60aadb83bf14087102f0555f4a1983865b

2 Likes

Success. kudos to @Fitch92 for surfacing the bug. @robjhyndman gets honorable mention, because this is what he always does—fix things right away.

#### test of remotes::install_github("robjhyndman/forecast")
suppressPackageStartupMessages({
  library(forecast)
  })


my_sample <- c(
997.791666085149, 999.366182253824, 1000.46381043618, 993.705371491957,
996.740285547083, 1003.4906422465, 1005.34156334679, 997.604304813648,
1009.6643663447, 993.128043433675, 1005.53114457311, 1004.714338528,
1017.72437048901, 1007.94598223837, 988.226025380339, 994.785531675548,
1011.97940179699, 998.636119804733, 994.062327383755, 993.979060157697,
1003.93555180002, 1001.70926154818, 997.933817810478, 979.693838855987,
992.823376865983, 997.838580296017, 1006.21213228311, 991.977377944057,
1003.77529414578, 1002.67024480564, 989.082946231366, 989.680523087659,
990.781349841785, 1005.75606421591, 997.035360314707, 994.928511836986,
996.012597263487, 994.249532642942, 1005.95070720716, 988.48958339183,
997.279197474874, 993.176463294942, 1007.25434521494, 1003.7052774576,
999.078187434345, 1004.91757624002, 1008.88416631965, 1007.04637128748,
1003.12247450407, 1015.17660495696, 1001.97021341098, 997.376118405193,
992.973077430376, 994.981878550185, 1003.15585020888, 1007.42839140189,
1008.82349023285, 1009.50815752809, 998.249912313893, 996.130572881505,
998.1908524304, 1004.27183684979, 1000.25755061327, 999.546581914197,
999.586563241259, 987.790001893712, 999.151672336352, 1001.78375990166,
999.719712907908, 1006.48174402213, 1003.59120454781, 998.954072595588,
1008.72600016238, 1006.72562410066, 1006.89970982311, 1005.21317048907,555180002, 1001.70926154818, 997.933817810478, 979.693838855987,
992.823376865983, 997.838580296017, 1006.21213228311, 991.977377944057,
1003.9871376226, 1007.50194110916, 995.716626103823, 994.981330040876,
1000.95853471046, 1000.34298358716, 1004.89210023244, 997.67098695885,
999.613667330844, 992.356606521014, 997.889751087205, 999.572103909505,555180002, 1001.70926154818, 997.933817810478, 979.693838855987,
992.823376865983, 997.838580296017, 1006.21213228311, 991.977377944057,
1011.73124586553, 1000.92379332441, 998.47164223236, 993.927783786705,
990.932274895557, 995.044718031122, 995.990214970003, 996.671678002519,
1015.14438283045, 997.18825887884, 991.291688264167, 1012.20878626223,
997.791666085149, 999.366182253824, 1000.46381043618, 993.705371491957,
996.740285547083, 1003.4906422465, 1005.34156334679, 997.604304813648,
1009.6643663447, 993.128043433675, 1005.53114457311, 1004.714338528,
1017.72437048901, 1007.94598223837, 988.226025380339, 994.785531675548,
1011.97940179699, 998.636119804733, 994.062327383755, 993.979060157697,
1003.93555180002, 1001.70926154818, 997.933817810478, 979.693838855987,
992.823376865983, 997.838580296017, 1006.21213228311, 991.977377944057,
1003.77529414578, 1002.67024480564, 989.082946231366, 989.680523087659,
990.781349841785, 1005.75606421591, 997.035360314707, 994.928511836986,
996.012597263487, 994.249532642942, 1005.95070720716, 988.48958339183,
997.279197474874, 993.176463294942, 1007.25434521494, 1003.7052774576,
999.078187434345, 1004.91757624002, 1008.88416631965, 1007.04637128748,
1003.12247450407, 1015.17660495696, 1001.97021341098, 997.376118405193,
992.973077430376, 994.981878550185, 1003.15585020888, 1007.42839140189,
1008.82349023285, 1009.50815752809, 998.249912313893, 996.130572881505,
998.1908524304, 1004.27183684979, 1000.25755061327, 999.546581914197,
999.586563241259, 987.790001893712, 999.151672336352, 1001.78375990166,
999.719712907908, 1006.48174402213, 1003.59120454781, 998.954072595588,
1008.72600016238, 1006.72562410066, 1006.89970982311, 1005.21317048907,
1003.9871376226, 1007.50194110916, 995.716626103823, 994.981330040876,
1000.95853471046, 1000.34298358716, 1004.89210023244, 997.67098695885,
999.613667330844, 992.356606521014, 997.889751087205, 999.572103909505,
1011.73124586553, 1000.92379332441, 998.47164223236, 993.927783786705,
990.932274895557, 995.044718031122, 995.990214970003, 996.671678002519,
1015.14438283045, 997.18825887884, 991.291688264167, 1012.20878626223,
997.791666085149, 999.366182253824, 1000.46381043618, 993.705371491957,
996.740285547083, 1003.4906422465, 1005.34156334679, 997.604304813648
)


my_sample_ts <- ts(my_sample, start = c(2016, 1), end = c(2019, 52), frequency = 52)

my_x <- window(my_sample_ts, end = c(2018, 52) + (4 - 1) / 52) 

refit <- function(x) {
    auto.arima(x,
    ic = c("bic"),
    stepwise = FALSE,
    approximation = TRUE,
    lambda = "auto",
    biasadj = TRUE
  )
}

refitted <- refit(my_x) 

mk_arima <- function(x,y) {
  forecast(x,
  h = y,
  lambda = x$lambda)
}

#### This is where the h=1 argument failed

mk_arima(refitted,1)$mean
#> Time Series:
#> Start = c(2019, 4) 
#> End = c(2019, 4) 
#> Frequency = 52 
#>      out 
#> 1044.236

#### It now succeeds

Created on 2020-11-05 by the reprex package (v0.3.0.9001)

1 Like

First of all, thank you for the change. But now I have problems using it in a loop. I keep receiving either NA or NaN in the matrix or sometimes (not in this example) I receive in my personal data values which are not being inverted after using Box.Cox.

install.packages("remotes")
#> package 'remotes' successfully unpacked and MD5 sums checked
#> 
#> The downloaded binary packages are in
#> Skipping install of 'forecast' from a github remote, the SHA1 (1734afe8) has not changed since last install.
#>   Use `force = TRUE` to force installation
suppressPackageStartupMessages({
  library(forecast)
})

sample <- c(997.791666085149, 999.366182253824, 1000.46381043618, 993.705371491957, 
            996.740285547083, 1003.4906422465, 1005.34156334679, 997.604304813648, 
            1009.6643663447, 993.128043433675, 1005.53114457311, 1004.714338528, 
            1017.72437048901, 1007.94598223837, 988.226025380339, 994.785531675548, 
            1011.97940179699, 998.636119804733, 994.062327383755, 993.979060157697, 
            1003.93555180002, 1001.70926154818, 997.933817810478, 979.693838855987, 
            992.823376865983, 997.838580296017, 1006.21213228311, 991.977377944057, 
            1003.77529414578, 1002.67024480564, 989.082946231366, 989.680523087659, 
            990.781349841785, 1005.75606421591, 997.035360314707, 994.928511836986, 
            996.012597263487, 994.249532642942, 1005.95070720716, 988.48958339183, 
            997.279197474874, 993.176463294942, 1007.25434521494, 1003.7052774576, 
            999.078187434345, 1004.91757624002, 1008.88416631965, 1007.04637128748, 
            1003.12247450407, 1015.17660495696, 1001.97021341098, 997.376118405193, 
            992.973077430376, 994.981878550185, 1003.15585020888, 1007.42839140189, 
            1008.82349023285, 1009.50815752809, 998.249912313893, 996.130572881505, 
            998.1908524304, 1004.27183684979, 1000.25755061327, 999.546581914197, 
            999.586563241259, 987.790001893712, 999.151672336352, 1001.78375990166, 
            999.719712907908, 1006.48174402213, 1003.59120454781, 998.954072595588, 
            1008.72600016238, 1006.72562410066, 1006.89970982311, 1005.21317048907, 
            1003.9871376226, 1007.50194110916, 995.716626103823, 994.981330040876, 
            1000.95853471046, 1000.34298358716, 1004.89210023244, 997.67098695885, 
            999.613667330844, 992.356606521014, 997.889751087205,999.572103909505, 
            1011.73124586553, 1000.92379332441, 998.47164223236,993.927783786705, 
            990.932274895557, 995.044718031122, 995.990214970003, 996.671678002519, 
            1015.14438283045, 997.18825887884, 991.291688264167,1012.20878626223, 
            997.791666085149, 999.366182253824, 1000.46381043618,993.705371491957, 
            996.740285547083, 1003.4906422465, 1005.34156334679, 997.604304813648, 
            1009.6643663447, 993.128043433675, 1005.53114457311,1004.714338528, 
            1017.72437048901, 1007.94598223837, 988.226025380339, 994.785531675548, 
            1011.97940179699, 998.636119804733, 994.062327383755, 993.979060157697, 
            1003.93555180002, 1001.70926154818, 997.933817810478,979.693838855987, 
            992.823376865983, 997.838580296017, 1006.21213228311, 991.977377944057, 
            1003.77529414578, 1002.67024480564, 989.082946231366, 989.680523087659, 
            990.781349841785, 1005.75606421591, 997.035360314707,994.928511836986, 
            996.012597263487, 994.249532642942, 1005.95070720716, 988.48958339183, 
            997.279197474874, 993.176463294942, 1007.25434521494, 1003.7052774576, 
            999.078187434345, 1004.91757624002, 1008.88416631965,1007.04637128748, 
            1003.12247450407, 1015.17660495696, 1001.97021341098,997.376118405193, 
            992.973077430376, 994.981878550185, 1003.15585020888,1007.42839140189, 
            1008.82349023285, 1009.50815752809, 998.249912313893,996.130572881505, 
            998.1908524304, 1004.27183684979, 1000.25755061327, 999.546581914197, 
            999.586563241259, 987.790001893712, 999.151672336352,1001.78375990166, 
            999.719712907908, 1006.48174402213, 1003.59120454781, 998.954072595588, 
            1008.72600016238, 1006.72562410066, 1006.89970982311,1005.21317048907, 
            1003.9871376226, 1007.50194110916, 995.716626103823,994.981330040876, 
            1000.95853471046, 1000.34298358716, 1004.89210023244,997.67098695885, 
            999.613667330844, 992.356606521014, 997.889751087205, 999.572103909505, 
            1011.73124586553, 1000.92379332441, 998.47164223236,993.927783786705, 
            990.932274895557, 995.044718031122, 995.990214970003,996.671678002519, 
            1015.14438283045, 997.18825887884, 991.291688264167,1012.20878626223, 
            997.791666085149, 999.366182253824, 1000.46381043618,993.705371491957, 
            996.740285547083, 1003.4906422465, 1005.34156334679,997.604304813648) 
sample_ts <- ts(sample, start=c(2016,1),end =c(2019,52), frequency = 52)  

arima_sample <- auto.arima (window(sample_ts, end=c(2018, 52)),
                     ic = c("bic"),
                     stepwise = FALSE,
                     approximation = TRUE,
                     lambda="auto",
                     biasadj = TRUE)

sample_mat <- matrix(0, nrow=52, ncol=1)
for(i in 1:52)
{  
  x <- window(sample_ts, end=c(2018, 52) + (i-1)/52)
  sample_refit <- Arima(x, model=arima_sample, lambda="auto", biasadj = TRUE)
  sample_mat[i,] <- forecast::forecast(sample_refit, h=1, lambda=sample_refit$lambda, biasadj=TRUE)$mean
}
sample_mat

#...
#[19,] 1001.1420
#[20,] 1002.8388
#[21,]        NA
#[22,] 1000.7413
#[23,] 1000.7112
#...

Created on 2020-11-07 by the reprex package (v0.3.0)

The NAs are caused by the automatic Box-Cox parameter giving weird results. In general, automatic choice of a Box-Cox transformation is not a good idea (which is why it is not the default in auto.arima()). You are much better off selecting any transformation you need manually, or not using one at all.

2 Likes