Plot function in R not showing months

I have residual results from auto.arima as well as arima. I need these residuals as ZOO object, becuause the next package requires the input as zoo object. Also I will be plotting results from the zoo object analysis. During all this, I want the graph to show months.

I have tried converting to zoo and plotting residuals from ts, xts, manual arima, auto arima but I am not getting the months. Its either some weird numbers or its days...I want like every quarter month...Like Jan, April, July, Oct.. etc...

library(timeSeries)
library(zoo)
library(tseries)
library(xts)
library(forecast)
reconstruct<-structure(list(...1 = structure(c(1579651200, 1579737600, 1579824000, 
                                               1580083200, 1580169600, 1580256000, 1580342400, 1580428800, 1580688000, 
                                               1580774400, 1580860800, 1580947200, 1581033600, 1581292800, 1581379200, 
                                               1581465600, 1581552000, 1581638400, 1581897600, 1581984000, 1582070400, 
                                               1582156800, 1582243200, 1582502400, 1582588800, 1582675200, 1582761600, 
                                               1582848000, 1583107200, 1583193600, 1583280000, 1583366400, 1583452800, 
                                               1583712000, 1583798400, 1583884800, 1583971200, 1584057600, 1584316800, 
                                               1584403200, 1584489600, 1584576000, 1584662400, 1584921600, 1585008000, 
                                               1585094400, 1585180800, 1585267200, 1585526400, 1585612800, 1585699200, 
                                               1585785600, 1585872000, 1586131200, 1586217600, 1586304000, 1586390400, 
                                               1586476800, 1586736000, 1586822400, 1586908800, 1586995200, 1587081600, 
                                               1587340800, 1587427200, 1587513600, 1587600000, 1587686400, 1587945600, 
                                               1588032000, 1588118400, 1588204800, 1588291200, 1588550400, 1588636800, 
                                               1588723200, 1588809600, 1588896000, 1589155200, 1589241600, 1589328000, 
                                               1589414400, 1589500800, 1589760000, 1589846400, 1589932800, 1590019200, 
                                               1590105600, 1590364800, 1590451200, 1590537600, 1590624000, 1590710400, 
                                               1590969600, 1591056000, 1591142400, 1591228800, 1591315200, 1591574400, 
                                               1591660800), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
                            death = c(17, 18, 26, 82, 131, 133, 172, 214, 428, 494, 566, 
                                      636, 721, 1015, 1115, 1120, 1373, 1525, 1870, 2010, 2125, 
                                      2250, 2254, 2633, 2713, 2774, 2817, 2876, 3090, 3164, 3260, 
                                      3355, 3469, 4009, 4284, 4636, 4957, 5460, 7265, 8117, 9073, 
                                      10215, 11789, 17518, 19944, 22915, 26165, 29804, 42040, 47204, 
                                      53846, 60087, 66583, 85288, 93805, 101022, 109173, 116914, 
                                      136917, 144272, 153036, 160746, 169629, 188670, 195864, 203141, 
                                      210412, 217250, 233562, 240188, 247168, 253423, 259010, 274146, 
                                      280293, 287056, 292923, 298876, 312208, 317941, 323378, 329130, 
                                      334886, 347700, 352668, 358074, 363110, 368816, 378366, 383144, 
                                      388363, 393355, 398511, 409902, 415166, 420786, 426314, 431371, 
                                      443223, 448268)), row.names = c(NA, 100L), class = "data.frame")

dt <- seq(as.Date("2020-01-22"), as.Date("2020-06-09"), by = "days")
dt<-dt[!weekdays(dt) %in% c("Saturday","Sunday")] # This removes weekends from data.
View(dt)
deathdata<-reconstruct[,2]
deathts=as.ts(deathdata,dt)
deathxts=as.xts(deathdata,dt)

#Calculating rate of change similar to returns using both forms

rdeathxts=100*diff(log(deathxts))
rdeathts=100*diff(log(deathts))

#I did auto.arima for xts and ts objects.
arimaxts=auto.arima(rdeathxts)
summary(arimaxts)

arimats=auto.arima(rdeathts)
summary(arimats)

#I do manual arima for both xts and ts objects.
myarima2ts=arima(rdeathts, order=c(4,1,3),include.mean=FALSE)# AS Auto arima used 0 mean.
summary(myarima2ts)

myarima2xts=arima(rdeathxts, order=c(4,1,3))
summary(myarima2xts)

# I need the residuals of auto.arima as zoo object, and then I want to plot them, BUT 
#I NEED THE GRAPH WITH MONTHS.I am calling the Residuals as Res.

RESdeathTS<-as.zoo(arimats$residuals)
plot(RESdeathTS)

RESdeathXTS<-as.zoo(arimaxts$residuals)
plot(RESdeathXTS)

#I tried with residuals of manual arima as well

RESdeath2TS<-as.zoo(myarima2ts$residuals)
plot(RESdeath2TS)

RESdeath2XTS<-as.zoo(myarima2xts$residuals)
plot(RESdeath2XTS)

I think you would find the tsibble and fable packages easier to do this. Here is the code and plot using your data.

library(dplyr)
library(tsibble)
library(fable)
library(ggplot2)

reconstruct <- structure(list(
  date = structure(c(
    1579651200, 1579737600, 1579824000,
    1580083200, 1580169600, 1580256000, 1580342400, 1580428800, 1580688000,
    1580774400, 1580860800, 1580947200, 1581033600, 1581292800, 1581379200,
    1581465600, 1581552000, 1581638400, 1581897600, 1581984000, 1582070400,
    1582156800, 1582243200, 1582502400, 1582588800, 1582675200, 1582761600,
    1582848000, 1583107200, 1583193600, 1583280000, 1583366400, 1583452800,
    1583712000, 1583798400, 1583884800, 1583971200, 1584057600, 1584316800,
    1584403200, 1584489600, 1584576000, 1584662400, 1584921600, 1585008000,
    1585094400, 1585180800, 1585267200, 1585526400, 1585612800, 1585699200,
    1585785600, 1585872000, 1586131200, 1586217600, 1586304000, 1586390400,
    1586476800, 1586736000, 1586822400, 1586908800, 1586995200, 1587081600,
    1587340800, 1587427200, 1587513600, 1587600000, 1587686400, 1587945600,
    1588032000, 1588118400, 1588204800, 1588291200, 1588550400, 1588636800,
    1588723200, 1588809600, 1588896000, 1589155200, 1589241600, 1589328000,
    1589414400, 1589500800, 1589760000, 1589846400, 1589932800, 1590019200,
    1590105600, 1590364800, 1590451200, 1590537600, 1590624000, 1590710400,
    1590969600, 1591056000, 1591142400, 1591228800, 1591315200, 1591574400,
    1591660800
  ), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
  death = c(
    17, 18, 26, 82, 131, 133, 172, 214, 428, 494, 566,
    636, 721, 1015, 1115, 1120, 1373, 1525, 1870, 2010, 2125,
    2250, 2254, 2633, 2713, 2774, 2817, 2876, 3090, 3164, 3260,
    3355, 3469, 4009, 4284, 4636, 4957, 5460, 7265, 8117, 9073,
    10215, 11789, 17518, 19944, 22915, 26165, 29804, 42040, 47204,
    53846, 60087, 66583, 85288, 93805, 101022, 109173, 116914,
    136917, 144272, 153036, 160746, 169629, 188670, 195864, 203141,
    210412, 217250, 233562, 240188, 247168, 253423, 259010, 274146,
    280293, 287056, 292923, 298876, 312208, 317941, 323378, 329130,
    334886, 347700, 352668, 358074, 363110, 368816, 378366, 383144,
    388363, 393355, 398511, 409902, 415166, 420786, 426314, 431371,
    443223, 448268
  )
), row.names = c(NA, 100L), class = "data.frame")

# Convert to tsibble object
reconstruct <- reconstruct |>
    mutate(
        date = as.Date(date),
        rdeath = 100*tsibble::difference(log(death))
        ) |>
    tsibble::as_tsibble(index = date)

# Fit ARIMA model
fit <- reconstruct |>
    tsibble::fill_gaps() |>
    model(arima = ARIMA(rdeath))

# Plot residuals
fit |>
    residuals() |>
    autoplot() +
    labs(x = "Month", y = "Residuals")
#> Plot variable not specified, automatically selected `.vars = .resid`
#> Warning: Removed 1 row containing missing values or values outside the scale range
#> (`geom_line()`).

Created on 2024-09-26 with reprex v2.1.1

If you need to convert the results to a zoo object, I suggest you use the tsbox package.

# Convert residuals to zoo object
residuals <- fit |>
	residuals() |>
	filter(!is.na(.resid)) |>
	tsbox::ts_zoo()

Thank you so much @robjhyndman . It worked ... :slight_smile:

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.