0

I would like to add a plot of the normal distribution with mean and variance of the residuals in a model in the same plot as the histogram of the residuals.

I am using the code below from chapter 5 of fpp3:

aug <- google_2015 |>
    model(NAIVE(Close)) |>
    augment()

autoplot(aug, .innov) +
    labs(y = "$US",
         title = "Residuals from the naïve method")

aug |>
    ggplot(aes(x = .innov)) +
    geom_histogram() +
    labs(title = "Histogram of residuals")
jpsmith
  • 11,023
  • 5
  • 15
  • 36
Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
  • `patchwork::wrap_plots` supports both ggplot2 and base graphics, see https://patchwork.data-imaginist.com/articles/guides/assembly.html, you can combine an arbitrary combination – r2evans Aug 18 '23 at 23:19

1 Answers1

1

It would be best to tell us that fpp3 is "Forecasting: Principles and Practice" (3rd Edition), that there is a corresponding R package, and to show us the code for creating the google_2015 object so we didn't have to go dig it out, but here you go ...

setup

library(fpp3)
google_2015 <- gafa_stock |>
  filter(Symbol == "GOOG", year(Date) >= 2015) |>
  mutate(day = row_number()) |>
  update_tsibble(index = day, regular = TRUE) |> 
  filter(year(Date) == 2015)
aug <- google_2015 |>
    model(NAIVE(Close)) |>
    augment()

plotting code

We need to (1) plot the histogram on a density rather than a count scale and (2) compute the required mean and SD on the fly (alternatively we could plot the histogram on the count scale and multiply the Normal density by the number of observations, but that seems slightly harder)

The only improvement of this on the linked duplicate is that the aes(y = ..density..) idiom used in those answers is deprecated as of ggplot2 3.4.0 and will throw a warning ...

gg0 <- aug |>
    ggplot(aes(x = .innov)) +
    geom_histogram(aes(y=after_stat(density)))
gg0 + geom_function(fun = dnorm, colour = "red", n = 1001,
         args = list(mean = mean(aug$.innov, na.rm = TRUE), 
                     sd = sd(aug$.innov, na.rm = TRUE)))
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • sorry, i forgot to add in the link for the fpp3 book and add other info. your solution works like a charm, – Ray Tayek Aug 19 '23 at 00:43