Reputation: 439
** Problem **
How box plot can print / show residuals for each month and how to use cycle() to accomplish that this. ALT: Another alternative is to your the innovation residuals (.resid). Seeking to have multiple boxplots by each month with residuals corresponding.
boxplot(residuals(fit_souvenirs) ~ cycle(residuals(fit_souvenirs)))
ALTernate solution
boxplot((res$.resid) ~ cycle(res$.resid))
Another ALTernate solution
model_augmt <- augment(fit_souvenirs, souvenirs$Month)
model_augmt$Month <- month(model_augmt$Month, label = FALSE)
boxplot((model_augmt$.innov) ~ cycle(model_augmt$Month))
The ALT produces this plot:
I have a time bases (x) plot with residuals (y) working.
The following is the code that errors on parameter for boxplot on residuals using cycle:
model_fit <- souvenirs %>% model(TSLM(Sales ~ trend() + season()))
res <- residuals(model_fit)
boxplot(residuals(model_fit) ~ cycle(residuals(model_fit)))
** Error Code / Message **
Error in attr(x, "tsp") <- c(1, NROW(x), 1) : invalid time series parameters specified
11.
hasTsp(x)
10.
tsp(hasTsp(x))
9.
cycle.default(residuals(model_fit))
8.
cycle(residuals(model_fit))
7.
eval(predvars, data, env)
6.
eval(predvars, data, env)
5.
stats::model.frame.default(formula = residuals(model_fit) ~ cycle(residuals(model_fit)))
4.
eval(m, parent.frame())
3.
eval(m, parent.frame())
2.
boxplot.formula(residuals(model_fit) ~ cycle(residuals(model_fit)))
1.
boxplot(residuals(model_fit) ~ cycle(residuals(model_fit)))
** Structure of data **
tbl_ts [84 x 2] (S3: tbl_ts/tbl_df/tbl/data.frame)
$ Month: mth [1:84] 1987 Jan, 1987 Feb, 1987 Mar, 1987 Apr, 1987 May, 1987 Jun, 198...
$ Sales: num [1:84] 1665 2398 2841 3547 3753 ...
- attr(*, "key")= tbl_df [1 x 1] (S3: tbl_df/tbl/data.frame)
..$ .rows: list<int> [1:1]
.. ..$ : int [1:84] 1 2 3 4 5 6 7 8 9 10 ...
.. ..@ ptype: int(0)
- attr(*, "index")= chr "Month"
..- attr(*, "ordered")= logi TRUE
- attr(*, "index2")= chr "Month"
- attr(*, "interval")= interval [1:1] 1M
..@ .regular: logi TRUE
Month Sales
1987 Jan 1665
1987 Feb 2398
1987 Mar 2841
1987 Apr 3547
1987 May 3753
1987 Jun 3715
** Dput data **
structure(list(Month = structure(c(6209, 6240, 6268, 6299, 6329,
6360, 6390, 6421, 6452, 6482, 6513, 6543, 6574, 6605, 6634, 6665,
6695, 6726, 6756, 6787, 6818, 6848, 6879, 6909, 6940, 6971, 6999,
7030, 7060, 7091, 7121, 7152, 7183, 7213, 7244, 7274, 7305, 7336,
7364, 7395, 7425, 7456, 7486, 7517, 7548, 7578, 7609, 7639, 7670,
7701, 7729, 7760, 7790, 7821, 7851, 7882, 7913, 7943, 7974, 8004,
8035, 8066, 8095, 8126, 8156, 8187, 8217, 8248, 8279, 8309, 8340,
8370, 8401, 8432, 8460, 8491, 8521, 8552, 8582, 8613, 8644, 8674,
8705, 8735), class = c("yearmonth", "vctrs_vctr")), Sales = c(1664.81,
2397.53, 2840.71, 3547.29, 3752.96, 3714.74, 4349.61, 3566.34,
5021.82, 6423.48, 7600.6, 19756.21, 2499.81, 5198.24, 7225.14,
4806.03, 5900.88, 4951.34, 6179.12, 4752.15, 5496.43, 5835.1,
12600.08, 28541.72, 4717.02, 5702.63, 9957.58, 5304.78, 6492.43,
6630.8, 7349.62, 8176.62, 8573.17, 9690.5, 15151.84, 34061.01,
5921.1, 5814.58, 12421.25, 6369.77, 7609.12, 7224.75, 8121.22,
7979.25, 8093.06, 8476.7, 17914.66, 30114.41, 4826.64, 6470.23,
9638.77, 8821.17, 8722.37, 10209.48, 11276.55, 12552.22, 11637.39,
13606.89, 21822.11, 45060.69, 7615.03, 9849.69, 14558.4, 11587.33,
9332.56, 13082.09, 16732.78, 19888.61, 23933.38, 25391.35, 36024.8,
80721.71, 10243.24, 11266.88, 21826.84, 17357.33, 15997.79, 18601.53,
26155.15, 28586.52, 30505.41, 30821.33, 46634.38, 104660.67)), row.names = c(NA,
-84L), key = structure(list(.rows = structure(list(1:84), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -1L), class = c("tbl_df",
"tbl", "data.frame")), index = structure("Month", ordered = TRUE), index2 = "Month", interval = structure(list(
year = 0, quarter = 0, month = 1, week = 0, day = 0, hour = 0,
minute = 0, second = 0, millisecond = 0, microsecond = 0,
nanosecond = 0, unit = 0), .regular = TRUE, class = c("interval",
"vctrs_rcrd", "vctrs_vctr")), class = c("tbl_ts", "tbl_df", "tbl",
"data.frame")
Upvotes: 0
Views: 481
Reputation: 439
A potential solution for innovation residuals order boxplots() by month
model_augmt <- augment(fit_souvenirs, souvenirs$Month)
model_augmt$Month <- month(model_augmt$Month, label = FALSE)
model_res_month <- data.frame(inno=model_augmt$.innov, month=model_augmt$Month)
boxplot(inno ~ month, data=model_res_month)
Upvotes: 0