Don’t forget to “Save a Permanent Copy” of this project first!!!!!!
Please complete the following R programs and answer each of the questions based on its relevant R output. After you complete this assignment, replace “FirstName LastName” for author with your name and knit the file into HTML and choose “open in Browser”. After that, you need to save the HTML file into a PDF file. In Google Chrome, you do it by choosing “Print…”-> setting “Destination” as “Save as PDF”-> Click “Save”.
Note: We don’t want to knit this R Notebook into PDF
directly. This is because if we do so, the generated pdf file will not
contain those interactive data visualizations produced by different
functions in the TSstudio package for this
dataset.
Install the following group of needed packages and load them into the
current R session. After the first installation, you can comment out the
R command by adding # in front of
install.packages() so that you don’t have to repeatedly
install it. This is also suggested for all the R packages you need to
install in rest of this class.
install.packages(c("TTR", "zoo", "dplyr", "sqldf", "TSstudio", "ggplot2", "hrbrthemes", "readr")) #
# After the first installation, you can comment out the install.packages() command by adding # in front so that you don't have to repeatedly install it.
library(TTR)
library(zoo)
library(dplyr)
library(sqldf)
## Warning in fun(libname, pkgname): couldn't connect to display ":0"
library(TSstudio)
library(ggplot2)
library(hrbrthemes)
library(readr)
Import the MealOrders data into the current R session.
Explore the first 10 records in the dataset. The data contain
information about the number of orders of different cuisine categories
for a meal delivery company. The “cuisine” column includes the four
different cuisine categories: Continental, Indian, Italian, and
Thai.
MealOrders <- read_csv("https://filedn.com/lJpzjOtA91quQEpwdrgCvcy/PredictiveAnalyticsandBusinessForecasting/MealOrders.csv")
head(MealOrders, 10)
The R code in the following chunk extract the total number of orders
of all the other three cuisines (except for Thai cuisine) in each week
and put the data into a new dataframe called others.
others<-sqldf("select distinct week as week,
sum(num_orders) as other_orders
from MealOrders where cuisine != 'Thai'
group by week")
head(others, 10)
Note: Use the example given in the section of Lab 2: The Moving Average Model(s) for Trend Identification as reference to complete the following R code chunks.
Complete the R code in the following chunk to create a 1-lagged
version of the other_orders column in the dataframe
others.
other_orders.lag<-lag(others$other_orders)
Complete R code in the following chunk to build a simple MA(13) and a
simple MA(52) model respectively and add the moving averages as two new
columns named orderma13 and orderma52 into the
data frame others.
others$orderma13=SMA(other_orders.lag, n = 13)
others$orderma52=SMA(other_orders.lag, n = 52)
Run the code in following chunk to visualize the order data.
ggplot(others) +
geom_line(aes(x=week, y=other_orders), color="black")+
geom_point(aes(x=week, y=other_orders), color="black")+
geom_line(aes(x=week, y=orderma13), color="red")+
geom_text(x=10, y=290000, label="MA(13)", size=3, color="red")+
geom_line(aes(x=week, y=orderma52), color="blue")+
geom_text(x=10, y=250000, label="MA(52)", size=3, color="blue")+
xlab("Week") +
ylab("Total Meal Orders") +
ggtitle("Weekly Total Meal Orders of Other Cuisines")
The R code in the following chunk extracts total number of orders of
Thai cuisine in each week and put the data into a new data frame called
Thai.
Thai<-sqldf("select distinct week as week,
sum(num_orders) as Thai_orders
from MealOrders where cuisine = 'Thai'
group by week") #
head(Thai,10)
Complete the R code in the following chunk to create a 1-lagged
version of the Thai_orders column in the data frame
Thai.
Thai_orders.lag<-lag(Thai$Thai_orders)
Complete the R code in the following chunk to build a MA(13) and a
MA(52) model respectively and add the moving averages as two new columns
named orderma13 and orderma52 into the data
frame Thai.
Thai$orderma13=SMA(Thai_orders.lag, n = 13)
Thai$orderma52=SMA(Thai_orders.lag, n = 52)
Run the code in following chunk to visualize the order data.
ggplot(Thai) +
geom_line(aes(x=week, y=Thai_orders), color="black")+
geom_point(aes(x=week, y=Thai_orders), color="black")+
geom_line(aes(x=week, y=orderma13), color="red")+
geom_text(x=10, y=135000, label="MA(13)", size=3, color="red")+
geom_line(aes(x=week, y=orderma52), color="blue")+
geom_text(x=10, y=120000, label="MA(52)", size=3, color="blue")+
xlab("Week") +
ylab("Thai Meal Orders") +
ggtitle("Weekly Total Meal Orders of Thai Cuisine and MA(12)")
Question 1: Given the nature of this dataset (i.e., weekly data), why are MA(13) and MA(52) both reasonable models for identifying the trend component of this time series order data.
MA(13) shows quarterly while MA(52) will show annual.
Question 2: Based on your data analysis, briefly comment on observed weekly total meal orders of all other cuisines by comparing its 13-week and 52-week moving average curves in the most recent 10 weeks.
The overall the most recent 10 weeks shows an overall decline whether looking at 13-week or 52-week.
Question 3: Also comment on observed weekly meal orders of Thai cuisine by comparing its 13-week and 52-week moving average curves in the most recent 10 weeks.
The 52-week moving average shows a slight decline while a 13-week MA shows a more rapid decline.
Question 4: Based on the above data analysis results, what suggestion may you provide to the company on purchasing raw food materials for Thai cuisine vs for other cuisines in the next 10 weeks?
It is possible that the next 10 weeks may go up for Thai Cuisine, however it is not 100 percent certain. Given the data with all orders, it is likely it will continue to go down.