Email : jirene113@gmail.com
Instagram : https://www.instagram.com/irenegani
RPubs : https://rpubs.com/irenegani/
Department : Business Statistics
Address : ARA Center, Matana University Tower
Jl. CBD Barat Kav, RT.1, Curug Sangereng, Kelapa Dua, Tangerang, Banten 15810.
## Installing package into 'C:/Users/irene/OneDrive/Documents/R/win-library/4.0'
## (as 'lib' is unspecified)
## Warning: unable to access index for repository http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/4.0:
## cannot open URL 'http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/4.0/PACKAGES'
## package 'tidyverse' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\irene\AppData\Local\Temp\RtmpGu8YKM\downloaded_packages
##
## tidyverse installed
## Warning: package 'tidyverse' was built under R version 4.0.5
## Warning in pacman::p_load(fpp3, tidyverse, tsibble, fable, feasts, tsibbledata): Failed to install/load:
## tidyverse
gafa_stock, PBS, vic_elec and peltrepresent.## starting httpd help server ... done
gafa_stock is a time series of stock prices in $USD from 2014-2018 for Apple, Amazon, Facebook and Google.
PBS is a time series of Australian monthly medicare with two values which is Scripts and Cost.
vic_elec is a time series of half-hourly electricity demand for Victoria, Australia with three values which is Demand, Temperature, and Holiday.
gafa_stock = 1 Day PBS = 1 Month vic_elec = 30 Minutes pelt = 1 Year ### gafa_stock The time interval of gafa_stock is 1 day.
PBS has no fix time interval.
The time interval of vic_elec is 30 minutes.
The time interval of pelt is 1 year.
filter() to find what days corresponded to the peak closing price for each of the four stocks in gafa_stock.## # A tsibble: 4 x 8 [!]
## # Key: Symbol [4]
## # Groups: Symbol [4]
## Symbol Date Open High Low Close Adj_Close Volume
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAPL 2018-10-03 230. 233. 230. 232. 230. 28654800
## 2 AMZN 2018-09-04 2026. 2050. 2013 2040. 2040. 5721100
## 3 FB 2018-07-25 216. 219. 214. 218. 218. 58954200
## 4 GOOG 2018-07-26 1251 1270. 1249. 1268. 1268. 2405600
timeseries %>%
pivot_longer(-Quarter) %>%
ggplot(aes(x = Quarter, y = value, colour = name))+
geom_line()+
facet_grid(name ~ ., scales = "free_y") ### Check what happens when you don’t include facet_grid().
timeseries %>%
pivot_longer(-Quarter) %>%
ggplot(aes(x = Quarter, y = value, colour = name))+
geom_line()include facet_grid(), all three sets are plotted on one graph rather than 3 different ones with the same x-axis values. Because the range of values differs significantly for each set, it makes it difficult to see patterns in individual sets. All the graphs are plotted on one plane, which makes reading all three a little difficult.
USgas package contains data on the demand for natural gas in the US.USgas package.## Warning: package 'USgas' was built under R version 4.0.5
us_total with year as the index and state as the key.## # A tsibble: 1,265 x 3 [1Y]
## # Key: state [53]
## year state y
## <int> <chr> <int>
## 1 1997 Alabama 324158
## 2 1998 Alabama 329134
## 3 1999 Alabama 337270
## 4 2000 Alabama 353614
## 5 2001 Alabama 332693
## 6 2002 Alabama 379343
## 7 2003 Alabama 350345
## 8 2004 Alabama 382367
## 9 2005 Alabama 353156
## 10 2006 Alabama 391093
## # ... with 1,255 more rows
tsibble_tourism <- tourism %>% mutate(Quarter = yearquarter(Quarter) ) %>%
as_tsibble(index = Quarter, key = c(Region, State, Purpose))
tsibble_tourism## # A tsibble: 24,320 x 5 [1Q]
## # Key: Region, State, Purpose [304]
## Quarter Region State Purpose Trips
## <qtr> <chr> <chr> <chr> <dbl>
## 1 1998 Q1 Adelaide South Australia Business 135.
## 2 1998 Q2 Adelaide South Australia Business 110.
## 3 1998 Q3 Adelaide South Australia Business 166.
## 4 1998 Q4 Adelaide South Australia Business 127.
## 5 1999 Q1 Adelaide South Australia Business 137.
## 6 1999 Q2 Adelaide South Australia Business 200.
## 7 1999 Q3 Adelaide South Australia Business 169.
## 8 1999 Q4 Adelaide South Australia Business 134.
## 9 2000 Q1 Adelaide South Australia Business 154.
## 10 2000 Q2 Adelaide South Australia Business 169.
## # ... with 24,310 more rows
tsibble_tourism %>% group_by(Region, Purpose) %>%
summarise(Trips = mean(Trips)) %>%
ungroup() %>%
filter(Trips == max(Trips))## # A tsibble: 1 x 4 [1Q]
## # Key: Region, Purpose [1]
## Region Purpose Quarter Trips
## <chr> <chr> <qtr> <dbl>
## 1 Melbourne Visiting 2017 Q4 985.
new_tsibble <- tsibble_tourism %>%
group_by(State) %>% summarise(Trips = sum(Trips))%>%
ungroup()
new_tsibble## # A tsibble: 640 x 3 [1Q]
## # Key: State [8]
## State Quarter Trips
## <chr> <qtr> <dbl>
## 1 ACT 1998 Q1 551.
## 2 ACT 1998 Q2 416.
## 3 ACT 1998 Q3 436.
## 4 ACT 1998 Q4 450.
## 5 ACT 1999 Q1 379.
## 6 ACT 1999 Q2 558.
## 7 ACT 1999 Q3 449.
## 8 ACT 1999 Q4 595.
## 9 ACT 2000 Q1 600.
## 10 ACT 2000 Q2 557.
## # ... with 630 more rows
aus_production
## Warning: Removed 20 row(s) containing missing values (geom_path).
pelt
gafa_stock
vic_elec
aus_arrivals data set comprises quarterly international arrivals to Australia from Japan, New Zealand, UK and the US.datatable(aus_arrivals,
caption = htmltools::tags$caption(
style = 'caption-side: bottom; text-align: center;',
htmltools::em('data set comprises quarterly international arrivals to Australia from Japan, New Zealand, UK and the US')),
extensions = 'FixedColumns',
option = list(scrollX = TRUE, fixedColumns = TRUE)
)autoplot(), gg_season() and gg_subseries() to compare the differences between the arrivals from these four countries.From information given above, we can see Japanese arrivals to Australia is greatly decreased. After exploring some information in google on wikipedia, what i got is on March 2007 Australia and Japan signed a joint security pact. The scope of security cooperation includes:
Law enforcement on combating transnational crime, including trafficking in illegal narcotics and precursors, people smuggling and trafficking, counterfeiting currency and arms smuggling Border security Counter-terrorism Disarmament and counter-proliferation of weapons of mass destruction and their means of delivery Peace operations Exchange of strategic assessments and related information Maritime and aviation security Humanitarian relief operations, including disaster relief Contingency planning, including for pandemics
set.seed(7777777)
seedseries <- aus_retail %>%
filter(`Series ID` == sample(aus_retail$`Series ID`,1))From the autoplot, we can see a clear seasonal or cyclic pattern in the time series, and a upward trend.
The seasonal plot shows that there are indeed seasonal patterns. The plot also reveals that there is a typical big jump every year in December, and a drop in February. Sales begin to increase in the fall, peaking between November and December, then decreasing after January, likely to coincide with holiday shopping and sales for Christmas.
The seasonal subseries offers a new perspective on seasonality by showing the monthly mean values. We see a large increase from November to December and a decrease from December to February, but also a small, decreasing trend in turnover from January to June and a similar increase from July to November, before the big spike from November to December.
autoplot(), gg_season(), gg_subseries(), gg_lag(), ACF() and explore features from the following time series: “Total Private” Employed from us_employment, Bricks from aus_production, Hare from pelt, “H02” Cost from PBS, and us_gasoline.## # A tsibble: 29 x 3 [1M]
## # Key: Series_ID [1]
## Series_ID lag acf
## <chr> <lag> <dbl>
## 1 CEU0500000001 1M 0.997
## 2 CEU0500000001 2M 0.993
## 3 CEU0500000001 3M 0.990
## 4 CEU0500000001 4M 0.986
## 5 CEU0500000001 5M 0.983
## 6 CEU0500000001 6M 0.980
## 7 CEU0500000001 7M 0.977
## 8 CEU0500000001 8M 0.974
## 9 CEU0500000001 9M 0.971
## 10 CEU0500000001 10M 0.968
## # ... with 19 more rows
## Warning: Removed 20 row(s) containing missing values (geom_path).
## Warning: Removed 20 row(s) containing missing values (geom_path).
## Warning: Removed 5 row(s) containing missing values (geom_path).
## Warning: Removed 20 rows containing missing values (gg_lag).
## # A tsibble: 22 x 2 [1Q]
## lag acf
## <lag> <dbl>
## 1 1Q 0.900
## 2 2Q 0.815
## 3 3Q 0.813
## 4 4Q 0.828
## 5 5Q 0.720
## 6 6Q 0.642
## 7 7Q 0.655
## 8 8Q 0.692
## 9 9Q 0.609
## 10 10Q 0.556
## # ... with 12 more rows
## # A tsibble: 19 x 2 [1Y]
## lag acf
## <lag> <dbl>
## 1 1Y 0.658
## 2 2Y 0.214
## 3 3Y -0.155
## 4 4Y -0.401
## 5 5Y -0.493
## 6 6Y -0.401
## 7 7Y -0.168
## 8 8Y 0.113
## 9 9Y 0.307
## 10 10Y 0.340
## 11 11Y 0.296
## 12 12Y 0.206
## 13 13Y 0.0372
## 14 14Y -0.153
## 15 15Y -0.285
## 16 16Y -0.295
## 17 17Y -0.202
## 18 18Y -0.0676
## 19 19Y 0.0956
## # A tsibble: 92 x 6 [1M]
## # Key: Concession, Type, ATC1, ATC2 [4]
## Concession Type ATC1 ATC2 lag acf
## <chr> <chr> <chr> <chr> <lag> <dbl>
## 1 Concessional Co-payments H H02 1M 0.834
## 2 Concessional Co-payments H H02 2M 0.679
## 3 Concessional Co-payments H H02 3M 0.514
## 4 Concessional Co-payments H H02 4M 0.352
## 5 Concessional Co-payments H H02 5M 0.264
## 6 Concessional Co-payments H H02 6M 0.219
## 7 Concessional Co-payments H H02 7M 0.253
## 8 Concessional Co-payments H H02 8M 0.337
## 9 Concessional Co-payments H H02 9M 0.464
## 10 Concessional Co-payments H H02 10M 0.574
## # ... with 82 more rows
## # A tsibble: 31 x 2 [1W]
## lag acf
## <lag> <dbl>
## 1 1W 0.893
## 2 2W 0.882
## 3 3W 0.873
## 4 4W 0.866
## 5 5W 0.847
## 6 6W 0.844
## 7 7W 0.832
## 8 8W 0.831
## 9 9W 0.822
## 10 10W 0.808
## # ... with 21 more rows
+ 1 with B + 2 with A + 3 with D + 4 with C # The aus_livestock data contains the monthly total number of pigs slaughtered in Victoria, Australia, from Jul 1972 to Dec 2018. Use filter() to extract pig slaughters in Victoria between 1990 and 1995. Use autoplot() and ACF() for this data. How do they differ from white noise? If a longer period of data is used, what difference does it make to the ACF?
Victoria_Pig<- aus_livestock %>%
filter(State == "Victoria",
Animal == "Pigs",
between(year(Month),1990,1995))
Victoria_Pig %>% ACF(Count) %>% autoplot()The spikes are almost all outside bounds, meaning that the set is not white noise.
dgoog <- gafa_stock %>%
filter(Symbol == "GOOG", year(Date) >= 2018) %>%
mutate(trading_day = row_number()) %>%
update_tsibble(index = trading_day, regular = TRUE) %>%
mutate(diff = difference(Close))Since the interval for GOOG by Date would be chaotic and not the same for each row when we use a filter for the GOOG symbol, we need to change our index to use the row sequence number so that the interval for each row is the same.