Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. # For starting values in TECH 8 output in .out files ####
  2.  
  3. likelihoods <- readLines(
  4. "C:/LOCATION/FILENAME.out"
  5. )
  6.  
  7. locations <- which(grepl(" ITER LOGLIKELIHOOD ABS CHANGE", likelihoods))
  8. end <- which(grepl("FINAL STAGE ITERATIONS", likelihoods)) - 1
  9.  
  10. llvalues <- list()
  11. for(i in seq_along(locations)){
  12.  
  13. if(i < length(locations)){
  14. x <- likelihoods[locations[i]:locations[i + 1] - 6] # 6 blank rows between
  15. x <- x[grepl("EM", x)] # Select rows with useful data
  16. x <- gsub("\\s+", ",", x) # Replace spaces with a coma
  17. x <- paste(x, collapse = "") # Turn into 1 vector
  18. x <- substring(x, 2) # Lose the leading comma
  19. x <- unlist(strsplit(x, ",")) # Split into parts
  20. x <- as.data.frame(matrix(x, ncol = 7, byrow = TRUE)) # Turn into DF
  21. names(x) <- c("iteration", "loglikelihood", "abs_chng", "rel_chng",
  22. "class", "counts", "algorithm")
  23. x$start_value_set <- i
  24. x$loglikelihood <- as.numeric(gsub("D", "e", x$loglikelihood))
  25. llvalues[[i]] <- x
  26. }
  27.  
  28. else{
  29. x <- likelihoods[locations[i]:end]
  30. x <- x[grepl("EM", x)]
  31. x <- gsub("\\s+", ",", x)
  32. x <- paste(x, collapse = "")
  33. x <- substring(x, 2)
  34. x <- unlist(strsplit(x, ","))
  35. x <- as.data.frame(matrix(x, ncol = 7, byrow = TRUE))
  36.  
  37. names(x) <- c("iteration", "loglikelihood", "abs_chng", "rel_chng", "class",
  38. "counts", "algorithm")
  39.  
  40. x$start_value_set <- i
  41. llvalues[[i]] <- x
  42. }
  43. }
  44.  
  45. ll_start <- do.call(rbind, llvalues) %>%
  46. mutate(loglikelihood = as.numeric(gsub("D", "e", loglikelihood)),
  47. iteration = as.numeric(as.character(iteration)))
  48.  
  49.  
  50. ggplot(ll_start, aes(y = loglikelihood, x = iteration, group = start_value_set,
  51. color = start_value_set)) +
  52. geom_line() +
  53. scale_color_viridis(guide = FALSE) +
  54. xlab("Iteration") +
  55. ylab("log(likelihood)")
  56. ggsave("plots/ll_start.png", scale = 0.8)
  57.  
  58.  
  59. # Final stage iterations in TECH 8 output in .out files ####
  60.  
  61. start <- which(grepl("FINAL STAGE ITERATIONS", likelihoods))
  62. locations <- which(grepl(" TECHNICAL 8 OUTPUT FOR STARTING VALUE SET",
  63. likelihoods))
  64. locations <- locations[locations > start]
  65. end <- which(grepl("SAVEDATA INFORMATION", likelihoods)) - 3
  66.  
  67. llvalues <- list()
  68. for(i in seq_along(locations)){
  69.  
  70. if(i < length(locations)){
  71. x <- likelihoods[(locations[i] + 3):(locations[i + 1] - 3)] # 3 blank rows
  72. x <- x[grepl("EM", x)] # Select rows with useful data
  73. x <- gsub("\\s+", ",", x) # Replace spaces with a coma
  74. x <- paste(x, collapse = "") # Turn into 1 vector
  75. x <- substring(x, 2) # Lose the leading comma
  76. x <- unlist(strsplit(x, ",")) # Split into parts
  77. x <- as.data.frame(matrix(x, ncol = 7, byrow = TRUE)) # Turn into DF
  78. names(x) <- c("iteration", "loglikelihood", "abs_chng", "rel_chng",
  79. "class", "counts", "algorithm")
  80. x$start_value_set <- i
  81. x$loglikelihood <- as.numeric(gsub("D", "e", x$loglikelihood))
  82. llvalues[[i]] <- x
  83. }
  84.  
  85. else{
  86. x <- likelihoods[(locations[i] + 3):end]
  87. x <- x[grepl("EM", x)]
  88. x <- gsub("\\s+", ",", x)
  89. x <- paste(x, collapse = "")
  90. x <- substring(x, 2)
  91. x <- unlist(strsplit(x, ","))
  92. x <- as.data.frame(matrix(x, ncol = 7, byrow = TRUE))
  93.  
  94. names(x) <- c("iteration", "loglikelihood", "abs_chng", "rel_chng", "class",
  95. "counts", "algorithm")
  96.  
  97. x$start_value_set <- i
  98. llvalues[[i]] <- x
  99. }
  100. }
  101.  
  102. ll_final <- do.call(rbind, llvalues) %>%
  103. mutate(loglikelihood = as.numeric(gsub("D", "e", loglikelihood)),
  104. iteration = as.numeric(as.character(iteration)),
  105. start_value_set = as.numeric(start_value_set))
  106.  
  107.  
  108. ggplot(ll_final, aes(y = loglikelihood, x = iteration, group = start_value_set,
  109. color = start_value_set)) +
  110. geom_line() +
  111. scale_color_viridis(guide = FALSE) +
  112. xlab("Iteration") +
  113. ylab("log(likelihood)")
  114. ggsave("plots/ll_final.png", scale = 0.8)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement