Guest User

Untitled

a guest
Jul 18th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. ---
  2. title: "CCM test"
  3. date: "June 27, 2018"
  4. output: html_document
  5. ---
  6.  
  7. ```{r setup, include=FALSE}
  8. knitr::opts_chunk$set(echo = TRUE, cache = TRUE)
  9.  
  10. indent1 = ' '
  11. indent2 = paste(rep(indent1, 2), collapse='')
  12. indent3 = paste(rep(indent1, 3), collapse='')
  13.  
  14. doeval = TRUE
  15. doecho = FALSE
  16.  
  17. library(knitr) # for kable
  18. library(tidyverse) # column as nested data frame
  19. library(ggplot2)
  20. library(rEDM)
  21. library(TransferEntropy)
  22. library(forecast)
  23. library(lmtest)
  24. library(vars)
  25. knitr::opts_chunk$set(echo = TRUE)
  26. ```
  27.  
  28. ```{r read data, message=FALSE}
  29. # read the data from the file
  30. TeslaSearch <- read.csv("Tesla Search.txt",header = FALSE,col.names=c("Tesla Search")) %>% as.tbl
  31. TeslaSale <- read.csv("Tesla Sale.txt",header = FALSE, col.names=c("Tesla Sale")) %>% as.tbl
  32.  
  33. AudiSale <- read.csv("AudiSale.txt",header = FALSE,col.names=c("Audi Sale")) %>% as.tbl
  34. AudiSearch <- read.csv("AudiSearch.txt",header = FALSE,col.names = c("Audi Search")) %>% as.tbl
  35. ```
  36.  
  37.  
  38.  
  39.  
  40. ```{r remove na, message=FALSE}
  41. TeslaSale <- na.omit(TeslaSale)
  42. TeslaSearch <- na.omit(TeslaSearch)
  43.  
  44. AudiSale <- na.omit(AudiSale)
  45. AudiSearch <- na.omit(AudiSearch)
  46.  
  47.  
  48.  
  49. # CONVERT THE DATA TO STATIONARY TIME SERIES
  50. N1 <- nrow(AudiSale)
  51. N2 <- nrow(AudiSearch)
  52.  
  53. Audi.ts1 <- cbind(log(AudiSearch[2:N1,] /AudiSearch[1:N1-1,]),log(AudiSale[2:N1,]/ AudiSale[1:N1-1,])) %>% as.tbl
  54. Tesla.ts1 <- cbind(log(TeslaSearch[2:N2,] /TeslaSearch[1:N2-1,]),log(TeslaSale[2:N2,]/ TeslaSale[1:N2-1,])) %>% as.tbl
  55. ```
  56.  
  57.  
  58. ```{r granger test, message=FALSE}
  59.  
  60. audi.var1 <- VAR(Audi.ts1, p =1 , type = "const")
  61. causality(audi.var1, cause = "Audi.Search")
  62.  
  63.  
  64. tesla.var1 <- VAR(Tesla.ts1, p =1 , type = "const")
  65. causality(tesla.var1, cause = "Tesla.Search")
  66.  
  67. ```
  68.  
  69.  
  70. Under the confidance level $\alpha = 0.05$, Audi.Search do not Granger-cause Audi.Sale. But Tesla.Search do not Granger-cause Tesla.Sale with lag = 1.
  71.  
  72. ```
  73. ## Compute the TE from AudiSearch to AudiSale
  74. computeTE(AudiSearch,AudiSale,3,1,"MI_diff") ## should be circa 0.16
  75. ```
  76.  
  77.  
  78. ```{r}
  79. block_Tesla <- cbind(TeslaSearch,TeslaSale)
  80.  
  81. block_Audi <- cbind(AudiSearch,AudiSale)
  82.  
  83. vars <- colnames(block_Audi)
  84.  
  85.  
  86. # generate all combinations of lib_column, target_column, tp
  87. params <- expand.grid(lib_column = vars,
  88. target_column = vars,
  89. tp = 1:12)
  90.  
  91. # throw out cases where lib == target
  92. params <- params[params$lib_column != params$target_column, ]
  93.  
  94. # E = 3 is optimal or very close to optimal for both vars
  95. # In other circumstances, we should use the best univariate E for each lib_column
  96. E <- 3
  97.  
  98.  
  99. audi.output <- do.call(rbind, lapply(seq_len(NROW(params)), function(i) {
  100. ccm(block_Audi, E = 3,
  101. lib_sizes = NROW(block_Audi), random_libs = FALSE,
  102. lib_column = params$lib_column[i],
  103. target_column = params$target_column[i],
  104. tp = params$tp[i], silent = TRUE)
  105. }
  106. )
  107. )
  108. kable(audi.output)
  109. ```
  110.  
  111.  
  112. ```
  113. audi.output$direction <- paste(output$lib_column, "xmap to", audi.output$target_column)
  114.  
  115. # visualize
  116. ggplot(output, aes(x = tp, y = rho, color = direction)) +
  117. geom_line() + theme_bw()
  118. ```
  119.  
  120. # Exaple for CCM test
  121.  
  122. Following is the standard example from the r-blogger.
  123.  
  124. ```{r example, message=FALSE}
  125. data(paramecium_didinium)
  126.  
  127.  
  128. vars <- names(paramecium_didinium)[2:3] # c("paramecium", "didinium")
  129.  
  130. # generate all combinations of lib_column, target_column, tp
  131. params <- expand.grid(lib_column = vars,
  132. target_column = vars,
  133. tp = -10:10)
  134.  
  135. # throw out cases where lib == target
  136. params <- params[params$lib_column != params$target_column, ]
  137.  
  138. # E = 3 is optimal or very close to optimal for both vars
  139. # In other circumstances, we should use the best univariate E for each lib_column
  140. E <- 3
  141.  
  142. # Perform cross mapping runs
  143.  
  144. output <- do.call(rbind, lapply(seq_len(NROW(params)), function(i) {
  145. ccm(paramecium_didinium, E = 3,
  146. lib_sizes = NROW(paramecium_didinium), random_libs = FALSE,
  147. lib_column = params$lib_column[i],
  148. target_column = params$target_column[i],
  149. tp = params$tp[i], silent = TRUE)
  150. }))
  151.  
  152. kable(output)
  153.  
  154. ```
Add Comment
Please, Sign In to add comment