library(fpp3)
library(readxl)
library(ggplot2)
library(USgas)
library(tsibble)
library(dplyr)
library(tsibbledata)
library(tibble)
library(zoo)
# install and load any package necessary
tute1 <- read_excel("C:/Users/natha/Desktop/Forecasting in R/tutedata.xlsx")
head(tute1)
## # A tibble: 6 × 4
## date Sales AdBudget GDP
## <dttm> <dbl> <dbl> <dbl>
## 1 1981-03-01 00:00:00 1020. 659. 252.
## 2 1981-06-01 00:00:00 889. 589 291.
## 3 1981-09-01 00:00:00 795 512. 291.
## 4 1981-12-01 00:00:00 1004. 614. 292.
## 5 1982-03-01 00:00:00 1058. 647. 279.
## 6 1982-06-01 00:00:00 944. 602 254
tute1%>%
mutate(Quarter = yearquarter(date)) %>%
select(-date) %>% as_tsibble( index = Quarter, key = c(Sales, AdBudget, GDP) )
## # A tsibble: 100 x 4 [1Q]
## # Key: Sales, AdBudget, GDP [100]
## Sales AdBudget GDP Quarter
## <dbl> <dbl> <dbl> <qtr>
## 1 735. 507. 287. 1983 Q3
## 2 771. 509. 290. 1994 Q3
## 3 778. 531. 296. 1982 Q3
## 4 783. 500. 288. 1990 Q3
## 5 786. 512. 309. 1997 Q3
## 6 786. 492. 304. 1993 Q3
## 7 792. 490. 301. 1984 Q3
## 8 792. 498. 283. 2001 Q3
## 9 793. 504. 301. 2000 Q3
## 10 795 512. 291. 1981 Q3
## # … with 90 more rows
ggplot(data = tute1, aes(x = date, y = GDP)) + geom_line() + labs(x = "Year", y = "USD", title = "GDP")
ggplot(data = tute1, aes(x = date, y = Sales)) + geom_line() + labs(x = "Year", y = "USD", title = "Sales")
ggplot(data = tute1, aes(x = date, y = AdBudget)) + geom_line() + labs(x = "Year", y = "USD", title = "AdBudget")
tute1%>%
pivot_longer(c(Sales, AdBudget, GDP), values_to = "USD")%>%
ggplot(aes(x = date, y= USD))+ geom_line()+facet_grid(name ~ ., scales = "free_y")+labs(title = "All Together", y = "USD", x = "Quarter")
us_total%>%
as_tsibble(index = year, key = c(state))
## # A tsibble: 1,266 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,256 more rows
us_total%>%
filter(state== "Maine" | state== "Vermont" | state== "New Hampshire" | state== "Massachusetts" | state== "Connecticut" | state== "Rhode Island")%>%
ggplot(aes(x = year, y= y))+ geom_line()+labs(title = "US Natural Gas Consumption by State", y = "Natural Gas Consumped", x = "Year")
tourism <- readxl::read_excel("C:/Users/natha/Desktop/Forecasting in R/tourism1.xlsx")
tourism_mine <- tourism %>% mutate(Quarter = yearquarter(Quarter) ) %>%
as_tsibble(index = Quarter, key = c(Region, State, Purpose))
tourism_mine
## # 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
tourism_mine %>% 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.
tourism_mine2 <- tourism_mine %>%
group_by(State) %>% summarise(Trips = sum(Trips))%>%
ungroup()
tourism_mine2
## # 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_arrivals
## # A tsibble: 508 x 3 [1Q]
## # Key: Origin [4]
## Quarter Origin Arrivals
## <qtr> <chr> <int>
## 1 1981 Q1 Japan 14763
## 2 1981 Q2 Japan 9321
## 3 1981 Q3 Japan 10166
## 4 1981 Q4 Japan 19509
## 5 1982 Q1 Japan 17117
## 6 1982 Q2 Japan 10617
## 7 1982 Q3 Japan 11737
## 8 1982 Q4 Japan 20961
## 9 1983 Q1 Japan 20671
## 10 1983 Q2 Japan 12235
## # … with 498 more rows
aus_arrivals%>%
autoplot(Arrivals)
aus_arrivals%>%
gg_season(Arrivals)
aus_arrivals%>%
gg_subseries(Arrivals)
set.seed(876543)
seedseries <- aus_retail %>%
filter(`Series ID` == sample(aus_retail$`Series ID`,1))
autoplot(seedseries, Turnover)
gg_season(seedseries, Turnover)
gg_subseries(seedseries, Turnover)
gg_lag(seedseries, Turnover)
seedseries %>% ACF(Turnover) %>% autoplot()
###There is a upward trend and a seasonal uptrend in December.
fb <- gafa_stock%>%
filter(Symbol == "FB")%>%
summarise(Adj_Close = mean(Adj_Close))%>%
summarise(Adj_Close = sd(Adj_Close))
SO <- read_excel("C:/Users/natha/Desktop/Forecasting in R/SOYTD.xlsx")
SO%>%
select(-Open)%>%
select(-Close)%>%
select(-High)%>%
select(-Low)%>%
select(-Volume)
## # A tibble: 251 × 2
## Date `Adj Close`
## <dttm> <dbl>
## 1 2021-09-13 00:00:00 63.8
## 2 2021-09-14 00:00:00 63.5
## 3 2021-09-15 00:00:00 63.5
## 4 2021-09-16 00:00:00 62.9
## 5 2021-09-17 00:00:00 61.7
## 6 2021-09-20 00:00:00 61.5
## 7 2021-09-21 00:00:00 61.4
## 8 2021-09-22 00:00:00 61.2
## 9 2021-09-23 00:00:00 61.0
## 10 2021-09-24 00:00:00 60.8
## # … with 241 more rows