-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_8.Rmd
422 lines (299 loc) · 14.9 KB
/
module_8.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
---
title: "R Notebook"
output: html_notebook
---
This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Ctrl+Shift+Enter*.
```{r}
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
options(scipen=999)
library(readr)
library(hrbrthemes)
library(tidyverse)
library(rlang)
library(rEDM)
```
```{r}
data <- read_csv("../ESMdata/ESMdata.csv")
View(data)
colnames(data)[1] <- "Observation"
```
```{r}
new <- data[,c(1:2,5,86, 11:22)]
#new <- na.omit(new)
new <- new[!duplicated(new$date), ]
new$date <- as.Date(new$date, tryFormats = c("%d/%m/%y"))
new
new$Observation<- 1:nrow(new)
View(new)
```
```{r}
new2 <- new[, c(1,9, 14)]
new2
```
```{r}
#Let’s take a look at the time series.
plot1 <- ggplot(data=new2, aes(x=Observation, y=mood_lonely)) + geom_line(aes(Observation, mood_lonely),color="black") + geom_line(aes(Observation,mood_guilty), color="red") + xlab("Days") + ylab("Item Rating") + ggtitle("Time Series of items mood_lonely and mood_guilty ") +
scale_y_continuous( n.breaks = 7, limits = c(-3, 3))
plot1
```
```{r}
# Preprocessing? Yes, for EDM they should be normalized.
new2$mood_lonely <- as.numeric(scale(new2$mood_lonely))
new2$mood_guilty <- as.numeric(scale(new2$mood_guilty))
plot2 <- ggplot(data=new2, aes(x=Observation, y=mood_lonely)) + geom_line(aes(Observation, mood_lonely),color="black") + geom_line(aes(Observation,mood_guilty), color="red") + xlab("Days") + ylab("Item Rating") + ggtitle("Normalized Time Series of items")+
scale_y_continuous( n.breaks = 7, limits = c(-6, 6))
plot2
```
```{r}
#Preprocessing? Overall, the signals look stationary, so no need to detrend. However, we will test this using a KPSS test.
library(tseries)
#perform KPSS test for stationarity for item mood_lonely
kpss_lonely <- kpss.test(new2$mood_lonely, null="Trend")
kpss_lonely
#perform KPSS test for stationarity for item mood_guilty
kpss_guilty <- kpss.test(new2$mood_guilty, null="Trend")
kpss_guilty
# The p-value is > 0.1 for both. Since this value is not less than .05, we fail to reject the null hypothesis of the KPSS test.
# This means we can assume that the time series is trend stationary.
```
Determining the complexity of a system based on Simplex Projection, S-Map
```{r}
# Run simplex projection on new2$mood_lonely
simplex_out <- Simplex(dataFrame = new2, lib = "1 118", pred = "119 238", E=1, columns='mood_lonely', target ='mood_lonely')
simplex_out
#simplex projections plot
plot(simplex_out$Observations[2:120],type='l', xlab = "Days", ylab="Item Rating", main = "mood_lonely")
lines(simplex_out$Predictions[2:120],type='l',col=2,lty=2)
# Compute error between observed and predicted values
ComputeError(simplex_out$Observations, simplex_out$Predictions)
# Run simplex projection on new2$mood_guilty
simplex_out2 <- Simplex(dataFrame = new2, lib = "1 118", pred = "119 238", E=1, columns='mood_guilty', target ='mood_guilty')
simplex_out2
#simplex projections plot
plot(simplex_out2$Observations[2:120],type='l', xlab = "Days", ylab="Item Rating", main = "mood_guilty")
lines(simplex_out2$Predictions[2:120],type='l',col=2,lty=2)
# Compute error between observed and predicted values
ComputeError(simplex_out2$Observations, simplex_out2$Predictions)
```
```{r}
#Check for the optimal embedding dimension for ML
rho_emd <- EmbedDimension(dataFrame = new2, lib = "1 118", pred = "119 238", columns='mood_lonely', target ='mood_lonely')
rho_emd
rho_emd2 <- EmbedDimension(dataFrame = new2, lib = "1 118", pred = "119 238", columns='mood_guilty', target ='mood_guilty')
rho_emd2
#It looks like E =1 and E = 10 are the one the optimize prediction skill, respectively.
```
```{r}
# Run simplex projection on new2$mood_lonely with optimized E
#Perform simplex projection and visualize the observed vs predicted values and compute the error metrics
simplex_out <- Simplex(dataFrame = new2, lib = "1 118", pred = "119 238", E=1, columns='mood_lonely', target ='mood_lonely')
simplex_out
#simplex projections plot
plot(simplex_out$Observations[2:119],type='l', xlab = "Time_point", ylab="Value", main = "mood_lonely")
lines(simplex_out$Predictions[2:119],type='l',col=2,lty=2)
# Compute error between observed and predicted values
ComputeError(simplex_out$Observations, simplex_out$Predictions)
# Run simplex projection on mood_guilty
simplex_out2 <- Simplex(dataFrame = new2, lib = "1 118", pred = "119 238", E=10, columns='mood_guilty', target ='mood_guilty')
simplex_out2
#simplex projections plot
plot(simplex_out2$Observations[2:119],type='l', xlab = "Time_point", ylab="Item Rating", main = "mood_guilty")
lines(simplex_out2$Predictions[2:119],type='l',col=2,lty=2)
# Compute error between observed and predicted values
ComputeError(simplex_out2$Observations, simplex_out2$Predictions)
```
```{r}
#Determine an optimal $\theta$ for weighting in S-Map projection
# Check for state dependence on mood_lonely
rho_theta_lonely <- PredictNonlinear(dataFrame = new2, lib = "1 118", pred = "119 238", E=1, columns='mood_lonely', target ='mood_lonely')
# Check for state dependence on mood_guilty
rho_theta_guilty <- PredictNonlinear(dataFrame = new2, lib = "1 118", pred = "119 238", E=10, columns='mood_guilty', target ='mood_guilty')
#It looks like ρ is maximized at θ=0 and =8 (theta is 0), repectively. So optimal value could be 0 and 8.
#conclusions on state dependence. e.g.:
#Since optimal θ=0 , this signal is likely not to exhibit state dependency (nonlinearity). The slope looks negative, but the prediction skill is very near to one the entire time.
```
Convergent Cross-Mapping (CCM)
Is there a bidirectional ‘causal’ relationship between AP and ML? Perform convergent cross-mapping to test this and compare it with a cross-correlation.
- Does there seem to be a relationship? If so, is it bidirectional or unidirectional?
```{r}
# Let’s get the right E
rho_emd3 <- EmbedDimension(dataFrame = new2, lib = "1 118", pred = "119 238", columns='mood_lonely', target ='mood_guilty')
```
```{r}
#CCM
# Run a cross correlation
xcor_out <- ccf(new2$mood_lonely,new2$mood_guilty,lag.max=6,type="correlation",plot = FALSE)$acf
# Run the convergent cross mapping
# Note that libSizes is the start, stop and increment index of library sizes
# In reality you should go up to max lib size
cmap <- CCM( dataFrame = new2, E = 2, Tp = 0, columns = "mood_guilty", target = "mood_lonely", libSizes = "3 118 5", sample = 100, showPlot = TRUE )
# Add the cross correlation to the plot
abline(h = max(abs(xcor_out)), col="black",lty=2)
# max(abs(xcor_out))
```
```{r}
# Test here for presence on increasing monotonic trend
Kendall::MannKendall(cmap$`mood_lonely:mood_guilty`)
Kendall::MannKendall(cmap$`mood_guilty:mood_lonely`)
```
```{r}
#intrpretation e.g.:
#Judging from the figure as well as the results from the Kendall’s test, it appears there is a bidirectional relationship between AP and MP. However, ML cross-maps more strongly to AP, than vice versa. Note however that our max(abs(xcor_out)) = 0.4897969, which is slightly higher than our ρ at maximum library length.
max(abs(xcor_out))
```
The rest is not relevant.
<!-- ```{r} -->
<!-- library(nonlinearTseries) -->
<!-- # Does adding an additional variable increase our prediction skill using simplex projection? Perform univariate (on ML) and multivariate embedding (with both ML and AP) and compare the prediction skill. -->
<!-- # First we find the right parameters for univariate embedding -->
<!-- tau.ami <- timeLag(new2$mood_lonely, technique = "ami", lag.max = 100, do.plot = T) -->
<!-- ``` -->
<!-- ```{r} -->
<!-- # Now we've selected the delay, determine an appropriate number of embedding dimensions using Cao's method -->
<!-- emb.dim = estimateEmbeddingDim(new2$mood_lonely, time.lag = tau.ami, max.embedding.dim = 15) -->
<!-- ``` -->
<!-- ```{r} -->
<!-- # Print both parameters -->
<!-- print(tau.ami) -->
<!-- print(emb.dim) -->
<!-- ``` -->
<!-- ```{r} -->
<!-- #Now we will run the univariate forecasting with S-Map. -->
<!-- # Run univariate s-map -->
<!-- smap_mood_lonely_uni <- SMap(dataFrame = new2, lib = "1 118", pred = "119 238", E=emb.dim, tau = tau.ami, columns='mood_lonely', target ='mood_lonely', embedded = FALSE, theta=0) -->
<!-- univariate_stats <- compute_stats(smap_mood_lonely_uni$predictions$Observations,smap_mood_lonely_uni$predictions$Predictions) -->
<!-- univariate_stats -->
<!-- ``` -->
<!-- Let’s create our multivariate embedding and simplex projection. In this case, we are only making the projection using ML and AP with no time delayed copies. -->
<!-- ```{r} -->
<!-- smap_ML_multi <- block_lnlp(block = new2, method= "s-map", lib = "1 118", pred = "119 238", columns= c("mood_lonely", "mood_guilty"), first_column_time = TRUE, stats_only = FALSE, theta=0, save_smap_coefficients = TRUE) -->
<!-- # Extract our metrics -->
<!-- multivariate_rho_ML <- smap_ML_multi$stats$rho[1] -->
<!-- smap_ML_multi$stats$rho[1] -->
<!-- smap_ML_multi$stats$mae[1] -->
<!-- smap_ML_multi$stats$rmse[1] -->
<!-- ``` -->
<!-- How does multi-view embedding compare? Does it increase prediction skill? -->
<!-- ```{r} -->
<!-- ``` -->
<!-- ```{r} -->
<!-- ``` -->
<!-- ```{r} -->
<!-- ``` -->
<!-- ```{r} -->
<!-- ``` -->
<!-- Forecasting with different embeddings -->
<!-- ```{r} -->
<!-- mv_out <- multiview(new2, lib = "1 118", pred = "119 238", E=2, target= "mood_guilty", k=5) -->
<!-- compute_stats(mv_out$Predictions$Observations,mv_out$Predictions$Predictions) -->
<!-- ``` -->
<!-- ```{r} -->
<!-- # #load data for simplex projection mood_strong -->
<!-- # -->
<!-- # strong_simplex <- as.data.frame(cbind(new$Observation,new$mood_strong)) -->
<!-- # -->
<!-- # names(strong_simplex) <- c("time_point","mood_strong") -->
<!-- # -->
<!-- # # normalize the signal -->
<!-- # -->
<!-- # strong_simplex$mood_strong <- as.numeric(scale(strong_simplex$mood_strong)) -->
<!-- # -->
<!-- # strong_simplex -->
<!-- # -->
<!-- # -->
<!-- # -->
<!-- # #load data for simplex projection mood_irritat -->
<!-- # -->
<!-- # irritat_simplex <- as.data.frame(cbind(new$Observation,new$mood_irritat)) -->
<!-- # -->
<!-- # names(irritat_simplex) <- c("time_point","mood_irritat") -->
<!-- # -->
<!-- # # normalize the signal -->
<!-- # -->
<!-- # irritat_simplex$mood_irritat <- as.numeric(scale(irritat_simplex$mood_irritat)) -->
<!-- # -->
<!-- # irritat_simplex -->
<!-- # -->
<!-- # -->
<!-- # -->
<!-- # -->
<!-- # ``` -->
<!-- # -->
<!-- # ```{r} -->
<!-- # -->
<!-- # # Run simplex projection on mood_strong -->
<!-- # -->
<!-- # simplex_out <- Simplex(dataFrame = strong_simplex, lib = "1 118", pred = "119 238", E=1, columns='mood_strong', target ='mood_strong') #753 is the day after which the transition in depressive symptoms took place. -->
<!-- # simplex_out -->
<!-- # -->
<!-- # #simplex projections plot -->
<!-- # plot(simplex_out$Observations[2:121],type='l', xlab = "Time_point", ylab="Value", main = "Mood_strong") -->
<!-- # lines(simplex_out$Predictions[2:121],type='l',col=2,lty=2) -->
<!-- # -->
<!-- # # Compute error between observed and predicted values -->
<!-- # ComputeError(simplex_out$Observations, simplex_out$Predictions) -->
<!-- # -->
<!-- # -->
<!-- # # Run simplex projection on mood_irritat -->
<!-- # -->
<!-- # simplex_out2 <- Simplex(dataFrame = irritat_simplex, lib = "1 118", pred = "119 238", E=1, columns='mood_irritat', target ='mood_irritat') #753 is the day after which the transition in depressive symptoms took place. -->
<!-- # simplex_out2 -->
<!-- # -->
<!-- # #simplex projections plot -->
<!-- # plot(simplex_out2$Observations[2:121],type='l', xlab = "Time_point", ylab="Value", main = "mood_irritat") -->
<!-- # lines(simplex_out2$Predictions[2:121],type='l',col=2,lty=2) -->
<!-- # -->
<!-- # # Compute error between observed and predicted values -->
<!-- # ComputeError(simplex_out2$Observations, simplex_out2$Predictions) -->
<!-- # -->
<!-- # ``` -->
<!-- # -->
<!-- # ```{r} -->
<!-- # -->
<!-- # rho_emd <- EmbedDimension(dataFrame = strong_simplex, lib = "1 118", pred = "119 238", columns='mood_strong', target ='mood_strong') -->
<!-- # rho_emd -->
<!-- # -->
<!-- # -->
<!-- # rho_emd2 <- EmbedDimension(dataFrame = irritat_simplex, lib = "1 118", pred = "119 238", columns='mood_irritat', target ='mood_irritat') -->
<!-- # rho_emd2 -->
<!-- # -->
<!-- # ``` -->
<!-- # -->
<!-- # ```{r} -->
<!-- # -->
<!-- # # Check for state dependence on mood_strong -->
<!-- # rho_theta_strong <- PredictNonlinear(dataFrame = strong_simplex, lib = "1 118", pred = "119 238", E=1, columns='mood_strong', target ='mood_strong') -->
<!-- # -->
<!-- # # Check for state dependence on mood_strong -->
<!-- # rho_theta_irritat <- PredictNonlinear(dataFrame = irritat_simplex, lib = "1 118", pred = "119 238", E=7, columns='mood_irritat', target ='mood_irritat') -->
<!-- # -->
<!-- # ``` -->
<!-- # -->
<!-- # ```{r} -->
<!-- # -->
<!-- # # Here we can plug back in that best theta value to get an S-map projection -->
<!-- # # with appropriate weighting -->
<!-- # s_map_strong <- SMap(dataFrame = strong_simplex, lib = "1 118", pred = "119 238", E=1, theta=2, columns ='mood_strong', target ='mood_strong') -->
<!-- # ComputeError(s_map_strong$predictions$Observations, s_map_strong$predictions$Predictions) -->
<!-- # -->
<!-- # -->
<!-- # -->
<!-- # # Here we can plug back in that best theta value to get an S-map projection -->
<!-- # # with appropriate weighting -->
<!-- # s_map_strong2 <- SMap(dataFrame = irritat_simplex, lib = "1 118", pred = "119 238", E=1, theta=8, columns ='mood_irritat', target ='mood_irritat') -->
<!-- # ComputeError(s_map_strong2$predictions$Observations, s_map_strong2$predictions$Predictions) -->
<!-- ``` -->
<!-- ```{r} -->
<!-- # -->
<!-- # # Here we can plug back in that best theta value to get an S-map projection -->
<!-- # # with appropriate weighting -->
<!-- # s_map_strong <- SMap(dataFrame = new2, lib = "1 118", pred = "119 238", E=1, theta=0, columns ='mood_lonely', target ='mood_lonely') -->
<!-- # ComputeError(s_map_strong$predictions$Observations, s_map_strong$predictions$Predictions) -->
<!-- # -->
<!-- # -->
<!-- # # Here we can plug back in that best theta value to get an S-map projection -->
<!-- # # with appropriate weighting -->
<!-- # s_map_strong2 <- SMap(dataFrame = new2, lib = "1 118", pred = "119 238", E=1, theta=8, columns ='mood_guilty', target ='mood_guilty') -->
<!-- # ComputeError(s_map_strong2$predictions$Observations, s_map_strong2$predictions$Predictions) -->
<!-- ``` -->