#install.packages("reticulate")
library(reticulate)
install_python(version = '3.9.13')
+ '/Users/satoshi/Library/Application Support/r-reticulate/pyenv/bin/pyenv' update
+ '/Users/satoshi/Library/Application Support/r-reticulate/pyenv/bin/pyenv' install --skip-existing 3.9.13
[1] "/Users/satoshi/.pyenv/versions/3.9.13/bin/python3.9"
reticulate::repl_python()
print('hello world')
hello world
#!pip install pandas
import pandas as pd
import datetime
data = pd.read_csv('https://filtereddatasets.s3.amazonaws.com/Groceries_retail/Groceriesv2.csv')
data.columns
Index(['Order_no', 'Prod_no', 'Prod_name', 'Sale_date', 'Sale_time',
'No_of_units', 'Unit_price', 'Total_amt'],
dtype='object')
data = data.drop(['Order_no', 'Prod_no', 'Sale_time','No_of_units', 'Unit_price'], axis=1)
data['Sale_date'] = pd.to_datetime(data['Sale_date'])
data['Total_amt'] = data['Total_amt'].str.replace('$', '').astype(float)
data['Prod_name'] = data['Prod_name'].astype(str).str.strip()
data = data.groupby(['Sale_date', 'Prod_name'])['Total_amt'].sum().reset_index()
data.to_csv('prueba.csv')
data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 90138 entries, 0 to 90137
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Sale_date 90138 non-null datetime64[ns]
1 Prod_name 90138 non-null object
2 Total_amt 90138 non-null float64
dtypes: datetime64[ns](1), float64(1), object(1)
memory usage: 2.1+ MB
quit
library(fpp3)
── Attaching packages ────────────────────────────────────────────────────────────────────────────────────────────────────────────────── fpp3 0.5 ──
✔ tibble 3.2.1 ✔ tsibble 1.1.3
✔ dplyr 1.1.3 ✔ tsibbledata 0.4.1
✔ tidyr 1.3.0 ✔ feasts 0.3.1
✔ lubridate 1.9.2 ✔ fable 0.3.3
✔ ggplot2 3.4.3 ✔ fabletools 0.3.3
── Conflicts ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────── fpp3_conflicts ──
✖ lubridate::date() masks base::date()
✖ dplyr::filter() masks stats::filter()
✖ tsibble::intersect() masks base::intersect()
✖ tsibble::interval() masks lubridate::interval()
✖ dplyr::lag() masks stats::lag()
✖ tsibble::setdiff() masks base::setdiff()
✖ tsibble::union() masks base::union()
read.csv('prueba.csv') |>
mutate(Date = ymd(Sale_date)) |>
select(-Sale_date) |>
as_tsibble(key = Prod_name, index = Date) |>
select(Date, Prod_name, Total_amt) -> prueba_tsibble
prueba_tsibble
# library(dplyr)
total_sales <- prueba_tsibble |>
summarise(TOTAL = sum(Total_amt))
total_sales |> autoplot()
Plot variable not specified, automatically selected `.vars = TOTAL`
gg_season(total_sales, period = "week")
Plot variable not specified, automatically selected `y = TOTAL`
gg_subseries(total_sales, y=TOTAL, period = 'week')
total_sales |>
model(STL(TOTAL)) |>
components() |>
autoplot()
fit_total_sales <- total_sales |>
model(TSLM(TOTAL ~ trend() + season()))
fc_total_sales <- forecast(fit_total_sales)
fc_total_sales |>
autoplot(total_sales) +
labs(
title = "Forecasts of TOTAL sales using regression",
y = "$"
)
total_sales |>
gg_tsdisplay(difference(TOTAL, 12),
plot_type='partial', lag=36) +
labs(title="Seasonally differenced", y="")
total_sales |>
gg_tsdisplay(difference(TOTAL, 12) |> difference(),
plot_type='partial', lag=36) +
labs(title = "Double differenced", y="")
install.packages("urca")
trying URL 'https://cran.rstudio.com/bin/macosx/big-sur-x86_64/contrib/4.3/urca_1.3-3.tgz'
Content type 'application/x-gzip' length 1106346 bytes (1.1 MB)
==================================================
downloaded 1.1 MB
The downloaded binary packages are in
/var/folders/ly/0xqts46d0xb6mpwwrttmy0tc0000gq/T//RtmpA4Tod9/downloaded_packages
library(urca)
fit <- total_sales |>
model(
arima012011 = ARIMA(TOTAL), #~ pdq(0,1,2) + PDQ(0,1,1)),
arima210011 = ARIMA(TOTAL), #~ pdq(2,1,0) + PDQ(0,1,1)),
auto = ARIMA(TOTAL, stepwise = FALSE, approx = FALSE)
)
Pivot
fit |> pivot_longer(everything(), names_to = "Model name",
values_to = "Orders")
Glance
glance(fit) |> arrange(AICc) |> select(.model:BIC)
fit |> select(auto) |> gg_tsresiduals()
augment(fit) |>
filter(.model == "auto") |>
features(.innov, ljung_box)
forecast(fit, h=36) |>
filter(.model=='auto') |>
autoplot(total_sales) +
labs(title = "Total sales for Grocery Store",
y="$")
total_sales |>
model(ARIMA(log(TOTAL) ~ 0 + pdq(3,1,1) + PDQ(2,1,2))) |>
forecast() |>
autoplot(total_sales) +
labs(title = "Total sales for Grocery Store",
y="$")
fit <- total_sales |>
model(NNETAR(TOTAL))
fit
fit |>
forecast(h = 14) |>
autoplot(total_sales) +
labs(y = "$", title = "Total Sales Forecast")
fit2 <- total_sales |>
model(NNETAR(sqrt(TOTAL)))
fit2
fit2 |>
forecast(PI = TRUE, h = 5) |>
autoplot(total_sales) +
labs(y = "$", title = "Total Sales Forecast")
fit2 |>
generate(times = 10, h = 30) |>
autoplot(.sim) +
autolayer(total_sales, TOTAL) +
theme(legend.position = "none")
# Generate forecasts
forecast_result <- forecast(fit2, h = 30, level = c(80, 95))
# Plot the original data
total_sales_plot <- autoplot(total_sales) +
labs(y = "$", title = "Total Sales Forecast")
Plot variable not specified, automatically selected `.vars = TOTAL`
# Add forecast and prediction intervals to the plot
total_sales_plot +
autolayer(forecast_result, series = "Point Forecast", PI = TRUE, fill = "blue") +
guides(fill = guide_legend(title = "Prediction Intervals"))
Warning: Ignoring unknown parameters: `series` and `PI`Warning: Ignoring unknown parameters: `series` and `PI`Warning: Ignoring unknown parameters: `series` and `PI`Warning: Ignoring unknown parameters: `series` and `PI`
library(forecast)
Registered S3 method overwritten by 'quantmod':
method from
as.zoo.data.frame zoo
Attaching package: ‘forecast’
The following object is masked from ‘package:fabletools’:
accuracy
set.seed(2015)
(fit <- nnetar(lynx, lambda=0.5))
Series: lynx
Model: NNAR(8,4)
Call: nnetar(y = lynx, lambda = 0.5)
Average of 20 networks, each of which is
a 8-4-1 network with 41 weights
options were - linear output units
sigma^2 estimated as 95.77
sim <- ts(matrix(0, nrow=20, ncol=9), start=end(lynx)[1]+1)
for(i in seq(9))
sim[,i] <- simulate(fit, nsim=20)
library(ggplot2)
autoplot(lynx) + forecast::autolayer(sim)
For a multivariate time series, specify a seriesname for each time series. Defaulting to column names.
fcast <- forecast(fit, PI=TRUE, h=20)
autoplot(fcast)
library(forecast)
set.seed(2015)
(fit_with_intervals <- nnetar(y=total_sales$TOTAL, lambda=0.5))
Series: total_sales$TOTAL
Model: NNAR(20,10)
Call: nnetar(y = total_sales$TOTAL, lambda = 0.5)
Average of 20 networks, each of which is
a 20-10-1 network with 221 weights
options were - linear output units
sigma^2 estimated as 19.91
total_sales
# Create a time series with a proper date index
start_date <- as.Date("2018-01-011") # Change this to your actual start date
sim <- ts(matrix(0, nrow=30, ncol=30), start=start_date)
# Generate simulated data for the time series
for(i in seq(30))
sim[,i] <- simulate(fit_with_intervals, nsim=30)
library(ggplot2)
library(forecast)
# Create a date index for the time series
date_index <- seq(start_date, by = "days", length.out = 30)
date_index
[1] "2018-01-01" "2018-01-02" "2018-01-03" "2018-01-04" "2018-01-05" "2018-01-06" "2018-01-07" "2018-01-08" "2018-01-09" "2018-01-10" "2018-01-11"
[12] "2018-01-12" "2018-01-13" "2018-01-14" "2018-01-15" "2018-01-16" "2018-01-17" "2018-01-18" "2018-01-19" "2018-01-20" "2018-01-21" "2018-01-22"
[23] "2018-01-23" "2018-01-24" "2018-01-25" "2018-01-26" "2018-01-27" "2018-01-28" "2018-01-29" "2018-01-30"
# Create a date index for the time series
date_index <- seq(start_date, by = "days", length.out = 30)
# Assign the date index to the time series
time_series <- ts(sim, start = start_date)
time_series
Time Series:
Start = 17532
End = 17561
Frequency = 1
Series 1 Series 2 Series 3 Series 4 Series 5 Series 6 Series 7 Series 8 Series 9 Series 10 Series 11 Series 12 Series 13 Series 14 Series 15
17532 47008.53 48286.88 48066.66 48608.81 48561.26 48087.91 47369.64 49408.28 47465.35 48369.16 47835.77 48127.05 47334.12 48054.57 48252.92
17533 53028.08 55505.28 54158.44 54428.32 55521.80 55926.85 54152.50 56132.58 54242.52 56720.68 55877.15 55328.20 55930.74 57239.13 55877.04
17534 46568.63 45360.90 46258.53 45416.19 44865.66 43061.42 44393.99 44958.75 43607.29 46054.74 45643.07 44709.48 44838.54 45990.35 45853.08
17535 52603.96 53324.93 53049.46 54809.50 53203.58 51483.29 53260.50 55019.17 53972.49 52638.61 54215.16 52567.02 53377.98 53023.01 53074.73
17536 44140.67 44198.77 43271.99 42354.99 44108.77 43168.55 42292.19 43704.93 43685.43 43721.15 42355.11 42971.60 43090.18 43817.84 42625.80
17537 55598.64 54806.19 55851.53 54241.37 55332.87 55984.72 54634.42 56757.15 55387.88 56542.89 54162.44 55385.74 55104.68 56261.20 56208.95
17538 47201.76 47255.68 46947.10 47393.78 47810.73 45133.31 45659.03 45742.29 43144.94 44795.46 46908.04 46766.95 45996.64 47137.79 45855.75
17539 42966.34 42317.58 42175.05 42218.16 44241.64 42461.95 42275.28 45547.06 41594.54 44401.42 43242.31 44283.07 41873.97 43181.20 44698.16
17540 51720.10 53513.68 53370.62 52499.86 52141.04 52480.77 53459.39 52274.05 51714.26 50763.17 53200.63 53826.32 52726.13 52415.06 52693.39
17541 51886.50 54346.66 52455.07 53257.09 54346.87 53082.60 51224.84 55096.15 50183.70 53459.93 53689.65 55111.22 51968.98 52542.29 53172.53
17542 64674.53 61924.74 62117.34 62218.03 61813.18 60341.01 61346.74 60987.50 60617.65 61526.17 60841.60 61487.52 60390.27 60894.94 61815.17
17543 72859.28 72094.43 71319.20 69323.91 74749.83 70496.33 70783.05 73437.99 69878.12 71951.24 71755.26 72337.07 72122.03 72914.04 72032.61
17544 64530.44 62967.27 60569.80 62546.06 63850.85 63269.77 61765.44 65749.51 62836.42 65296.20 64362.89 62540.15 64885.51 62673.33 64625.73
17545 60997.54 59885.88 62814.28 57506.99 60877.58 62267.27 61800.75 61577.86 60426.46 61398.09 62032.52 61143.87 61002.06 61692.26 61488.73
17546 66210.07 65278.97 65983.38 64292.74 62537.58 64321.77 66217.85 63888.61 68120.92 65442.81 67159.00 67249.44 65290.69 66509.10 64347.03
17547 58391.56 56410.08 57428.55 56040.32 55428.15 54932.81 58843.83 56125.61 57773.13 53353.85 57475.41 56189.07 56258.75 56098.62 56106.02
17548 44503.10 48136.45 44479.90 42780.58 44255.42 46820.19 46342.06 45389.40 46323.09 46694.26 45562.43 46123.28 43995.36 46369.19 45234.70
17549 41052.82 41470.48 42114.75 39178.62 40260.97 42171.58 40928.91 39813.99 40737.32 43167.54 41485.73 42680.95 39005.27 42644.19 42055.35
17550 37759.99 39569.52 40037.50 36666.06 38901.35 38337.06 39729.96 36564.25 38952.40 38447.79 38513.21 40505.52 35735.01 40426.98 37805.07
17551 53585.19 57838.99 55199.40 54424.45 55140.63 56122.79 56466.84 54710.10 59014.78 56657.28 58101.82 57124.09 53953.60 54822.48 54565.31
17552 55068.88 56101.20 55997.94 52059.37 51450.76 56992.29 56167.96 52519.19 57164.56 56634.03 55567.50 56252.24 51849.55 55349.40 54050.45
17553 63357.12 68174.81 65158.11 67029.75 65962.50 63359.01 67217.89 64239.52 67749.00 66885.43 63961.45 66663.14 63612.73 67404.95 65405.66
17554 50059.53 53394.25 55031.61 53433.49 51543.26 52746.32 52311.45 53300.96 55694.59 54160.72 54831.59 55506.13 51418.87 56332.43 51233.70
17555 50985.74 51691.58 54585.70 48157.09 47546.77 50434.94 53857.90 47489.57 55596.19 50930.71 50771.57 51979.88 48241.49 52899.24 47335.25
17556 47252.65 49497.88 48425.57 46495.66 45027.91 48267.12 48719.24 43664.96 50747.32 47853.64 48679.39 48206.76 46268.19 49394.54 47728.10
17557 34989.40 36922.52 37054.20 34143.60 32005.90 35803.63 34787.15 32222.14 37125.44 36779.84 35674.85 36909.22 33210.77 36768.88 33101.08
17558 32311.38 34867.25 34046.65 32237.16 33615.70 32671.68 33842.04 31278.92 36254.90 33395.54 32896.55 34093.92 31450.65 34080.35 32183.45
17559 28985.75 29215.66 29027.48 27177.80 28383.27 28145.54 28245.87 28123.50 28419.19 29418.27 30229.41 29661.48 26845.66 28750.94 30204.18
17560 39258.68 39699.88 38008.05 40013.26 38953.84 36304.98 37512.52 35184.97 36549.57 37433.14 37107.77 37946.42 36536.31 37450.46 38768.06
17561 46164.05 47135.65 45977.53 48632.53 50605.37 45910.67 44192.31 48926.44 45091.88 48157.97 48172.01 48484.05 46676.29 48371.64 46687.04
Series 16 Series 17 Series 18 Series 19 Series 20 Series 21 Series 22 Series 23 Series 24 Series 25 Series 26 Series 27 Series 28 Series 29
17532 48058.44 47063.84 47838.86 48945.21 46306.68 46077.77 47623.08 46488.93 48009.49 47095.71 48919.34 46476.64 47485.74 47651.84
17533 55175.24 54519.22 54804.92 54863.35 55475.35 55053.70 55864.48 53091.52 56676.10 55801.68 54109.06 55419.69 54718.56 53907.77
17534 46281.19 45675.92 45338.82 45394.25 44505.33 45961.27 45793.16 45130.75 44897.57 46378.32 45595.80 47089.18 44309.91 45131.08
17535 53843.96 53436.78 53507.76 50876.86 54355.19 52886.86 54056.95 53695.03 53213.96 53522.87 51280.17 51913.40 54147.70 53378.58
17536 42799.31 42351.68 43605.10 42062.67 43145.18 43481.83 43394.61 44385.96 43344.67 41569.86 43411.81 43545.04 45146.68 43771.38
17537 56919.38 56122.84 55527.30 55480.87 57432.03 56883.81 56201.70 55861.83 57500.11 54795.13 54451.38 57068.96 55855.83 54796.35
17538 47173.37 45464.13 47914.27 45403.90 46159.19 45966.82 46414.30 43835.29 48494.01 46532.95 44521.65 44264.41 45257.55 47197.84
17539 45205.95 43034.37 43082.33 41622.30 44132.73 43310.28 43901.50 41523.45 45696.54 42201.44 42524.75 42018.02 42609.58 43874.27
17540 52747.87 52156.39 50474.27 51210.11 52210.70 55264.89 51251.45 52752.11 50915.52 52301.34 51139.73 53129.66 52959.49 50953.34
17541 53127.83 50154.45 53096.74 51228.90 53458.96 52970.08 52017.50 50420.38 53468.41 52426.98 52814.72 52085.64 54121.43 51007.62
17542 62021.37 61677.34 61086.24 58979.12 62943.58 63564.18 60729.38 60746.05 61023.39 61229.36 61344.09 63079.62 64170.74 62585.59
17543 75303.91 71220.43 74915.11 68645.91 72957.28 71431.54 72682.64 70697.43 76887.18 71205.57 70908.34 71401.38 70505.30 73871.46
17544 65256.84 61802.24 64590.45 60969.28 63073.04 65405.62 65477.47 63528.67 64581.35 64120.52 62171.03 64972.49 63002.00 64069.23
17545 62031.27 62128.15 62000.25 59388.18 63599.12 60579.57 63844.71 60427.53 60052.87 61600.47 62201.49 59774.64 60060.41 61551.95
17546 65729.58 64535.61 64232.84 64666.72 65718.08 67273.60 65550.08 65609.23 65380.23 64546.23 64511.63 67291.77 66585.62 65695.51
17547 56394.75 57712.74 56673.73 57143.42 56876.28 57106.64 57245.16 58173.00 54265.63 55648.37 58069.12 54669.84 57088.37 57481.16
17548 47764.40 45322.50 44790.42 45297.21 47474.06 46271.51 44309.12 44920.10 46163.96 43790.31 46929.77 44884.55 46839.71 46429.04
17549 40495.93 39967.61 40598.11 40362.51 43740.32 45113.08 40077.55 41288.64 40274.00 42014.69 43306.26 41849.20 42351.86 40592.59
17550 38275.26 38392.26 38159.86 40448.34 37645.53 40677.16 36198.44 37467.38 36449.49 39518.13 41671.50 36521.07 39078.73 39970.11
17551 55479.73 53117.92 54593.79 59746.28 54758.36 55140.15 53876.31 56396.84 53401.93 56212.67 58574.04 54329.39 56711.38 53887.22
17552 53513.79 54035.51 51460.51 59535.49 54206.53 55790.95 52142.01 55279.90 48841.04 56263.29 58246.76 54032.63 55330.65 54072.68
17553 64382.29 67314.27 62151.42 68532.56 67228.75 67527.91 62705.27 67399.20 60768.55 68091.47 69769.69 67695.94 66855.17 63696.61
17554 51939.04 54684.99 53440.36 57502.04 52889.87 50842.49 52196.59 56080.09 47976.09 56648.43 57280.85 51868.53 52165.06 53188.46
17555 49126.11 52368.77 48237.09 52534.27 49245.92 51128.04 49891.01 54087.05 47154.26 49390.66 55262.95 50908.50 50798.64 51373.51
17556 47475.18 48175.90 46150.77 51786.37 49384.77 48305.15 46885.41 50452.45 43039.68 50423.69 51280.02 48588.43 49272.91 46179.53
17557 34551.30 36698.53 34590.41 41451.76 36035.64 35745.39 34113.33 34821.95 33235.14 36635.18 37619.20 36686.93 36313.17 35031.00
17558 32779.71 33812.20 31905.93 36101.86 33723.83 32451.18 31407.53 35202.45 33320.10 31546.89 34649.20 31788.51 33031.72 33534.67
17559 28084.00 27789.18 29000.20 31298.21 29630.42 28965.24 26902.56 27738.50 27791.33 30201.55 29995.33 28716.72 28963.28 29101.04
17560 36992.88 35944.28 37083.80 40025.04 38493.03 39015.76 33782.66 39277.18 39198.44 38206.55 36490.65 40337.09 40110.95 36860.21
17561 45948.33 46822.22 49529.68 46399.51 46067.71 44998.77 45646.66 46538.58 49695.23 47931.84 46186.90 45199.44 46517.80 47065.75
Series 30
17532 48105.50
17533 55808.81
17534 43460.64
17535 52185.13
17536 43327.63
17537 55874.12
17538 45367.97
17539 42640.42
17540 51502.42
17541 51398.66
17542 60647.43
17543 72686.63
17544 63975.99
17545 60764.58
17546 64103.29
17547 55948.83
17548 45363.19
17549 41059.93
17550 38587.08
17551 57275.83
17552 53306.08
17553 65597.42
17554 52488.70
17555 50372.95
17556 48062.36
17557 34909.85
17558 33487.32
17559 27083.39
17560 36210.35
17561 45251.57
# Create a plot
autoplot(time_series) +
autolayer(sim)
For a multivariate time series, specify a seriesname for each time series. Defaulting to column names.
# Calculate the average and confidence intervals
sim_mean <- rowMeans(sim)
sim_upper <- apply(sim, 1, quantile, probs = 0.975)
sim_lower <- apply(sim, 1, quantile, probs = 0.025)
# Create a data frame for ggplot2
plot_data <- data.frame(Date = date_index, Mean = sim_mean, Upper = sim_upper, Lower = sim_lower)
# Create a plot
ggplot(plot_data, aes(x = Date, y = Mean)) +
geom_ribbon(aes(ymin = Lower, ymax = Upper), fill = "blue", alpha = 0.5) +
geom_line() +
labs(title = "Time Series with Confidence Intervals", y = "Value") +
theme_minimal()
plot_data
# Create a plot
ggplot() +
geom_ribbon(data = plot_data, aes(x = Date, ymin = Lower, ymax = Upper), fill = "blue", alpha = 0.5) +
geom_line(data = total_sales, aes(x = Date, y = TOTAL), color = "red") +
labs(title = "Total Sales with Confidence Intervals", y = "Total Sales") +
theme_minimal()
library(ggplot2)
library(tsibble)
library(dplyr)
# Assuming you have a tsibble named 'total_sales' and a data frame 'plot_data'
# Create a plot with improved aesthetics
ggplot() +
geom_ribbon(data = plot_data, aes(x = Date, ymin = Lower, ymax = Upper), fill = "blue", alpha = 0.5, color = "blue") +
geom_line(data = total_sales, aes(x = Date, y = TOTAL), color = "black") +
labs(title = "Total Sales with Confidence Intervals", y = "Total Sales", fill = "Confidence Intervals") +
theme_minimal() +
theme(
plot.title = element_text(size = 16, hjust = 0.5),
axis.title.x = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1),
axis.text.y = element_text(size = 12),
legend.title = element_text(size = 12),
legend.text = element_text(size = 10)
) +
scale_fill_identity()
library(ggplot2)
library(tsibble)
library(dplyr)
# Assuming you have a tsibble named 'total_sales' and a data frame 'plot_data'
# Define custom colors for the confidence intervals
confidence_colors <- c("blue", "lightblue") # Adjust colors as needed
# Create a plot with improved aesthetics and a legend
ggplot() +
geom_ribbon(data = plot_data, aes(x = Date, ymin = Lower, ymax = Upper, fill = "Confidence Intervals"), alpha = 0.5, color = "blue") +
geom_line(data = total_sales, aes(x = Date, y = TOTAL, color = "Total Sales")) +
labs(title = "Total Sales with Confidence Intervals", y = "Total Sales") +
theme_minimal() +
theme(
plot.title = element_text(size = 16, hjust = 0.5),
axis.title.x = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1),
axis.text.y = element_text(size = 12),
legend.title = element_text(size = 12),
legend.text = element_text(size = 10)
) +
scale_fill_manual(
name = "Legend",
values = setNames(confidence_colors, "Confidence Intervals"),
breaks = "Confidence Intervals",
labels = "Confidence Intervals"
) +
scale_color_manual(
name = "Legend",
values = setNames("black", "Total Sales"),
breaks = "Total Sales",
labels = "Total Sales"
)
# Create a time series with a proper date index
start_date <- as.Date("2018-01-01") # Change this to your actual start date
sim <- ts(matrix(0, nrow=30, ncol=10), start=start_date)
# Generate simulated data for the time series
for(i in seq(10))
sim[,i] <- simulate(fit_with_intervals, nsim=30)
library(ggplot2)
library(forecast)
# Create a date index for the time series
date_index <- seq(start_date, by = "days", length.out = 30)
#date_index
# Create a date index for the time series
date_index <- seq(start_date, by = "days", length.out = 30)
# Assign the date index to the time series
time_series <- ts(sim, start = start_date)
#time_series
# Calculate the average and confidence intervals
sim_mean <- rowMeans(sim)
sim_upper <- apply(sim, 1, quantile, probs = 0.975)
sim_lower <- apply(sim, 1, quantile, probs = 0.025)
sim_upper_80 <- apply(sim, 1, quantile, probs = 0.90)
sim_lower_80 <- apply(sim, 1, quantile, probs = 0.10)
# Create a data frame for ggplot2
plot_data <- data.frame(Date = date_index, Mean = sim_mean, Upper_95 = sim_upper, Lower_95 = sim_lower, Upper_80=sim_upper_80, Lower_80=sim_lower_80)
# Create a plot
ggplot(plot_data, aes(x = Date, y = Mean)) +
geom_ribbon(aes(ymin = Lower_80, ymax = Upper_80), fill = "blue", alpha = 0.5) +
geom_line() +
labs(title = "Time Series with Confidence Intervals", y = "Value") +
theme_minimal()
# Create the plot
ggplot() +
#geom_line(data = total_sales, aes(x = Date, y = TOTAL), color = "black") +
geom_ribbon(data = plot_data, aes(x = Date, ymin = Lower_80, ymax = Upper_80), fill = "black", alpha = 0.5) +
geom_line(data = plot_data, aes(x = Date, y = Mean), color = "black") +
geom_ribbon(data = plot_data, aes(x = Date, ymin = Lower_95, ymax = Upper_95), fill = "red", alpha = 0.5) +
labs(
title = "Total Sales with Confidence Intervals",
x = "Date",
y = "Total Sales"
) +
theme_minimal()
# Create the plot
ggplot() +
#geom_line(data = total_sales, aes(x = Date, y = TOTAL), color = "black") +
geom_ribbon(data = plot_data, aes(x = Date, ymin = Lower_80, ymax = Upper_80, fill = "80% CI"), alpha = 0.5) +
geom_line(data = plot_data, aes(x = Date, y = Mean), color = "black") +
geom_ribbon(data = plot_data, aes(x = Date, ymin = Lower_95, ymax = Upper_95, fill = "95% CI"), alpha = 0.5) +
labs(
title = "Total Sales with Confidence Intervals",
x = "Date",
y = "Total Sales"
) +
scale_fill_manual(values = c("80% CI" = "blue", "95% CI" = "red")) +
guides(fill = guide_legend(title = "Confidence Interval")) +
theme_minimal()
# Create a custom color palette with better contrast
confidence_colors <- c("80% CI" = "#0000ff", "95% CI" = "#0000ff")
# Create the plot
ggplot() +
geom_line(data = total_sales, aes(x = Date, y = TOTAL), color = "black") +
geom_ribbon(data = plot_data, aes(x = Date, ymin = Lower_80, ymax = Upper_80, fill = "80% CI"), alpha = 0.5) +
geom_line(data = plot_data, aes(x = Date, y = Mean), color = "black") +
geom_ribbon(data = plot_data, aes(x = Date, ymin = Lower_95, ymax = Upper_95, fill = "95% CI"), alpha = 0.5) +
labs(
title = "Total Sales with Confidence Intervals",
x = "Date",
y = "Total Sales"
) +
scale_fill_manual(values = confidence_colors) +
guides(fill = guide_legend(title = "Confidence Interval")) +
theme_minimal()
#install.packages("plotly")
# Load the necessary libraries
library(ggplot2)
library(plotly)
# Create a custom color palette with better contrast
confidence_colors <- c("80% CI" = "#3498DB", "95% CI" = "#E74C3C")
# Create the base ggplot object
p <- ggplot() +
geom_line(data = total_sales, aes(x = Date, y = TOTAL), color = "black") +
geom_ribbon(data = plot_data, aes(x = Date, ymin = Lower_80, ymax = Upper_80, fill = "80% CI"), alpha = 0.5) +
geom_line(data = plot_data, aes(x = Date, y = Mean), color = "darkblue") +
geom_ribbon(data = plot_data, aes(x = Date, ymin = Lower_95, ymax = Upper_95, fill = "95% CI"), alpha = 0.5) +
labs(
title = "Future sales forecast with confidence intervals",
x = "Date",
y = "Total Sales"
) +
scale_fill_manual(values = confidence_colors) +
guides(fill = guide_legend(title = "Confidence Interval")) +
theme_minimal()
# Convert ggplot object to plotly object for interactivity
plotly_plot <- ggplotly(p)
# Display the interactive plot
plotly_plot
NA
library(forecast)
set.seed(2015)
(forecas_intervals <- nnetar(y=total_sales$TOTAL, lambda=0.5))
Series: total_sales$TOTAL
Model: NNAR(20,10)
Call: nnetar(y = total_sales$TOTAL, lambda = 0.5)
Average of 20 networks, each of which is
a 20-10-1 network with 221 weights
options were - linear output units
sigma^2 estimated as 19.91
fcast <- forecast(forecas_intervals, PI=TRUE, h=60)
autoplot(fcast)
autoplot_ts <- autoplot(fcast)
autoplot_ts