rtime-seriesfable-r

How to fix TSLM warning: argument must be coercible to non-negative integer


I am trying to find multiple seasonality in a stock price time series (GOOG). Converting the double closing price column to an integer complains also. There are no NA's and all of the numbers are positive. Ho do I get rid of this error?

Edit 1: Not using a pipe gets the same warning:

fitClose <- model(close,TSLM(Close ~ trend() + season())) # complains

Original code:

rm(list = ls())
require(fpp3)
goog<-gafa_stock|>filter(Symbol=="GOOG",year(Date)==2018)
close<-goog|>select(Close) # just get close
sum(is.na(close)) # no na's
min(close$Close) # min is positive
fitClose <- close |> model(TSLM(Close ~ trend() + season())) # complains
# Warning message:
# 1 error encountered for TSLM(Close ~ trend() + season())
# [1] argument must be coercible to non-negative integer
c<-close |> mutate(Close=as.integer(Close)) # convert to integer
#autoplot(c)
#close|>mutate(Close = Close / first(Close))
fit <- c |> model(TSLM(Close ~ trend() + season())) # still complains
report(fit)

Solution

  • The tsibble has an irregular time index, as you can see below with the "[!]"

    library(fpp3)
    
    gafa_stock
    #> # A tsibble: 5,032 x 8 [!]
    #> # Key:       Symbol [4]
    #>    Symbol Date        Open  High   Low Close Adj_Close    Volume
    #>    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>     <dbl>
    #>  1 AAPL   2014-01-02  79.4  79.6  78.9  79.0      67.0  58671200
    #>  2 AAPL   2014-01-03  79.0  79.1  77.2  77.3      65.5  98116900
    #>  3 AAPL   2014-01-06  76.8  78.1  76.2  77.7      65.9 103152700
    #>  4 AAPL   2014-01-07  77.8  78.0  76.8  77.1      65.4  79302300
    #>  5 AAPL   2014-01-08  77.0  77.9  77.0  77.6      65.8  64632400
    #>  6 AAPL   2014-01-09  78.1  78.1  76.5  76.6      65.0  69787200
    #>  7 AAPL   2014-01-10  77.1  77.3  75.9  76.1      64.5  76244000
    #>  8 AAPL   2014-01-13  75.7  77.5  75.7  76.5      64.9  94623200
    #>  9 AAPL   2014-01-14  76.9  78.1  76.8  78.1      66.1  83140400
    #> 10 AAPL   2014-01-15  79.1  80.0  78.8  79.6      67.5  97909700
    #> # ℹ 5,022 more rows
    

    You can re-index it to be daily data (with missing values on non-trading days) using the as_tsibble() function with regular = TRUE:

    gafa_stock |> 
        filter(Symbol=="GOOG", year(Date)==2018) |> 
        as_tsibble(index = Date, regular = TRUE) |> 
        model(TSLM(Close ~ trend() + season())) |> 
        report()
    #> Series: Close 
    #> Model: TSLM 
    #> 
    #> Residuals:
    #>      Min       1Q   Median       3Q      Max 
    #> -136.531  -53.911   -7.886   54.088  151.789 
    #> 
    #> Coefficients:
    #>                 Estimate Std. Error t value Pr(>|t|)    
    #> (Intercept)   1113.25932   12.02183  92.603   <2e-16 ***
    #> trend()          0.01593    0.04114   0.387    0.699    
    #> season()week2   -2.82771   13.45490  -0.210    0.834    
    #> season()week3         NA         NA      NA       NA    
    #> season()week4         NA         NA      NA       NA    
    #> season()week5   -7.58925   13.67159  -0.555    0.579    
    #> season()week6   -2.88701   13.45333  -0.215    0.830    
    #> season()week7   -1.56729   13.52010  -0.116    0.908    
    #> ---
    #> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    #> 
    #> Residual standard error: 67.93 on 245 degrees of freedom
    #> Multiple R-squared: 0.001919,    Adjusted R-squared: -0.01845
    #> F-statistic: 0.0942 on 5 and 245 DF, p-value: 0.99306
    

    Created on 2023-06-12 with reprex v2.0.2

    season() here has automatically selected weekly seasonality, but due to the missing weekends, two of the seasonal components can't be estimated.

    In any case, there is no seasonality of any kind in stock prices in an efficient market.