Home > Net >  R time series ggtsdisplay function won't run but I can do time series, ACF and PACF separately
R time series ggtsdisplay function won't run but I can do time series, ACF and PACF separately

Time:10-22

I'm using the following libraries to try and work with time series data.

First I install fpp3 because it has the aus_airpassengers dataset. Here is the dput for that dataset or else you could get it from the package. It just has two columns, one for Year and one for Passengers.

library(fpp3)

structure(list(Year = c(1970, 1971, 1972, 1973, 1974, 1975, 1976, 
1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 
1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 
1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 
2010, 2011), Passengers = c(7.3187, 7.3266, 7.7956, 9.3846, 10.6647, 
11.0551, 10.8643, 11.3065, 12.1223, 13.0225, 13.6488, 13.2195, 
13.1879, 12.6015, 13.2368, 14.4121, 15.4973, 16.8802, 18.8163, 
15.1143, 17.5534, 21.8601, 23.8866, 26.9293, 26.8885, 28.8314, 
30.0751, 30.9535, 30.1857, 31.5797, 32.577569, 33.477398, 39.021581, 
41.386432, 41.596552, 44.657324, 46.951775, 48.728837, 51.488427, 
50.026967, 60.640913, 63.3603103378)), row.names = c(NA, -42L
), key = structure(list(.rows = structure(list(1:42), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = c(NA, -1L), class = c("tbl_df", 
"tbl", "data.frame")), index = structure("Year", ordered = TRUE), index2 = "Year", interval = structure(list(
    year = 1, quarter = 0, month = 0, week = 0, day = 0, hour = 0, 
    minute = 0, second = 0, millisecond = 0, microsecond = 0, 
    nanosecond = 0, unit = 0), .regular = TRUE, class = c("interval", 
"vctrs_rcrd", "vctrs_vctr")), class = c("tbl_ts", "tbl_df", "tbl", 
"data.frame"))

aus_airpassengers <- aus_airpassengers

aus_airpassengers <- aus_airpassengers %>%
  filter(Year >= 1970) %>%
  filter(Year <= 2011)

I use autoplot which I think is from the ggplot2 package to see how passengers change over these 42 years.

autoplot(aus_airpassengers)   
  labs(title="Air Passengers from 1970 to 2011")  
  labs(x ="Date")   
  labs(y = "Passenger numbers (1000's)") 

enter image description here

Now I'm trying to use the ggtsdisplay function from the forecast package in order to plot the time series along with the ACF and PACF plots.

library(forecast)

ggtsdisplay(aus_airpassengers)

But it's giving me this error:

Error in ggtsdisplay(aus_airpassengers) : 
  ggtsdisplay is only for univariate time series

I looked online for what this error means, and I come across posts that tell me I probably have more than one value at the same time value. But that's not the case here. We can see that both of these tests evaluate to 42, meaning each Year is a unique year in the dataset (plus I can just look over the dataset to see that each year is unique.)

length(unique(aus_airpassengers$Year))
nrow(aus_airpassengers)

I'm also confused because if I use the feasts library to plot the ACF and PACF plots it works just fine.

library(feasts)

aus_airpassengers %>%
  ACF(Passengers) %>%
  autoplot() 

aus_airpassengers %>%
  PACF(Passengers) %>%
  autoplot() 

enter image description here

enter image description here

Does anyone know what's up with ggtsdisplay?

CodePudding user response:

You are using ggtsdisplay from the forecast package which is designed for ts objects. You need to use gg_tsdisplay from the feasts package (which is loaded when you load fpp3).

In general, if you are using tsibbles and fable, you should not be loading the forecast package.

  • Related