library(ggplot2)
library(tidyquant)
## Warning: package 'tidyquant' was built under R version 4.4.2
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Warning: package 'xts' was built under R version 4.4.2
## Warning: package 'PerformanceAnalytics' was built under R version 4.4.2
## ── Attaching core tidyquant packages ─────────────────────── tidyquant 1.0.10 ──
## ✔ PerformanceAnalytics 2.0.8 ✔ TTR 0.24.4
## ✔ quantmod 0.4.26 ✔ xts 0.14.1
## ── Conflicts ────────────────────────────────────────── tidyquant_conflicts() ──
## ✖ zoo::as.Date() masks base::as.Date()
## ✖ zoo::as.Date.numeric() masks base::as.Date.numeric()
## ✖ PerformanceAnalytics::legend() masks graphics::legend()
## ✖ quantmod::summary() masks base::summary()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(rstatix)
##
## Attaching package: 'rstatix'
##
## The following object is masked from 'package:stats':
##
## filter
library(lubridate)
## Warning: package 'lubridate' was built under R version 4.4.2
##
## Attaching package: 'lubridate'
##
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(forecast)
library(dplyr)
##
## ######################### Warning from 'xts' package ##########################
## # #
## # The dplyr lag() function breaks how base R's lag() function is supposed to #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or #
## # source() into this session won't work correctly. #
## # #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop #
## # dplyr from breaking base R's lag() function. #
## # #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning. #
## # #
## ###############################################################################
##
## Attaching package: 'dplyr'
##
## The following objects are masked from 'package:xts':
##
## first, last
##
## The following objects are masked from 'package:stats':
##
## filter, lag
##
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)
data("airquality")
df = airquality
df |> freq_table(5)
## # A tibble: 5 × 3
## Month n prop
## <int> <int> <dbl>
## 1 5 31 20.3
## 2 6 30 19.6
## 3 7 31 20.3
## 4 8 31 20.3
## 5 9 30 19.6
length(df$Month)
## [1] 153
library(zoo)
head(df)
## Ozone Solar.R Wind Temp Month Day
## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 NA NA 14.3 56 5 5
## 6 28 NA 14.9 66 5 6
# Moving average with K = 3
colnames(df)
## [1] "Ozone" "Solar.R" "Wind" "Temp" "Month" "Day"
df <- df |> mutate(
temp3 =rollmean(Temp, k=4,
fill=NA,
align = "right")
)
## Show results
head(df, 4)
## Ozone Solar.R Wind Temp Month Day temp3
## 1 41 190 7.4 67 5 1 NA
## 2 36 118 8.0 72 5 2 NA
## 3 12 149 12.6 74 5 3 NA
## 4 18 313 11.5 62 5 4 68.75
df$No = 1:nrow(df)
ggplot(df, aes(No, Temp)) +
geom_line()+
geom_ma(ma_fun = SMA, n=4, color="green")+
geom_ma(ma_fun = SMA, n=10, color="red")

library(forecast)
colnames(df)
## [1] "Ozone" "Solar.R" "Wind" "Temp" "Month" "Day" "temp3"
## [8] "No"
df |> freq_table(5)
## # A tibble: 5 × 3
## Month n prop
## <int> <int> <dbl>
## 1 5 31 20.3
## 2 6 30 19.6
## 3 7 31 20.3
## 4 8 31 20.3
## 5 9 30 19.6
nrow(df)
## [1] 153
## Change datat to times series
myts <- ts(df$Temp, start=c(2017, 1), end=c(2024, 12), frequency=12)
summary(myts)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 56.00 68.75 79.00 76.42 83.25 93.00
##
# Fit an ARIMA model to the data
model <- auto.arima(myts)
# Generate forecasts for the next 10 time points
forecast_values <- forecast(model, h = 10)
plot(forecast_values)

library(ggfortify)
## Warning: package 'ggfortify' was built under R version 4.4.2
## Registered S3 methods overwritten by 'ggfortify':
## method from
## autoplot.Arima forecast
## autoplot.acf forecast
## autoplot.ar forecast
## autoplot.bats forecast
## autoplot.decomposed.ts forecast
## autoplot.ets forecast
## autoplot.forecast forecast
## autoplot.stl forecast
## autoplot.ts forecast
## fitted.ar forecast
## fortify.ts forecast
## residuals.ar forecast
autoplot(forecast_values)
