library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(tsibble)
## Warning: package 'tsibble' was built under R version 4.3.3
## Registered S3 method overwritten by 'tsibble':
## method from
## as_tibble.grouped_df dplyr
##
## Attaching package: 'tsibble'
## The following object is masked from 'package:lubridate':
##
## interval
## The following objects are masked from 'package:base':
##
## intersect, setdiff, union
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ stringr 1.5.1
## ✔ forcats 1.0.0 ✔ tibble 3.2.1
## ✔ purrr 1.0.2 ✔ tidyr 1.3.1
## ✔ readr 2.1.5
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ tsibble::interval() masks lubridate::interval()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(fpp3)
## Warning: package 'fpp3' was built under R version 4.3.3
## ── Attaching packages ──────────────────────────────────────────── fpp3 1.0.0 ──
## ✔ tsibbledata 0.4.1 ✔ fable 0.3.4
## ✔ feasts 0.3.2 ✔ fabletools 0.4.2
## Warning: package 'tsibbledata' was built under R version 4.3.3
## Warning: package 'feasts' was built under R version 4.3.3
## Warning: package 'fabletools' was built under R version 4.3.3
## Warning: package 'fable' was built under R version 4.3.3
## ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
## ✖ lubridate::date() masks base::date()
## ✖ dplyr::filter() masks stats::filter()
## ✖ tsibble::intersect() masks base::intersect()
## ✖ tsibble::interval() masks lubridate::interval()
## ✖ dplyr::lag() masks stats::lag()
## ✖ tsibble::setdiff() masks base::setdiff()
## ✖ tsibble::union() masks base::union()
library(forecast)
## Warning: package 'forecast' was built under R version 4.3.3
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
?aus_production
## starting httpd help server ... done
?pelt
?gafa_stock
?vic_elec
autoplot(aus_production, Bricks)
## Warning: Removed 20 rows containing missing values or values outside the scale range
## (`geom_line()`).
autoplot(pelt,Lynx)
autoplot(gafa_stock, Close)
autoplot(vic_elec, Demand)
autoplot(vic_elec, Demand) +
ggtitle("Half-hourly electricity demand of Victoria") +
xlab("Date ") +
ylab("Demand")+
geom_line(colour = "purple")
library(dplyr)
gafa_stock %>%
group_by(Symbol) %>% #Grouping by stock symbol
filter(Close == max(Close)) %>% #Filtering to find the row with the max closing value for each stock
select(Symbol, Date, Close)
## # A tsibble: 4 x 3 [!]
## # Key: Symbol [4]
## # Groups: Symbol [4]
## Symbol Date Close
## <chr> <date> <dbl>
## 1 AAPL 2018-10-03 232.
## 2 AMZN 2018-09-04 2040.
## 3 FB 2018-07-25 218.
## 4 GOOG 2018-07-26 1268.
## 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)
mytimeseries |>
pivot_longer(-Quarter) |>
ggplot(aes(x = Quarter, y = value, colour = name)) +
geom_line() +
facet_grid(name ~ ., scales = "free_y")
mytimeseries |>
pivot_longer(-Quarter) |>
ggplot(aes(x = Quarter, y = value, colour = name)) +
geom_line()
#install.packages('USgas')
library(USgas)
## Warning: package 'USgas' was built under R version 4.3.3
data("us_total")
us_gas_tsibble <- as_tsibble(us_total, key = state, index = year)
new_england_states <- c("Maine", "Vermont", "New Hampshire", "Massachusetts", "Connecticut", "Rhode Island")
new_england_tsibble <- us_gas_tsibble %>% filter(state %in% new_england_states)
autoplot(new_england_tsibble, y) +
labs(title = "Annual Natural Gas Consumption by State in New England Area",
x = "Year",
y = "Natural Gas Consumption")
tourism1<- readxl::read_excel("tourism.xlsx")
View(tourism1)
str(tourism1)
## tibble [24,320 × 5] (S3: tbl_df/tbl/data.frame)
## $ Quarter: chr [1:24320] "1998-01-01" "1998-04-01" "1998-07-01" "1998-10-01" ...
## $ Region : chr [1:24320] "Adelaide" "Adelaide" "Adelaide" "Adelaide" ...
## $ State : chr [1:24320] "South Australia" "South Australia" "South Australia" "South Australia" ...
## $ Purpose: chr [1:24320] "Business" "Business" "Business" "Business" ...
## $ Trips : num [1:24320] 135 110 166 127 137 ...
tourism1<-tourism1 %>%
mutate(
Quarter = yearquarter(Quarter),
Trips = as.numeric(Trips)
)
tourism1_tsibble <- tourism1 %>%
as_tsibble(key = c(Region, State, Purpose), index = Quarter)
max_average_trips <- tourism1_tsibble %>%
group_by(Region, Purpose) %>%
summarize(max_average_trips = mean(Trips, na.rm = TRUE)) %>%
arrange(desc(max_average_trips))
## Warning: Current temporal ordering may yield unexpected results.
## ℹ Suggest to sort by `Region`, `Purpose`, `Quarter` first.
head(max_average_trips,1)
## # A tsibble: 1 x 4 [1Q]
## # Key: Region, Purpose [1]
## # Groups: Region [1]
## Region Purpose Quarter max_average_trips
## <chr> <chr> <qtr> <dbl>
## 1 Melbourne Visiting 2017 Q4 985.
statetrips_tsibble <- tourism1 %>%
group_by(State, Quarter) %>%
summarize(total_trips = sum(Trips, na.rm = TRUE)) %>%
as_tsibble(key = State, index = Quarter)
## `summarise()` has grouped output by 'State'. You can override using the
## `.groups` argument.
fixed<-us_employment %>%
filter(Title == "Total Private")
fixed%>% autoplot(Employed)
fixed%>%gg_season(Employed)
fixed%>% gg_subseries(Employed)
fixed%>% gg_lag(Employed)
ACF(fixed, Employed) %>%
autoplot()
autoplot(aus_production,Bricks)
## Warning: Removed 20 rows containing missing values or values outside the scale range
## (`geom_line()`).
gg_season(aus_production,Bricks)
## Warning: Removed 20 rows containing missing values or values outside the scale range
## (`geom_line()`).
gg_subseries(aus_production,Bricks)
## Warning: Removed 5 rows containing missing values or values outside the scale range
## (`geom_line()`).
gg_lag(aus_production,Bricks)
## Warning: Removed 20 rows containing missing values (gg_lag).
ACF(aus_production,Bricks)%>%
autoplot()
autoplot(pelt,Hare)
gg_subseries(pelt,Hare)
gg_lag(pelt,Hare)
ACF(pelt,Hare)%>%
autoplot()
fixed2<-PBS %>%
filter(ATC2 == "H02")
fixed2%>% autoplot(Cost)
fixed2%>%gg_season(Cost)
fixed2%>% gg_subseries(Cost)
ACF(fixed2, Cost) %>%
autoplot()
#### It is difficult to tell if there’s a trend. In many cases there is
a heavy seasonality. Concessional co-payments seem to have higher values
in the months of February to early April. However, we can observe the
opposite happening for the safety net during the same months,as they are
both experiencing their lowest values.
autoplot(us_gasoline,Barrels)
gg_season(us_gasoline,Barrels)
gg_subseries(us_gasoline,Barrels)
gg_lag(us_gasoline,Barrels)
ACF(us_gasoline,Barrels)%>%
autoplot()