Year | Observation |
---|---|
2012 | 123 |
2013 | 39 |
2014 | 78 |
2015 | 52 |
2016 | 110 |
y <- ts(c(123,39,78,52,110), start=2012)
print(y)
## Time Series:
## Start = 2012
## End = 2016
## Frequency = 1
## [1] 123 39 78 52 110
z <- rnorm(24) # Example random data
y_monthly <- ts(z, start=2003, frequency=12)
print(y_monthly)
## Jan Feb Mar Apr May Jun
## 2003 0.07892140 -0.01494641 -0.60744695 -0.49877288 0.20708524 -0.69320963
## 2004 -1.22666201 1.49370891 0.72677341 -1.64537366 0.21715871 -0.31949408
## Jul Aug Sep Oct Nov Dec
## 2003 -0.27153537 0.74119921 -1.34941574 0.57876384 -1.65858722 1.24228295
## 2004 0.10170494 -0.61251667 0.49055486 -0.98854571 -1.51802061 -0.24212867
The “frequency” is the number of observations before the seasonal pattern repeats. Below are common frequencies used in R:
Data Type | Frequency |
---|---|
Annual | 1 |
Quarterly | 4 |
Monthly | 12 |
Weekly | 52 |
Daily | 7 or 365.25 |
frequency = 60
)frequency = 1440
)frequency = 10080
)frequency = 525960
)library(fpp3)
install.packages("tsibbledata")
## Warning: package 'tsibbledata' is in use and will not be installed
library(tsibbledata)
autoplot(aus_retail) + ggtitle("Australian Retail Data")
## Plot variable not specified, automatically selected `.vars = Turnover`
# Load required libraries
library(fpp3)
library(tsibbledata) # Ensure tsibbledata is installed
# Filter for Melbourne-Sydney Economy Class passengers
melsyd <- ansett %>%
filter(Airports == "MEL-SYD", Class == "Economy")
# Plot the data
autoplot(melsyd, Passengers) +
ggtitle("Economy class passengers: Melbourne-Sydney") +
xlab("Year") +
ylab("Thousands")
# Load necessary libraries
library(fpp3)
library(tsibbledata) # Ensure tsibbledata is loaded
# Check available datasets in tsibbledata
data(package = "tsibbledata")
# Visualizing antidiabetic drug sales (PBS dataset)
PBS %>%
filter(ATC2 == "A10") %>% # A10 is the code for antidiabetic drugs
autoplot(Cost) +
ggtitle("Antidiabetic Drug Sales") +
ylab("$ million") +
xlab("Year")
# Exercise 2.3: Time Series Patterns
autoplot(gafa_stock) + ggtitle("Stock Prices of GAFA Companies")
## Plot variable not specified, automatically selected `.vars = Open`
# Load necessary libraries
library(fpp3)
# Use the PBS dataset for antidiabetic drug sales
PBS %>%
filter(ATC2 == "A10") %>%
gg_season(Cost, labels = "both") +
ylab("$ million") +
ggtitle("Seasonal Plot: Antidiabetic Drug Sales")
# Load necessary libraries
library(fpp3)
# Use the PBS dataset for antidiabetic drug sales
PBS %>%
filter(ATC2 == "A10") %>%
gg_season(Cost, polar = TRUE) +
ylab("$ million") +
ggtitle("Polar Seasonal Plot: Antidiabetic Drug Sales")
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?
# Load necessary libraries
library(fpp3)
# Use the PBS dataset for antidiabetic drug sales
PBS %>%
filter(ATC2 == "A10") %>%
gg_subseries(Cost) +
ylab("$ million") +
ggtitle("Seasonal Subseries Plot: Antidiabetic Drug Sales")
#ggAcf(beer2)
#ggAcf(aelec, lag=48)
library(fpp3)
# Extract beer production data
beer2 <- aus_production %>%
filter_index("1992 Q1" ~ "2006 Q4") %>% # Use appropriate years
select(Beer)
# Compute and plot the ACF for beer production
beer2 %>%
ACF(Beer) %>%
autoplot() +
ggtitle("Autocorrelation Function of Beer Production")