This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data %>% | |
ggplot(aes(x=t, y=sales))+ | |
geom_line(col = 'light blue')+ | |
geom_point()+ | |
xlab('Time')+ | |
ylab('Sales')+ | |
theme_minimal() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ggplot(data, aes(x=t, y=sales), col='light blue')+ | |
geom_point()+ | |
stat_smooth(method="lm",col="red") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')+ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dfOut %>% | |
ggplot(aes(x=t, y=S))+ | |
geom_line(col="light blue")+ | |
geom_point(col="black")+ | |
xlab('Time')+ | |
ylab('Seasonality')+ | |
theme_minimal() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))){ |
OlderNewer