I have some timeseries data I'm trying to loop and analyze across multiple people in the form of:
library(lubridate)
library(xts)
library(trend)
Game_Metrics_Boggle$DeviceTime <- ymd_hms(Game_Metrics_Boggle$DeviceTime)
Participant_WordTask <- split(arrange(Game_Metrics_Boggle,StudyId,DeviceTime), arrange(Game_Metrics_Boggle,StudyId,DeviceTime,StudyId,DeviceTime)$StudyId)
for (n in 1:length(Participant_WordTask)){
ads_xts <- xts(filter(Participant_WordTask[[n]], GameEndReason == "TIMER_UP")$NumberOfSuccesfulWords , order.by=as.POSIXct(filter(Participant_WordTask[[n]], GameEndReason == "TIMER_UP")$DeviceTime))
ads_ts <- ts(ads_xts, frequency = nweeks(ads_xts)) }
An example series would look something like this ranging from 3-17 weeks based on the subject's time playing:
> ads_xts
[,1]
2022-05-17 13:14:44 17
2022-05-17 13:20:22 23
2022-05-17 13:25:59 16
2022-05-18 23:52:39 25
2022-05-18 23:57:57 28
2022-05-19 00:03:10 18
2022-05-19 16:14:57 25
2022-05-19 16:21:12 18
2022-05-19 16:26:51 16
2022-05-19 16:42:13 21
2022-05-20 20:57:19 11
2022-05-20 21:02:33 15
2022-05-20 21:07:50 16
2022-05-21 22:05:26 26
2022-05-21 22:10:43 20
2022-05-21 22:15:59 24
2022-05-21 22:33:27 19
2022-05-23 18:21:13 18
2022-05-23 18:26:21 20
2022-05-25 19:37:58 13
2022-05-25 19:43:49 12
2022-05-26 23:39:42 15
2022-05-26 23:44:55 13
2022-05-27 00:25:47 16
2022-05-27 00:30:55 22
2022-05-27 22:39:35 13
2022-05-27 22:44:45 7
2022-05-27 22:49:57 24
2022-05-27 23:39:37 20
2022-05-27 23:44:50 24
2022-05-27 23:50:07 19
2022-05-27 23:55:19 17
2022-05-28 15:57:45 22
2022-05-28 16:02:55 24
2022-05-29 21:40:13 25
2022-05-29 21:45:49 23
2022-05-29 21:50:57 23
2022-05-29 21:56:55 16
2022-05-31 22:37:16 22
2022-05-31 22:42:58 24
2022-05-31 22:48:12 23
2022-06-03 16:58:53 13
2022-06-03 17:04:05 24
2022-06-03 17:09:21 23
2022-06-03 17:24:42 12
2022-06-04 15:39:21 26
2022-06-04 15:44:24 13
2022-06-04 15:49:54 30
2022-06-06 16:08:58 26
2022-06-06 16:14:15 16
2022-06-08 00:11:43 24
2022-06-08 00:16:54 24
2022-06-09 16:38:49 22
2022-06-09 16:43:59 16
2022-06-09 16:49:07 16
2022-06-09 16:54:15 19
2022-06-10 23:38:22 31
2022-06-10 23:43:44 38
2022-06-10 23:49:00 20
2022-06-11 00:23:19 22
2022-06-11 00:28:28 20
2022-06-13 09:15:51 20
2022-06-13 09:21:01 16
2022-06-13 09:26:15 33
2022-06-15 22:53:33 26
2022-06-15 22:58:43 25
2022-06-16 22:21:01 29
2022-06-16 22:26:21 27
2022-06-16 23:03:17 28
2022-06-17 23:11:37 27
2022-06-17 23:16:45 26
2022-06-18 22:37:47 25
2022-06-18 22:42:54 27
2022-06-18 22:48:02 29
2022-06-19 23:10:49 14
2022-06-19 23:15:58 26
2022-06-19 23:21:05 39
2022-06-20 00:04:58 25
2022-06-20 00:10:10 25
2022-06-20 00:15:24 20
2022-06-20 23:53:59 22
2022-06-22 23:58:06 32
2022-06-23 00:03:10 14
2022-06-23 17:58:57 21
2022-06-24 14:31:48 39
2022-06-24 14:36:52 29
2022-06-25 10:46:54 24
2022-06-26 21:20:47 25
2022-06-26 21:25:58 20
2022-06-29 01:09:03 26
2022-06-29 01:14:14 29
2022-06-30 16:41:04 26
2022-06-30 16:46:12 24
2022-06-30 16:51:22 14
2022-07-01 22:19:18 42
2022-07-01 22:24:32 31
2022-07-02 11:05:44 21
2022-07-02 11:10:51 25
2022-07-02 11:16:31 33
2022-07-02 11:21:35 24
2022-07-03 16:57:22 26
2022-07-03 17:02:32 24
2022-07-04 18:42:02 20
2022-07-04 18:47:09 22
2022-07-04 18:52:15 21
2022-07-06 00:05:32 30
2022-07-07 22:39:26 25
2022-07-07 22:44:29 11
2022-07-07 22:49:41 26
2022-07-08 23:20:29 27
2022-07-08 23:25:37 30
2022-07-08 23:57:44 21
2022-07-09 13:29:39 30
2022-07-09 13:34:47 29
2022-07-10 11:40:36 20
2022-07-10 11:45:42 32
2022-07-10 11:50:51 22
Problem is, I'm able to analyze the data just fine, but whenever I try to decompose and plot the data (and possibly add the point for Pettitt’s change in value), the plot does one of 4 things. None of which I want.
If I add:
> plot(decompose(ads_ts))
To the loop, then the x axis no longer displays the time information I want.
If I take the solution suggested here and create the decompose.xts function:
The variable produced (dex in the example) is corrupted somehow and despite having the variable callable in the terminal, can't be read by any function.
> View(dex)
Error in `[.xts`(x, seq_len(n), , drop = FALSE) :
invalid time series parameters specified
> plot(dex)
Error in `[.xts`(cs$Env$xdata, cs$Env$xsubset) :
invalid time series parameters specified
8.
`[.xts`(cs$Env$xdata, cs$Env$xsubset)
7.
cs$Env$xdata[cs$Env$xsubset]
6.
xy.coords(.index(cs$Env$xdata[cs$Env$xsubset]), cs$Env$xdata[cs$Env$xsubset][,
1])
5.
plot.xts(x = x[, tmp], y = y, ... = ..., subset = subset, panels = panels,
multi.panel = multi.panel, col = col[tmp], up.col = up.col,
dn.col = dn.col, bg = bg, type = type, lty = lty[tmp], lwd = lwd[tmp],
lend = lend, main = main, observation.based = observation.based, ...
4.
plot.xts(p, main = paste("Decomposition of", x$type, "time series"),
multi.panel = 4, yaxis.same = FALSE, major.ticks = "days",
grid.ticks.on = "days", ...)
3.
plot(p, main = paste("Decomposition of", x$type, "time series"),
multi.panel = 4, yaxis.same = FALSE, major.ticks = "days",
grid.ticks.on = "days", ...)
2.
plot.decomposed.xts(dex)
1.
plot(dex)
Finally, using stl in any for to do LOESS produces multiple errors:
> stl(ads_ts,s.window="periodic")
Error in stl(ads_ts, s.window = "periodic") :
only univariate series are allowed
> stl(ads_ts,s.window="additive")
Error in stl(ads_ts, s.window = "additive") :
only univariate series are allowed
> stl(ads_xts,s.window="additive")
Error in stl(ads_xts, s.window = "additive") :
series is not periodic or has less than two periods
> stl(ads_xts,s.window="periodic")
Error in stl(ads_xts, s.window = "periodic") :
series is not periodic or has less than two periods
Finally, any attempt to assign a plot to a variable just results in that value being assigned NULL.
> output <- plot(decompose(ads_ts))
> output
NULL
Any suggestions on how I might be able to view and label this data would be sincerely appreciated.
Thank you in advance.