list_packages <- c("forecast", "readxl", "stargazer", "fpp", 
                   "fpp2", "scales", "quantmod", "urca",
                   "vars", "tseries", "ggplot2", "dplyr", 'tidyverse', 'lubridate', 'scales')
new_packages <- list_packages[!(list_packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)

# Load necessary packages
lapply(list_packages, require, character.only = TRUE)
## [[1]]
## [1] TRUE
## 
## [[2]]
## [1] TRUE
## 
## [[3]]
## [1] TRUE
## 
## [[4]]
## [1] TRUE
## 
## [[5]]
## [1] TRUE
## 
## [[6]]
## [1] TRUE
## 
## [[7]]
## [1] TRUE
## 
## [[8]]
## [1] TRUE
## 
## [[9]]
## [1] TRUE
## 
## [[10]]
## [1] TRUE
## 
## [[11]]
## [1] TRUE
## 
## [[12]]
## [1] TRUE
## 
## [[13]]
## [1] TRUE
## 
## [[14]]
## [1] TRUE
## 
## [[15]]
## [1] TRUE
sales <- read_excel("/Users/saurabh/Downloads/Time Series /Final Project/global_superstore_2016.xlsx", sheet = 'Orders')
head(sales)
## # A tibble: 6 x 24
##   `Row ID` `Order ID` `Order Date`        `Ship Date`         `Ship Mode`
##      <dbl> <chr>      <dttm>              <dttm>              <chr>      
## 1    40098 CA-2014-A… 2014-11-11 00:00:00 2014-11-13 00:00:00 First Class
## 2    26341 IN-2014-J… 2014-02-05 00:00:00 2014-02-07 00:00:00 Second Cla…
## 3    25330 IN-2014-C… 2014-10-17 00:00:00 2014-10-18 00:00:00 First Class
## 4    13524 ES-2014-K… 2014-01-28 00:00:00 2014-01-30 00:00:00 First Class
## 5    47221 SG-2014-R… 2014-11-05 00:00:00 2014-11-06 00:00:00 Same Day   
## 6    22732 IN-2014-J… 2014-06-28 00:00:00 2014-07-01 00:00:00 Second Cla…
## # … with 19 more variables: `Customer ID` <chr>, `Customer Name` <chr>,
## #   Segment <chr>, `Postal Code` <dbl>, City <chr>, State <chr>,
## #   Country <chr>, Region <chr>, Market <chr>, `Product ID` <chr>,
## #   Category <chr>, `Sub-Category` <chr>, `Product Name` <chr>,
## #   Sales <dbl>, Quantity <dbl>, Discount <dbl>, Profit <dbl>, `Shipping
## #   Cost` <dbl>, `Order Priority` <chr>

Wrangling ->Dates ->Aggregate to Country - Store - Month level Country - Product - Month level Country - Month level ->Drop unnecessary columns keep only one size

EDA -> Time series pattern by countries -> Heat map by stores; sizes

cat('Number of rows:', nrow(sales), '\n')
## Number of rows: 51290
cat('Columns names are :- \n')
## Columns names are :-
#sales <- sales[!duplicated(sales),]
names(sales)
##  [1] "Row ID"         "Order ID"       "Order Date"     "Ship Date"     
##  [5] "Ship Mode"      "Customer ID"    "Customer Name"  "Segment"       
##  [9] "Postal Code"    "City"           "State"          "Country"       
## [13] "Region"         "Market"         "Product ID"     "Category"      
## [17] "Sub-Category"   "Product Name"   "Sales"          "Quantity"      
## [21] "Discount"       "Profit"         "Shipping Cost"  "Order Priority"
sapply(sales, function(x) sum(is.na(x)))
##         Row ID       Order ID     Order Date      Ship Date      Ship Mode 
##              0              0              0              0              0 
##    Customer ID  Customer Name        Segment    Postal Code           City 
##              0              0              0          41296              0 
##          State        Country         Region         Market     Product ID 
##              0              0              0              0              0 
##       Category   Sub-Category   Product Name          Sales       Quantity 
##              0              0              0              0              0 
##       Discount         Profit  Shipping Cost Order Priority 
##              0              0              0              0
sales <- subset(sales, select =  c( `Order Date`, Country, Market, Category, `Sub-Category`, `Product Name`, `Product ID`, Quantity))

names(sales)
## [1] "Order Date"   "Country"      "Market"       "Category"    
## [5] "Sub-Category" "Product Name" "Product ID"   "Quantity"
sales$Year_mon <- format(sales$`Order Date`, '%Y-%m')
sales$Year <- format(sales$`Order Date`, '%Y')
sales$Month <- format(sales$`Order Date`, '%B')
sales$Date <- lubridate::floor_date(sales$`Order Date`, unit = 'month')
Country_filtered <- sales[which(sales$Country %in% c('United States', 'Australia', 'France', 'Mexico', 'Germany', 'China')),]
picked_country <- 'United States'
Country_picked <- sales[which(Country_filtered$Country %in% c(picked_country)),]
# Summing up transactions to a Country - shop - product - gender - size - year - month level
Country_level <- aggregate(cbind( Quantity) ~ Date, data = Country_picked, FUN = sum)
Category_sales <- aggregate(cbind(Quantity) ~ Category + Date, data = Country_picked, FUN = sum)
Sub_Category_sales <- aggregate(cbind(Quantity) ~ Category + `Sub-Category` + Date, data = Country_picked, FUN = sum)
Country_filtered <- sales[which(sales$Country %in% c('United States', 'Australia', 'France', 'Mexico', 'Germany', 'China')),]
Country_picked <- sales[which(Country_filtered$Country %in% c(picked_country)),]
Country_level <- aggregate(cbind( Quantity) ~ Date, data = Country_picked, FUN = sum)
Category_sales <- aggregate(cbind(Quantity) ~ Category + Date, data = Country_picked, FUN = sum)
Sub_Category_sales <- aggregate(cbind(Quantity) ~ Category + `Sub-Category` + Date, data = Country_picked, FUN = sum)

EDA Phase

1. Monthly time series plot

ggplot(Country_level, aes(y = Quantity, x =  Date))  + 
scale_x_datetime(labels = date_format("%b"), date_breaks = '1 month', expand = c(0.05,0.1)) + 
facet_grid(~year(Date), space = "free_x", scales = "free_x", switch='x')+
  geom_point() + geom_line(linetype = 'dashed') +
  ggtitle("Monthly Time Series Plot") +
  theme_bw() +
  theme(
    plot.title = element_text(hjust = 0.5),
    strip.placement = 'outside',
    strip.background = element_rect( colour = "grey50"),
    #panel.spacing = unit(0,'cm'),
    axis.text.x = element_text(angle = 90)
        ) 

Observations from the plot: - * There is a discernable time series pattern. * There is obvious monthly seasonality. * There is a trend component.

2. Monthly Series for each category

ggplot(Category_sales, aes(y = Quantity, x =  Date))  + 
scale_x_datetime(labels = date_format("%b"), date_breaks = '1 month', expand = c(0.05,0.1)) + 
facet_grid(~year(Date), space = "free_x", scales = "free_x", switch='x')+
  geom_point() + geom_line( aes(color = Category,linetype = Category), size = 1) +
  ggtitle("Monthly Time Series Plot by Category") +
  theme_bw() +
  theme(
    plot.title = element_text(hjust = 0.5),
    strip.placement = 'outside',
    strip.background = element_rect( colour = "grey50"),
    #panel.spacing = unit(0,'cm'),
    axis.text.x = element_text(angle = 90)
        ) 

Observations from the plot: - * Office supplies bought are considerably higher in volume than Furniture and Technology. * Office supplies also begin at a similar point in the beginning of the year but rise in volume much faster. * Furniture and technology volumes and patterns are very close to each other.

3. Sub Category heat map

ggplot(Sub_Category_sales, aes(y = `Sub-Category`, x = Date, fill = Quantity)) +
  scale_x_datetime(labels = date_format("%b"), date_breaks = '1 month', expand = c(0,0)) + 
facet_grid(~year(Date), space = "free_x", scales = "free_x", switch='x') +
geom_tile() +
  geom_text(aes(label = Quantity), size  = 3) +
  scale_fill_gradient(low="grey", high="red") +
  ggtitle("Monthly Time Series Heat Map by Sub-Category") +
  theme_bw() +
  theme(
    plot.title = element_text(hjust = 0.5),
    strip.placement = 'outside',
    strip.background = element_rect( colour = "grey50"),
    #panel.spacing = unit(0,'cm'),
    axis.text.x = element_text(angle = 90)
        ) 

Observations from the plot: - * Most sub-categories have the same pattern * Binders and papers seem to be bought in the highest volume.

Forecast Model Identification

Sales_ts <- ts(Country_level$Quantity, frequency = 12, start = c(2012,1), end = c(2015,12))
Sales_ts
##       Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
## 2012  363  397  400  411  489  663  380  734  814  729  720  982
## 2013  404  299  524  476  645  993  514  989  869  820 1151 1080
## 2014  522  474  607  581  962 1329  783 1044 1367  814 1349 1316
## 2015  637  643  863  788  898 1300  668 1362 1572 1310 1836 1606
Sales_train_ts <- ts(Country_level$Quantity[1:36], frequency = 12, start = c(2012,1), end = c(2014,12))
Sales_train_ts
##       Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
## 2012  363  397  400  411  489  663  380  734  814  729  720  982
## 2013  404  299  524  476  645  993  514  989  869  820 1151 1080
## 2014  522  474  607  581  962 1329  783 1044 1367  814 1349 1316
Sales_train_ts %>% ggtsdisplay(lag.max =36)

Observations from the plot :-

adf.test(Sales_train_ts, k =1)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  Sales_train_ts
## Dickey-Fuller = -3.488, Lag order = 1, p-value = 0.06047
## alternative hypothesis: stationary
adf.test(Sales_train_ts, k = 2)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  Sales_train_ts
## Dickey-Fuller = -2.3414, Lag order = 2, p-value = 0.4403
## alternative hypothesis: stationary
adf.test(Sales_train_ts, k = 12)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  Sales_train_ts
## Dickey-Fuller = -2.354, Lag order = 12, p-value = 0.4354
## alternative hypothesis: stationary

Observations * We see that since p-value is > 0.05, we fail to reject the null hypothesis and thus the series is non-stationary

Sales_train_ts %>%diff() %>% ggtsdisplay(lag.max =40)

Observations from the plot :-

# Sales_train_ts %>% diff() %>% diff(lag=12) %>% ggtsdisplay(lag.max=36)
Sales_train_ts %>%diff() %>% diff(lag=12) %>% ggtsdisplay(lag.max=36)

Observations: - * The spike at lag 1 suggests an MR(1) component and the spike at lag 12 suggests a seasonal MR(1) component.

Sales_train_ts %>% log() %>% diff() %>% diff(lag=12) %>% adf.test()
## 
##  Augmented Dickey-Fuller Test
## 
## data:  .
## Dickey-Fuller = -3.5363, Lag order = 2, p-value = 0.05885
## alternative hypothesis: stationary

It is now stationary as the p-value is < 0.05 So when we perform seasonal difference and normal difference, the series is stationary.

Result of model identification

From the exploration, our guess is that the right model to fit this data is an ARIMA(0,1,1)(0,1,1)[12]

Let us check what auto arima tells us: -

auto_fit <- Sales_train_ts %>% auto.arima()
## Warning: The chosen seasonal unit root test encountered an error when testing for the second difference.
## From stl(): series is not periodic or has less than two periods
## 1 seasonal differences will be used. Consider using a different unit root test.
summary(auto_fit)
## Series: . 
## ARIMA(0,0,0)(0,1,0)[12] with drift 
## 
## Coefficients:
##         drift
##       14.1181
## s.e.   2.3527
## 
## sigma^2 estimated as 19961:  log likelihood=-152.36
## AIC=308.72   AICc=309.3   BIC=311.08
## 
## Training set error measures:
##                    ME     RMSE      MAE       MPE     MAPE     MASE
## Training set 0.166133 112.9286 75.21243 -3.785049 10.53844 0.422344
##                    ACF1
## Training set -0.1480112

The difference is that the auto arima model is missing the MR(1) seasonal component

Model Fitting

fit <- Sales_train_ts  %>% Arima( order = c(1,1,2), seasonal = c(0,1,1), include.constant = FALSE, lambda = 0)
summary(fit)
## Series: . 
## ARIMA(1,1,2)(0,1,1)[12] 
## Box Cox transformation: lambda= 0 
## 
## Coefficients:
##           ar1     ma1      ma2     sma1
##       -0.9997  0.0800  -0.8794  -0.5829
## s.e.   0.0031  0.3845   0.3795   0.6176
## 
## sigma^2 estimated as 0.02771:  log likelihood=7.12
## AIC=-4.23   AICc=-0.7   BIC=1.45
## 
## Training set error measures:
##                    ME     RMSE      MAE      MPE     MAPE      MASE
## Training set 22.37088 100.3341 65.73635 2.151166 8.020521 0.3691325
##                      ACF1
## Training set 0.0008944884

Checking terms for significance

-0.6235/0.1787   
## [1] -3.489088
0.2691/0.3991
## [1] 0.6742671

sma1 comes out as not significant as the absolute value of the t-statistic is < 1.96 This is also what the auto arima model suggested to us. Let’s then compare the AICs of the two models.

AIC(fit)
## [1] -4.231914
AIC(auto_fit)
## [1] 308.7243

The (0,1,1)(0,1,0)[12] which the auto arima suggested has a slightly lower AIC

summary(auto_fit)
## Series: . 
## ARIMA(0,0,0)(0,1,0)[12] with drift 
## 
## Coefficients:
##         drift
##       14.1181
## s.e.   2.3527
## 
## sigma^2 estimated as 19961:  log likelihood=-152.36
## AIC=308.72   AICc=309.3   BIC=311.08
## 
## Training set error measures:
##                    ME     RMSE      MAE       MPE     MAPE     MASE
## Training set 0.166133 112.9286 75.21243 -3.785049 10.53844 0.422344
##                    ACF1
## Training set -0.1480112

Checking significance

-0.6097/0.1636
## [1] -3.726773

The term is significant as the absolute value of the test statistic is > 1.96

Residual Diagnostics and Model Adequacy

checkresiduals(fit)

## 
##  Ljung-Box test
## 
## data:  Residuals from ARIMA(1,1,2)(0,1,1)[12]
## Q* = 5.2466, df = 3, p-value = 0.1546
## 
## Model df: 4.   Total lags used: 7

The residuals look good. There are no spikes in the ACF, there are no obvious patterns in the residuals and the residuals are centered around 0.

In the Ljung-Box test, we see that the p-value is > 0.05 which means we cannot reject the null hypothesis. Thus the model is adequate.

Forecasting next 12 months

forecasts <- fit %>% forecast(h = 12)


# MSE and MAE
cat('MSE of the model is:', mean((forecasts$mean[1:12] - Country_level$Quantity[37:48])^2), '\n')
## MSE of the model is: 20956.52
cat('MAE of the model is:', sum(abs(forecasts$mean[1:12] - Country_level$Quantity[37:48])), '\n')
## MAE of the model is: 1443.797
forecasts <- fit %>% forecast(h = 12)
forecast_values <- vector()
forecast_values <- c(forecast_values, rep(NA, 48))
forecast_values[37:48] <- forecasts$mean[1:12]

plotting_dataset <- Country_level

plotting_dataset$Forecasts <- forecast_values
names(plotting_dataset)[2] <- 'Actuals'

ggplot(plotting_dataset, aes(x = Date)) +
  geom_line(aes(y = Actuals)) +
  geom_line(aes(y = Forecasts), color = 'Red', linetype = 'dashed', size = 1)
## Warning: Removed 36 rows containing missing values (geom_path).

forecasts <- auto_fit %>% forecast(h = 12)
forecast_values <- vector()
forecast_values <- c(forecast_values, rep(NA, 48))
forecast_values[37:48] <- forecasts$mean[1:12]

plotting_dataset <- Country_level

plotting_dataset$Forecasts <- forecast_values
names(plotting_dataset)[2] <- 'Actuals'

ggplot(plotting_dataset, aes(x = Date)) +
  geom_line(aes(y = Actuals)) +
  geom_line(aes(y = Forecasts), color = 'Red', linetype = 'dashed', size = 1)
## Warning: Removed 36 rows containing missing values (geom_path).

forecasts
##          Point Forecast     Lo 80     Hi 80     Lo 95     Hi 95
## Jan 2015       691.4167  510.3547  872.4787  414.5063  968.3271
## Feb 2015       643.4167  462.3547  824.4787  366.5063  920.3271
## Mar 2015       776.4167  595.3547  957.4787  499.5063 1053.3271
## Apr 2015       750.4167  569.3547  931.4787  473.5063 1027.3271
## May 2015      1131.4167  950.3547 1312.4787  854.5063 1408.3271
## Jun 2015      1498.4167 1317.3547 1679.4787 1221.5063 1775.3271
## Jul 2015       952.4167  771.3547 1133.4787  675.5063 1229.3271
## Aug 2015      1213.4167 1032.3547 1394.4787  936.5063 1490.3271
## Sep 2015      1536.4167 1355.3547 1717.4787 1259.5063 1813.3271
## Oct 2015       983.4167  802.3547 1164.4787  706.5063 1260.3271
## Nov 2015      1518.4167 1337.3547 1699.4787 1241.5063 1795.3271
## Dec 2015      1485.4167 1304.3547 1666.4787 1208.5063 1762.3271
## app.R ##
library(shiny)
library(shinydashboard)
## 
## Attaching package: 'shinydashboard'
## The following object is masked from 'package:graphics':
## 
##     box
sidebar <-   dashboardSidebar(
  ###side bar panel options
  sidebarMenu(
    menuItem("Exploratory Data Analysis",tabName = "eda"),
    
    menuItem("Forecast",tabName = "forecast"),
    
    menuItem(selectInput("country_id", "Select the Country:",
                 c("United States","Australia","Mexico","Germany","China","France"
                   )))
  ))


body <- dashboardBody(
  tabItems(tabItem(tabName = "eda", 
                  
                   fluidRow(
                    
                 box( width=12, plotOutput("plot1"))
                  ),
                  fluidRow(
                  box( width=12, plotOutput("plot2"))),
                  
                  fluidRow(box( width=12, plotOutput("plot3")))),
                  
                  
                  
                  tabItem(tabName = "forecast", 
                          
                  fluidRow(
                    box(width =12, plotOutput("plot4"))),
                    
                  fluidPage(
                    sliderInput("period", "Forecast Period (in months):",
                    min = 12, max = 60, step=12,value = 24),plotOutput("plot5")))))





ui <- dashboardPage(skin = "red",
  dashboardHeader(title="Sales Forecasting"),
  sidebar,
  body
)

server <- function(input, output) { 


  


#monthly_ts
  
output$plot1<-renderPlot({
  
Country_picked <- sales[which(Country_filtered$Country %in% c(input$country_id)),]
Country_level <- aggregate(cbind( Quantity) ~ Date, data = Country_picked, FUN = sum)

  
    ggplot(Country_level, aes(y = Quantity, x = Date))  + 
      scale_x_datetime(labels = date_format("%b"), date_breaks = '1 month', expand = c(0.05,0.1)) + 
      facet_grid(~year(Date), space = "free_x", scales = "free_x", switch='x')+
      geom_point() + geom_line(linetype = 'dashed') +
      ggtitle("Monthly Time Series Plot") +
      theme_bw() +
      theme(
        plot.title = element_text(hjust = 0.5),
        strip.placement = 'outside',
        strip.background = element_rect( colour = "grey50"),
        #panel.spacing = unit(0,'cm'),
        axis.text.x = element_text(angle = 90)
      ) 
    
  })
  
  
  
  ##tab_2
  
  #cat_ts 
  output$plot2<-renderPlot({
Country_picked <- sales[which(Country_filtered$Country %in% c(input$country_id)),]
Category_sales <- aggregate(cbind(Quantity) ~ Category + Date, data = Country_picked, FUN = sum)
    
    ggplot(Category_sales, aes(y = Quantity, x =  Date))  + 
      scale_x_datetime(labels = date_format("%b"), date_breaks = '1 month', expand = c(0.05,0.1)) + 
      facet_grid(~year(Date), space = "free_x", scales = "free_x", switch='x')+
      geom_point() + geom_line( aes(color = Category,linetype = Category), size = 1) +
      ggtitle("Monthly Time Series Plot by Category") +
      theme_bw() +
      theme(
        plot.title = element_text(hjust = 0.5),
        strip.placement = 'outside',
        strip.background = element_rect( colour = "grey50"),
        #panel.spacing = unit(0,'cm'),
        axis.text.x = element_text(angle = 90)
      ) 
  })
  
  #heatmap
  output$plot3 <- renderPlot({
Country_picked <- sales[which(Country_filtered$Country %in% c(input$country_id)),]
Sub_Category_sales <- aggregate(cbind(Quantity) ~ Category + `Sub-Category` + Date, data = Country_picked, FUN = sum)

    ggplot(Sub_Category_sales, aes(y = `Sub-Category`, x = Date, fill = Quantity)) +
      scale_x_datetime(labels = date_format("%b"), date_breaks = '1 month', expand = c(0,0)) + 
      facet_grid(~year(Date), space = "free_x", scales = "free_x", switch='x') +
      geom_tile() +
      geom_text(aes(label = Quantity), size  = 3) +
      scale_fill_gradient(low="grey", high="red") +
      ggtitle("Monthly Time Series Plot by Sub-Category") +
      theme_bw() +
      theme(
        plot.title = element_text(hjust = 0.5),
        strip.placement = 'outside',
        strip.background = element_rect( colour = "grey50"),
        panel.spacing = unit(0,'cm'),
        axis.text.x = element_text(angle = 90)
      )   
    })
  
  #forecast plot 
  
output$plot4 <- renderPlot({
Country_picked <- sales[which(Country_filtered$Country %in% c(input$country_id)),]
Country_level <- aggregate(cbind( Quantity) ~ Date, data = Country_picked, FUN = sum)
Sales_train_ts <- ts(Country_level$Quantity[1:36], frequency = 12, start = c(2012,1), end = c(2014,12)) 
## Country models
if (input$country_id == "United States") {
    fit <- Sales_train_ts  %>% Arima( order = c(0,1,1), seasonal = c(0,1,0), include.constant = FALSE)
} else 
  if (input$country_id == "Australia"){
    fit <- Sales_train_ts  %>% Arima( order = c(1,1,1), seasonal = c(0,1,1), include.constant = TRUE)
      }else 
        if (input$country_id == "France"){
          fit <- Sales_train_ts  %>% Arima( order = c(0,0,0), seasonal = c(1,1,0), include.constant = TRUE)
            }else 
              if(input$country_id == "China"){
               fit <-  Sales_train_ts  %>% Arima( order = c(1,1,2), seasonal = c(0,1,1), include.constant = FALSE, lambda = 0)
              }else
              if (input$country_id == "Mexico"){
                fit <- Sales_train_ts  %>% Arima( order = c(1,0,1), seasonal = c(1,1,0), include.constant = TRUE , lambda = 0)
                  }else 
                    if (input$country_id == "Germany"){
                      fit <- Sales_train_ts  %>% Arima( order = c(0,1,1), seasonal = c(0,1,1), include.constant = FALSE, lambda = 0)
                        }else {fit <- Sales_train_ts  %>% Arima( order = c(1,1,2), seasonal = c(0,1,1), include.constant = FALSE, lambda = 0)
                        }




forecasts <- fit %>% forecast(h = 12)
forecast_values <- vector()
forecast_values <- c(forecast_values, rep(NA, 48))
forecast_values[37:48] <- forecasts$mean[1:12]


plotting_dataset <- Country_level

plotting_dataset$Forecasts <- forecast_values
names(plotting_dataset)[2] <- 'Actuals'
    
    
    ggplot(plotting_dataset, aes(x =  Date))  +
scale_x_datetime(labels = date_format("%b"), date_breaks = '1 month', expand = c(0.05,0.1)) +
facet_grid(~year(Date), space = "free_x", scales = "free_x", switch = 'x')+
  geom_point(aes(y = Actuals)) +
  geom_line(aes(y = Actuals, colour = 'Actuals')) +
  geom_line(aes(y = Forecasts, colour = 'Forecasts'), linetype = 'dashed', size = 1) +
    scale_color_manual(name = 'Values', values = c('Actuals' = 'Black',
    'Forecasts' = 'red')) +
  ggtitle("Forecast Comparison") +
  theme_bw() +
  theme(
    plot.title = element_text(hjust = 0.5),
    strip.placement = 'outside',
    strip.background = element_rect( colour = "grey50"),
    panel.spacing = unit(0,'cm'),
    axis.text.x = element_text(angle = 90))  
    
    
  })
  
  output$plot5 <- renderPlot({
    Country_picked <- sales[which(Country_filtered$Country %in% c(input$country_id)),]
Country_level <- aggregate(cbind( Quantity) ~ Date, data = Country_picked, FUN = sum)
Sales_train_ts <- ts(Country_level$Quantity[1:36], frequency = 12, start = c(2012,1), end = c(2014,12)) 
## Country models
if (input$country_id == "United States") {
    fit <- Sales_train_ts  %>% Arima( order = c(0,1,1), seasonal = c(0,1,0), include.constant = FALSE)
} else 
  if (input$country_id == "Australia"){
    fit <- Sales_train_ts  %>% Arima( order = c(1,1,1), seasonal = c(0,1,1), include.constant = TRUE)
      }else 
        if (input$country_id == "France"){
          fit <- Sales_train_ts  %>% Arima( order = c(0,0,0), seasonal = c(1,1,0), include.constant = TRUE)
            }else 
              if(input$country_id == "China"){
               fit <-  Sales_train_ts  %>% Arima( order = c(1,1,2), seasonal = c(0,1,1), include.constant = FALSE, lambda = 0)
              }else
              if (input$country_id == "Mexico"){
                fit <- Sales_train_ts  %>% Arima( order = c(1,0,1), seasonal = c(1,1,0), include.constant = TRUE , lambda = 0)
                  }else 
                    if (input$country_id == "Germany"){
                      fit <- Sales_train_ts  %>% Arima( order = c(0,1,1), seasonal = c(0,1,1), include.constant = FALSE, lambda = 0)
                        }else {fit <- Sales_train_ts  %>% Arima( order = c(1,1,2), seasonal = c(0,1,1), include.constant = FALSE, lambda = 0)
                        }

    
    plot(forecast(fit,h=input$period ))
  })
  
  
}

shinyApp(ui, server)
## PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
Shiny applications not supported in static R Markdown documents