1

I am currently working on a sample data that requires me to perform time series forecasting in R on a given set of data.So i need to forecast for daily basis. I am getting the following error message.

Error in -.default(x, trend) : non-numeric argument to binary operator

My data format

Items               Regions         vDate       QTY
Tractor TT35 4WD    Tiruchengode    2016-01-01  2
Tractor TT35 4WD    Tiruchengode    2016-01-02  7
Tractor TT35 4WD    Tiruchengode    2016-01-03  6
Tractor TT35 4WD    Tiruchengode    2016-01-04  0
Tractor TT35 4WD    Tiruchengode    2016-01-05  6
Tractor TT35 4WD    Tiruchengode    2016-01-06  6
Tractor TT35 4WD    Tiruchengode    2016-01-07  1
Tractor TT35 4WD    Tiruchengode    2016-01-08  6
Tractor TT35 4WD    Tiruchengode    2016-01-09  0
Tractor TT35 4WD    Tiruchengode    2016-01-10  4
Tractor TT35 4WD    Tiruchengode    2016-01-11  4
Tractor TT35 4WD    Tiruchengode    2016-01-12  0
Tractor TT35 4WD    Tiruchengode    2016-01-13  6
Tractor TT35 4WD    Tiruchengode    2016-01-14  7
Tractor TT35 4WD    Tiruchengode    2016-01-15  3

In items column i have three types , in regions column there are 18 regions , for each region i have three items and for each item i have two years of data (2016-01-01 to 2017-01-31), i need to forecast the QTY column for the next year(2018-01-31)

I am using the below code

   ts_temp = ts(dt_ts[Regions==i & Item==j,]$Data,frequency = 365,start = 
   c(2016,1,1))
   # plot(ts_temp)
   #tsss<-decompose(ts_temp)
   #plot(tsss)
    model_hw = HoltWinters(ts_temp) 

When i run model_hw iam getting the above error.

Any suggestions please,
Thanks in Advance

Eswaraiah
  • 13
  • 5
  • Can you share your data using `dput()`? You can put your data on site like https://pastebin.com/ then add the link to your question – Tung Mar 22 '18 at 06:57

1 Answers1

0

The following script gave me no problems:

library(readr)
dt_ts <- read_csv("~/test.csv")
attach(dt_ts)

i = 'Tiruchengode'
j = 'Tractor TT35 4WD'

ts_temp = ts(dt_ts[Regions==i & Items==j,]$QTY,frequency = 365,start = c(2016,1,1))

model_hw = HoltWinters(ts_temp) 

That error message means that HoltWinters() is trying to perform a binary operation, like 2+2, with something that isn't a number. Try validating your data to make sure QTY contains only numbers:

class(dt_ts[Regions==i & Items==j,]$QTY)

should return:

[1] "integer"
Will F
  • 191
  • 9
  • Usually it's not good practice to use `attach` in your code (use `with` instead). See the discussions [here](https://stackoverflow.com/questions/10067680/why-is-it-not-advisable-to-use-attach-in-r-and-what-should-i-use-instead) & [Problem with "attach"](https://stat.ethz.ch/pipermail/r-sig-teaching/2014q4/000591.html) – Tung Apr 08 '18 at 15:18
  • Using `i` and `j` for variable names are not good either as they are often used as part of `for` loops. [Good enough practices in scientific computing](http://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1005510#sec005) – Tung Apr 08 '18 at 15:21
  • My code was an isolated example meant to mimic the code he had already exposed. I assume it was already in a for loop. – Will F Apr 09 '18 at 00:18