setwd("~/Business Forcasting")
options(digits = 3, scipen = 99999, stringsAsFactors = FALSE)
remove(list = ls())
graphics.off()
#load libraries
suppressWarnings({
suppressPackageStartupMessages({
library(tidyverse)
library(pdfetch)
library(lubridate)
library(ggplot2)
library(forecast)
library(tsbox)
library(tsibble)
#library(timetk)
library(TSstudio)
library(rio)
library(pdfetch)
library(tidyr)
library(stringr)
library(dygraphs)
library(quantmod)
library(broom)
})
})Quarto
Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.
Running Code
When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
#FINAL PROJECT
# Gather the following economics activity series for the state of Connecticut:
# Real Trade Weighted Value of the dollar for Connecticut
# Total Nonfarm Employees in Connecticut (CTNA)
# Average Hourly Earnings, All Employees, Total Private, Connecticut
# (SMU09000000500000003)
# Average Weekly Hours of All Employees, Total Private Connecticut
#(SMU09000000500000002)
# Assemble them into an equally weighted “Performance Index” for Connecticut;
# BEFORE you create the index make sure you standardize (or normalize) the series.
# Conduct a 12 month forecast using triple exponential smoothing.
# Provide succinct commentary on the economic environment expected for CT.
# Assemble your work in a Quarto document.
# Submit the Quarto document html to the bucket labeled “Final Project”
# by April 30 COB.
#========================== #
# QED ============
#You can add options to executable code like this
Rtrade <- getSymbols("RTWVDCT684NMFRBDAL",src="FRED",auto.assign=FALSE)
ts_info(Rtrade) The Rtrade series is a xts object with 1 variable and 426 observations
Frequency: monthly
Start time: 1988-01-01
End time: 2023-06-01
Nfemp <- getSymbols("CTNA",src="FRED",auto.assign=FALSE)
ts_info(Nfemp) The Nfemp series is a xts object with 1 variable and 411 observations
Frequency: monthly
Start time: 1990-01-01
End time: 2024-03-01
Earn <- getSymbols("SMU09000000500000003",src="FRED",auto.assign=FALSE)
ts_info(Earn) The Earn series is a xts object with 1 variable and 207 observations
Frequency: monthly
Start time: 2007-01-01
End time: 2024-03-01
Time <- getSymbols("SMU09000000500000002",src="FRED",auto.assign=FALSE)
ts_info(Time) The Time series is a xts object with 1 variable and 207 observations
Frequency: monthly
Start time: 2007-01-01
End time: 2024-03-01
# Assemble them into an equally weighted “Performance Index” for Connecticut;
Rtrade.ts = ts(Rtrade, start = c(1988,1), frequency = 12)
Rtrade.ts = window(Rtrade.ts, start = c(2007,1), end =c(2023,6), frequency = 12)
Nfemp.ts = ts(Nfemp, start = c(1990,1), frequency = 12)
Nfemp.ts = window(Nfemp.ts, start = c(2007,1), end =c(2023,6), frequency = 12)
Earn.ts = ts(Earn, start = c(2007,1), frequency = 12)
Earn.ts = window(Earn.ts, start = c(2007,1), end =c(2023,6), frequency = 12)
Time.ts = ts(Time, start = c(2007,1), frequency = 12)
Time.ts = window(Time.ts, start = c(2007,1), end =c(2023,6), frequency = 12)ts_info(Rtrade.ts) The Rtrade.ts series is a ts object with 1 variable and 198 observations
Frequency: 12
Start time: 2007 1
End time: 2023 6
ts_info(Nfemp.ts) The Nfemp.ts series is a ts object with 1 variable and 198 observations
Frequency: 12
Start time: 2007 1
End time: 2023 6
ts_info(Earn.ts) The Earn.ts series is a ts object with 1 variable and 198 observations
Frequency: 12
Start time: 2007 1
End time: 2023 6
ts_info(Time.ts) The Time.ts series is a ts object with 1 variable and 198 observations
Frequency: 12
Start time: 2007 1
End time: 2023 6
cbind(Rtrade.ts, Nfemp.ts,Earn.ts, Time.ts) |> tidy() |>
group_by(series) |>
dplyr::mutate(normindex = value*100/first(value),
standvalues = value/max(value)) |>
ggplot(aes(x = index, y = normindex, col =series)) + geom_line()#standardize the variable
cbind(Rtrade.ts, Nfemp.ts,Earn.ts, Time.ts) |> tidy() |>
group_by(series) |>
dplyr::mutate(normindex = value*100/first(value),
standvalues = value/max(value)) |>
ggplot(aes(x = index, y = standvalues, col =series)) + geom_line()#standardize the variable
mydat = cbind.data.frame(Rtrade.ts/max(Rtrade.ts),
Nfemp.ts/max(Nfemp.ts),
Earn.ts/max(Earn.ts),
Time.ts/max(Time.ts)
)
mydat = mydat |> dplyr::mutate(CTIndex = rowSums(mydat))
head(mydat,3) RTWVDCT684NMFRBDAL CTNA SMU09000000500000003 SMU09000000500000002 CTIndex
1 0.780 0.987 0.718 0.980 3.47
2 0.778 0.986 0.729 0.968 3.46
3 0.777 0.986 0.722 0.977 3.46
mydat.ts = ts(mydat$CTIndex, start = c(2007,1), frequency = 12)
mydat.ts |> tidy() |>
ggplot(aes(x = index, y = value)) + geom_line()#Forecast
mymodel = HoltWinters(mydat.ts)
myfc = forecast(mymodel, h = 12)
autoplot(myfc)autoplot(mydat.ts) + autolayer(myfc$mean) +
xlim(2022, 2026) + ylim(3.7,4.0)Scale for x is already present.
Adding another scale for x, which will replace the existing scale.