cancel
Showing results for 
Search instead for 
Did you mean: 

Custom R component behaving strangely

0 Kudos

Hello,

I'm using a custom R component in PA 2.4 to forecast hourly power consumption data for multiple customers (cannot use the built in algorithms because they do not allow me to segment the data per customer and they do an aggregated forecast).

I'm using a fourier decomposition to use as an regressor for my time series (variable xreg1):

ts3 <- msts(cust1$POWER,seasonal.periods=c(24,168))

z <- fourier(ts3,K=c(5,5))

zfall <- fourierf(ts3,K=c(5,5),h=nr)

xreg1 <- cbind(z,cust1$CDD,cust1$BankHoliday)

The problem is that if I use auto.arima it works, if I use arima (as I know the parameters to use) I get an error that xreg1 does not exist.

So the following code works:

     fit <- auto.arima(ts3, xreg=xreg1, seasonal = FALSE)

But the following code gives me an error (that object xreg1 does not exist - "Error in eval(expr, envir,enclos) : object 'xreg1' not found"):

   

     fit <- arima(ts3, order=c(4,1,3), xreg=xreg1)

My code works in the R console so I don't understand what PA needs in order for this to work.

If you have any ideas, please let me know.

Regards,

Dan

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

I found the issue: it seems that " fit <- arima(ts3, order=c(4,1,3), xreg=xreg1)"  does not work because of the lowercase "a".


Used as below the code works without issues:


fit <- Arima(ts3, order=c(4,1,3), xreg=xreg1)


Apparently there are 2 arima functions in R (one being arima, the second Arima).


Although in the R console there are no issues with this, the custom R component in PA seems to see it as an error but has a strange way of reporting it.


Thank you for your help, Eser!


Dan

achab
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Dan,

As a FYI, we have a web site for you to report product improvement ideas.

See Predictive Analytics: Home

My 2 cents here,

Best regards

Antoine

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi Dan,


Try to write your code as a function and define all your variables in this function.

Regards,

Eser

0 Kudos

Hi Eser,

I've tried to encapsulate the arima function within a function and it now gives a different error message

The function is below:

ar <- function(ts,or,xr){

  res <- arima(ts, order = or, xreg = xr)

  return(res)

}

I'm calling it as:

      fit <- ar(ts3, c(4,1,3), xreg1)

The strange thins is that if I change it as below:

ar <- function(ts,xr){

  res <- auto.arima(ts, xreg = xr)

  return(res)

}

And call it:

    

     fit <- ar(ts3,  xreg1)

It works without any problems.

Again, if I test the query in the R console it works without errors/warnings.

It seems that there is something that PA does not like about it, but I can't figure out what...

Just as an appendix, the whole code:

reg1 <- function(df1, cus, hn){

  library(forecast)

  nr <- as.numeric(hn)

  cust1 <- df1[df1$Cust == cus,] 

  cust1$Timestamp <- as.POSIXct(cust1$Timestamp,tz="UTC",format="%Y-%m-%d %H:%M") 

  cust1$Date <- as.Date(cust1$Date,"%Y-%m-%d")

  cust1 <- cust1[order(cust1$Timestamp),]

 

  ts3 <- msts(cust1$POWER,seasonal.periods=c(24,168))

  z <- fourier(ts3,K=c(5,5))

  zfall <- fourierf(ts3,K=c(5,5),h=nr)

 

  xreg1 <- cbind(z,cust1$CDD,cust1$BankHoliday)

 

  fit <- ar(ts3, c(4,1,3), xreg1)

  summ <- fit

 

  tmp <- cust1[8761,]

  tmp_all <- cust1

 

  for (i in 1:nr)

  {

    tmp[1,1] <- tmp[1,1] + 3600

    tmp[1,2] <- as.Date(tmp[1,1])

    tmp[1,3] <- strftime(tmp[1,1], format="%H", tz="UTC")

    tmp[1,4:11] = c(0,NA,NA,0,NA,0,0,0)

    tmp_all <- rbind(tmp_all,tmp)

  } 

 

  fcall <- forecast(fit,xreg=cbind(zfall,cust1$CDD[1:nr],cust1$BankHoliday[1:nr]),h=nr) 

 

  tmp_all$arima <- c(fcall$fitted,fcall$mean)

 

  tmp_all$Date <- as.character(tmp_all$Date)

 

  #tmp_all$Timestamp <- as.POSIXlt(cust1$Timestamp,tz="UTC",format="%Y-%m-%d %H:%M")

  return(list(out=tmp_all))

}

ar <- function(ts,or,xr){

  res <- arima(ts, order = or, xreg = xr)

  return(res)

}

Former Member
0 Kudos

Hi Dan,

I think you need to rearrange your code to make it compatible with PA. Because every component needs a data frame as an input where your input "ts" is a time series.

Check this post:

Maybe someone can help better...

Eser