# ----- Data Acquisition -----
<- "MRTSSM451211USN"
fred_code suppressMessages(getSymbols(fred_code, src = "FRED"))
[1] "MRTSSM451211USN"
<- get(fred_code) book_sales_xts
# ----- Data Acquisition -----
<- "MRTSSM451211USN"
fred_code suppressMessages(getSymbols(fred_code, src = "FRED"))
[1] "MRTSSM451211USN"
<- get(fred_code) book_sales_xts
<- fortify.zoo(book_sales_xts) %>%
book_sales_tbl rename(Date = Index, Sales = MRTSSM451211USN) %>%
mutate(Month = yearmonth(Date)) %>%
as_tsibble(index = Month)
%>%
book_sales_tbl autoplot(Sales) +
labs(y = "Sales (Millions of Dollars)", x = "Month") +
theme_minimal(base_size = 12)
%>%
book_sales_tbl gg_season(Sales, labels = "both") +
labs(y = "Sales (Millions of Dollars)") +
theme_minimal(base_size = 12)
%>%
book_sales_tbl model(STL(Sales ~ trend(window = 21) + season(window = "periodic"), robust = TRUE)) %>%
components() %>%
autoplot() +
theme_minimal(base_size = 10)
<- book_sales_tbl %>% model(ARIMA(Sales))
arima_fit %>% gg_tsresiduals() arima_fit
<- features(book_sales_tbl, Sales, guerrero)$lambda_guerrero
lambda_est <- book_sales_tbl %>% model(ARIMA(box_cox(Sales, lambda_est)))
arima_fit_bc %>% gg_tsresiduals() arima_fit_bc
The following global configuration automates plot exports using knitr’s chunk options, standardizing all figures as 300dpi
PNGs. By setting fig.path
and dimensions globally, it ensures consistent output quality and organized file storage.
```{r chunk-export, include=FALSE}
# Global settings for all figures
knitr::opts_chunk$set(
dev = "png", # PNG device
dpi = 300, # Print quality
fig.path = "figures/",# Organized storage
fig.ext = "png", # Explicit file extension
fig.width = 8, # 8" width
fig.height = 4.5 # 4.5" height
)
```
Storing figures in a dedicated /figures
folder maintains project organization for team workflows.
```{r main-plot, fig.cap="Book Sales Trend Analysis"}
# Plot will auto-save to figures/main-plot.png
book_sales_tbl %>%
autoplot(Sales) +
labs(title = NULL) + # Titles in caption instead
theme_minimal()
```
We can use knitr::include_graphics()
to cleanly insert pre-saved PNGs into our markdown or presentation documents.
```{r embed-plot, echo=FALSE}
# Best practice inclusion method
knitr::include_graphics("figures/seasonal-plot.png")
```