Skip to content

Instantly share code, notes, and snippets.

View omartinez182's full-sized avatar

Eduardo Martinez omartinez182

  • Duke University
  • Durham, NC
View GitHub Profile
@omartinez182
omartinez182 / time-series-analysis-s-1.R
Created June 19, 2020 18:01
Understanding Time Series Analysis with R - Snippet 1
x<-seq(1,20)
y<-c(2.8, 2.1, 4, 4.5, 3.8, 3.2, 4.8, 5.4, 4, 3.6, 5.5, 5.8, 4.3, 3.9, 6, 6.4, NA, NA, NA, NA)
data <- data.frame(t=x, sales=y)
data
@omartinez182
omartinez182 / time-series-analysis-s-2.R
Created June 19, 2020 18:05
Understanding Time Series Analysis with R - Snippet 2
data %>%
ggplot(aes(x=t, y=sales))+
geom_line(col = 'light blue')+
geom_point()+
xlab('Time')+
ylab('Sales')+
theme_minimal()
@omartinez182
omartinez182 / time-series-analysis-s-3.R
Created June 19, 2020 18:06
Understanding Time Series Analysis with R - Snippet 3
ggplot(data, aes(x=t, y=sales), col='light blue')+
geom_point()+
stat_smooth(method="lm",col="red")
@omartinez182
omartinez182 / time-series-analysis-s-4.R
Created June 19, 2020 18:08
Understanding Time Series Analysis with R - Snippet 4
df <- data
Y <- 2 # 2 is the position of our "sales" column in the data frame
k <- 4 # k is equal to the size of the subset
#Array with all of the raw values of 'Y'
arr <- df[, Y]
#Empty array to store the values of the moving averages
MM<-rep(NA, length(arr))
index <- k-1
@omartinez182
omartinez182 / time-series-analysis-s-5.R
Created June 19, 2020 18:09
Understanding Time Series Analysis with R - Snippet 5
dfOut <- data.frame(df, MM=MM)
dfOut %>%
ggplot(aes(x=t, y=sales))+
geom_line(col = 'light blue')+
geom_point()+
geom_line(aes(x=t, y=MM), col = 'orange')+
geom_point(aes(x=t, y=MM), col = 'orange')+
xlab('Time')+
ylab('Sales')+
@omartinez182
omartinez182 / time-series-analysis-s-6.R
Created June 19, 2020 18:11
Understanding Time Series Analysis with R - Snippet 6
df <- data
Y <- 2 #2 is the position of our "sales" column in the data frame
k <- 4 #k is equal to the size of the subset
#Array with all of the raws of 'Y'
arr <- df[, Y]
#Empty array to store the values of the moving averages
MM<-rep(NA, length(arr))
SI <- rep(NA, length(arr))
index <- k-1
@omartinez182
omartinez182 / time-series-analysis-s-7.R
Created June 19, 2020 18:12
Understanding Time Series Analysis with R - Snippet 7
dfOut %>%
ggplot(aes(x=t, y=S))+
geom_line(col="light blue")+
geom_point(col="black")+
xlab('Time')+
ylab('Seasonality')+
theme_minimal()
@omartinez182
omartinez182 / time-series-analysis-s-8.R
Created June 19, 2020 18:12
Understanding Time Series Analysis with R - Snippet 8
#Deseasonality
dfOut <- data.frame(dfOut, DES=(dfOut[ ,2]/dfOut[, 5])) #Y/S
dfOut %>%
ggplot(aes(x=t, y=DES))+
geom_line(col="light blue")+
geom_point(col="black")+
xlab('Time')+
ylab('DES')+
theme_minimal()
@omartinez182
omartinez182 / time-series-analysis-s-9.R
Created June 19, 2020 18:13
Understanding Time Series Analysis with R - Snippet 9
#Linear model
model <- lm(formula = DES ~ t, dfOut)
#Predictions
f <- predict(object = model, newdata = data.frame(t=dfOut[, 1]))
dfOut <- data.frame(dfOut, f=f)
#New prediction accounting for seasonality
predictions <- dfOut[, 7]*dfOut[, 5]
@omartinez182
omartinez182 / time-series-analysis-s-10.R
Created June 19, 2020 18:14
Understanding Time Series Analysis with R - Snippet 10
MultForecast<-function(k, df, Y){
#Array with all of the raw values of 'Y'
arr <- df[, Y]
#Empty array to store the values of the moving averages
MM <- rep(NA, length(arr))
SI <- rep(NA, length(arr))
index <- k-1
#Calculate moving averages + SI
for(i in c(1:length(arr))){