Autoplot fn not working with forecast method

I am trying to run following code as given in FPP3 book, however it is produces an error:-

fit <- gdppc %>% model(trend_model = TSLM(GDP_per_capita ~ trend()))

fit |>
forecast(h = "3 years") |>
filter(Country == "Sweden") |>
autoplot(gdppc) +
labs(y = "$US", title = "GDP per capita for Sweden")

This code produces the following error:-

Error in f(...) :
3 arguments passed to .Internal(is.unsorted) which requires 2

However, when I tweak it a little bit and do not pass any argument to autoplot function it produces the plot only for forecasted values. It does not show original data along with forecasted point values and prediction intervals as shown in the FPP3 book. Following is the code for reference:-

gdppc %>% model(trend_model = TSLM(GDP_per_capita ~ trend())) %>%
forecast(h = "5 years") %>%
filter(Country == "Australia") %>%
autoplot() +
labs(y = "Number of People", title = "Forecast for Aus GDPPC")

Can anyone explain why autoplot is not working with forecast object and help me to improve the code to get desired output of original values along with forecasted point values and prediction intervals as shown in the FPP3 book.

Perhaps you've loaded some other package that is over-writing the autoplot method. Try running your code in a new session with only library(fpp3) loaded.

1 Like

Sir as per your instructions, I have loaded only fpp3 library and run the code as mentioned. Pse refer to the screenshot attached.

However, the error persists. I tried again after clearing the environment but the error persists.

Requesting your kind assistance.

Regards

I can't reproduce the problem. Please run sessionInfo() immediately after the problem occurs, and post the output.

I have attached the screenshot with sessionInfo() immediately after the issue.

Thanks and Warm Regards

OK. Nothing obvious there. Now run traceback() immediately after the error is obtained, and post the output.

Dear Sir, please find attached screenshot immediately after execution of traceback() command.

Thanks and Warm Regards

This looks like a potential R installation issue. My best guess is that you should try re-installing (and updating) R.

The error is suggesting that the internal function is.unsorted expects 2 arguments, however this is not the case. I'm hopeful that re-installing R will fix this internal function so that it expects the 3 arguments that it usually does.

1 Like

Hi Mitchel, thanks a lot for the help.

I reinstalled R and RStudio and it solved the issue.

Thanks again for the help.

Regards

1 Like

Fantastic, glad we could figure it out.

1 Like

Hello,

I am having the same problem, however it still occurs after reinstalling R and RStudio. I am using R in windows, but the problem also happened using WSL rstudio-server. Both in R 4.3.1.
Is there a workaround to get both the data and the forecast with autoplot (or ggplot)?

Please ask a new question and provide reproducible code.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.