Guest User

Untitled

a guest
Oct 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. library(tidyverse)
  2.  
  3. phis <- c(0.6, -0.4, 0.2, 0.2, 0.1)
  4. p <- length(phis)
  5. X <- rnorm(p)
  6.  
  7. G <- rbind(phis,
  8. cbind(diag(p-1),
  9. rep(0, p-1)))
  10.  
  11. F <- c(1, rep(0, p-1))
  12.  
  13. E <- eigen(G)$vectors
  14. A <- eigen(G)$values
  15.  
  16. # confirm eigendecomp work
  17. round(E %*% diag(A) %*% solve(E), 4) == G
  18.  
  19. Z <- solve(E) %*% X
  20. F0 <- t(E) %*% F
  21.  
  22. k <- 50
  23. Y <- data.frame(matrix(NA, k, p))
  24.  
  25. for (i in 1:p){
  26.  
  27. if (Im(A[i]) == 0) {
  28. Y[, i] <- Re(sapply(1:k, function(t) A[i]^t*F0[i]*Z[i]))
  29. }
  30.  
  31. if (Im(A[i]) != 0) {
  32. match <- which(Im(A) == -Im(A[i]))
  33. Y[, i] <- Re(sapply(1:k, function(t) A[i]^t*F0[i]*Z[i])) +
  34. Re(sapply(1:k, function(t) A[match]^t*F0[match]*Z[match]))
  35. }
  36.  
  37. names(Y)[i] <- paste0("Eig", "_Mod", round(Mod(A[i]), 2), "_Period", round(1/Arg(A[i]), 2))
  38.  
  39. }
  40.  
  41. # remove duplicate columns
  42. Y.df <- data.frame((t(unique(t(Y)))))
  43.  
  44.  
  45. Y.df <- Y.df %>% mutate(
  46. t = 1:k,
  47. Total = Re(sapply(1:k, function(i) t(F0) %*% diag(A)^i %*% Z))
  48. )
  49.  
  50. Y.df %>%
  51. gather(Y, val, -t) %>%
  52. ggplot(aes(t, val, col = Y)) + geom_smooth(se = FALSE, span = 0.05) +
  53. facet_wrap(~Y, scales = "free", nrow = ncol(Y.df) - 1) +
  54. theme(legend.position="none") +
  55. labs(y = "")
Add Comment
Please, Sign In to add comment