##Question 2
top_price <- gafa_stock %>%
dplyr::select(Date, Close,Symbol) %>%
group_by(Symbol) %>%
filter(Close == max(Close)) %>%
ungroup()
print(top_price)
## # A tsibble: 4 x 3 [!]
## # Key: Symbol [4]
## Date Close Symbol
## <date> <dbl> <chr>
## 1 2018-10-03 232. AAPL
## 2 2018-09-04 2040. AMZN
## 3 2018-07-25 218. FB
## 4 2018-07-26 1268. GOOG
tute1 <- readr::read_csv("tute1.csv")
## Rows: 100 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (3): Sales, AdBudget, GDP
## date (1): Quarter
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
mytimeseries <- tute1 |>
mutate(Quarter = yearquarter(Quarter)) |>
as_tsibble(index = Quarter)
##With facet_grid
withfacet_grid <- mytimeseries |>
pivot_longer(-Quarter) |>
ggplot(aes(x = Quarter, y = value, colour = name)) +
geom_line() +
labs(title = "With Facet_grid", x = "Quarter", y = "Value") +
facet_grid(name ~ ., scales = "free_y")
##With facet_grid
withoutfacet_grid <- mytimeseries |>
pivot_longer(-Quarter) |>
ggplot(aes(x = Quarter, y = value, colour = name)) +
geom_line() +
labs(title = "Without Facet_grid", x = "Quarter", y = "Value")
grid.arrange(withfacet_grid,withoutfacet_grid,ncol = 2)
Facet_grid command creates separate y axes for each series we are
interested in viewing. Given the different units, this is helpful.
library(USgas)
?us_total
gas_tsibble <- as_tsibble(us_total, index = year, key = state)
gas_tsibble %>%
filter(state %in% c('Massachusetts','Rhode Island','New Hampshire','Vermont','Maine','Connecticut')) %>%
autoplot()
## Plot variable not specified, automatically selected `.vars = y`
unique(aus_retail$Industry)
## [1] "Cafes, restaurants and catering services"
## [2] "Cafes, restaurants and takeaway food services"
## [3] "Clothing retailing"
## [4] "Clothing, footwear and personal accessory retailing"
## [5] "Department stores"
## [6] "Electrical and electronic goods retailing"
## [7] "Food retailing"
## [8] "Footwear and other personal accessory retailing"
## [9] "Furniture, floor coverings, houseware and textile goods retailing"
## [10] "Hardware, building and garden supplies retailing"
## [11] "Household goods retailing"
## [12] "Liquor retailing"
## [13] "Newspaper and book retailing"
## [14] "Other recreational goods retailing"
## [15] "Other retailing"
## [16] "Other retailing n.e.c."
## [17] "Other specialised food retailing"
## [18] "Pharmaceutical, cosmetic and toiletry goods retailing"
## [19] "Supermarket and grocery stores"
## [20] "Takeaway food services"
set.seed(1234)
myseries <- aus_retail |>
filter(`Series ID` == sample(aus_retail$`Series ID`,1))
autoplot(myseries)
## Plot variable not specified, automatically selected `.vars = Turnover`
gg_season(myseries)
## Plot variable not specified, automatically selected `y = Turnover`
gg_subseries(myseries)
## Plot variable not specified, automatically selected `y = Turnover`
gg_lag(myseries)
## Plot variable not specified, automatically selected `y = Turnover`
ACF(myseries, var = Turnover) |> autoplot()
## Warning: The `...` argument of `PACF()` is deprecated as of feasts 0.2.2.
## ℹ ACF variables should be passed to the `y` argument. If multiple variables are
## to be used, specify them using `vars(...)`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
##Question 8
colnames(us_employment)
## [1] "Month" "Series_ID" "Title" "Employed"
private <- us_employment %>%
filter(Title =="Total Private")
autoplot(private)
## Plot variable not specified, automatically selected `.vars = Employed`
gg_season(private)
## Plot variable not specified, automatically selected `y = Employed`
gg_subseries(private)
## Plot variable not specified, automatically selected `y = Employed`
gg_lag(private)
## Plot variable not specified, automatically selected `y = Employed`
ACF(private, var = Employed, maxLag = 12) |> autoplot()
## Warning: ACF currently only supports one column, `Employed` will be used.
#### Bricks ####
colnames(aus_production)
## [1] "Quarter" "Beer" "Tobacco" "Bricks" "Cement"
## [6] "Electricity" "Gas"
bricks <- aus_production %>%
dplyr::select(Quarter,Bricks)
autoplot(bricks)
## Plot variable not specified, automatically selected `.vars = Bricks`
## Warning: Removed 20 rows containing missing values (`geom_line()`).
gg_season(bricks)
## Plot variable not specified, automatically selected `y = Bricks`
## Warning: Removed 20 rows containing missing values (`geom_line()`).
gg_subseries(bricks)
## Plot variable not specified, automatically selected `y = Bricks`
## Warning: Removed 5 rows containing missing values (`geom_line()`).
gg_lag(bricks)
## Plot variable not specified, automatically selected `y = Bricks`
## Warning: Removed 20 rows containing missing values (gg_lag).
ACF(bricks, var = Bricks) |> autoplot()
###Hare###
data("pelt")
colnames(pelt)
## [1] "Year" "Hare" "Lynx"
hare <- pelt %>%
dplyr::select(Year,Hare)
autoplot(hare)
## Plot variable not specified, automatically selected `.vars = Hare`
gg_subseries(hare)
## Plot variable not specified, automatically selected `y = Hare`
gg_lag(hare)
## Plot variable not specified, automatically selected `y = Hare`
ACF(hare, var = Hare) |> autoplot()
data("PBS")
colnames(PBS)
## [1] "Month" "Concession" "Type" "ATC1" "ATC1_desc"
## [6] "ATC2" "ATC2_desc" "Scripts" "Cost"
HO2 <- PBS %>%
filter(ATC2 == "H02") %>%
dplyr::select(Month, Cost)
autoplot(HO2)
## Plot variable not specified, automatically selected `.vars = Cost`
gg_season(HO2)
## Plot variable not specified, automatically selected `y = Cost`
gg_subseries(HO2)
## Plot variable not specified, automatically selected `y = Cost`
ACF(HO2, var = Cost) |> autoplot()
colnames(us_gasoline)
## [1] "Week" "Barrels"
autoplot(us_gasoline)
## Plot variable not specified, automatically selected `.vars = Barrels`
gg_season(us_gasoline)
## Plot variable not specified, automatically selected `y = Barrels`
gg_subseries(us_gasoline)
## Plot variable not specified, automatically selected `y = Barrels`
gg_lag(us_gasoline)
## Plot variable not specified, automatically selected `y = Barrels`
ACF(us_gasoline, var = Barrels) |> autoplot()
#### Chapter 2 ###Question 9 1 -> B No clear seasonal pattern 2 ->
A Steady seasonal pattern is reflected in ACF A. Follows monthly
pattern. 3 -> D Monthly seasonal pattern shown in D. 4 -> C 5 year
and 10 year groupings appear in the D ACF.
Canadian gas seasonality shifts over time, making box cox unhelpful.
canadian_gas |> autoplot()
## Plot variable not specified, automatically selected `.vars = Volume`
canadian_gas |> autoplot(box_cox(Volume,-.5))
canadian_gas |> autoplot(box_cox(Volume,.5))
gas <- tail(aus_production, 5*4) |> dplyr::select(Gas)
autoplot(gas)
## Plot variable not specified, automatically selected `.vars = Gas`
##Seasonality? Production ppears to increase in quarters 2 and 3.
#B
decomp <- as_tsibble(gas) |>
model(classical_decomposition(Gas, type = "multiplicative"))
plot1 <- components(decomp) |> autoplot()
##C: Do the results confirm initial interpretation? Yes, overall trend slightly positive, seasonality is clear.
##D:
# Plot the seasonally adjusted data
components <- components(decomp)
ggplot(components,aes(x = Quarter, y = season_adjust)) +
ggtitle("Seasonally Adjusted Data") +
geom_line(aes(y = season_adjust))
##E
gas2 <- gas
gas2$Gas[8] <- gas2$Gas[8] + 300
decomp2 <- as_tsibble(gas2) |>
model(classical_decomposition(Gas, type = "multiplicative"))
plot2 <- components(decomp2) |> autoplot()
grid.arrange(plot1,plot2, ncol(2))
## Warning: Removed 2 rows containing missing values (`geom_line()`).
## Removed 2 rows containing missing values (`geom_line()`).
##F
gas3 <- gas
gas3$Gas[18] <- gas3$Gas[18] + 300
decomp3 <- as_tsibble(gas3) |>
model(classical_decomposition(Gas, type = "multiplicative"))
plot3 <- components(decomp3) |> autoplot()
plot1
## Warning: Removed 2 rows containing missing values (`geom_line()`).
plot2
## Warning: Removed 2 rows containing missing values (`geom_line()`).
plot3
## Warning: Removed 2 rows containing missing values (`geom_line()`).
##Yes, it does make a difference. The placement of outlier determines the point at which trend is disrupted. Seasonality seems to shift more when outliers are placed at toward the end of the series.
####Chapter 3 ###Question 9 A: There appears to be consistent seasonality in this series as displayed by the stl decomposition figure. Months of particular note are January and August (on the low side) as well as September and December (on the high side). Seasonal factors could include the winter holiday season (increase in December followed by decrease in January) and the summer (part-time workers or students leaving the workforce in August and re-entering in September).
B: Yes, during ’91-’92 the remainder graph appears to show a downward trend that is not attributed to seasonal fluctuations. A recession during that period would explain it.
####Chapter 3 ###Question 10
##A:
cangas <- as.ts(cangas)
autoplot(cangas)
##B:
cangas %>%
stl(t.window=13, s.window=11) %>%
autoplot()
cangas_decomp <- cangas %>%
stl(t.window=13, s.window=11)
##C:
#Seasonality appears more pronounced in the middle of the time period, from 1980-1990.
##D:
#Given the inconsistency of the seasonal trend, it would be better to create the seasonally adjusted series using STL. STL, using LOESS smoothing, can account for the changing seasonality over time. This is a more effective way of accounting for the seasonal trend in this series.
cangas_sea_adjusted <- seasadj(cangas_decomp)
plot1 <- autoplot(cangas_sea_adjusted)
#E:
# Perform seasonal adjustment
cangas <- as.ts(cangas)
cangas_adjusted_seats <- seas(cangas)
# Perform seasonal adjustment using X-11
x11_cangas <- seas(cangas, x11 = "")
# Extract the seasonally adjusted data
seasonally_adjusted_seats <- final(cangas_adjusted_seats)
plot2 <- autoplot(seasonally_adjusted_seats)
seasonally_adjusted_x11 <- final(x11_cangas)
plot3 <- autoplot(seasonally_adjusted_x11)
grid.arrange(plot1,plot2,plot3, ncol(1))
stl <- as.data.frame(cangas_sea_adjusted)
x11 <- as.data.frame(seasonally_adjusted_x11)
seats <- as.data.frame(seasonally_adjusted_seats)
seas_comp <- cbind(stl,x11,seats)
colnames(seas_comp) <- c("stl","x11","seats")
seas_comp$Date <- seq.Date(from = as.Date("1960-01-01"),
to = as.Date("2005-02-01"),
by = "month")
seas_comp <- seas_comp %>%
pivot_longer(1:3,names_to = 'adjustment_type', values_to = 'cangas')
ggplot(seas_comp, aes(x=Date, y=cangas, color=adjustment_type)) +
geom_line()
#They appear to follow a very similar pattern. Either the changes in the seasonality are not as dramatic as I initially thought, or I did something wrong.