Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # For starting values in TECH 8 output in .out files ####
- likelihoods <- readLines(
- "C:/LOCATION/FILENAME.out"
- )
- locations <- which(grepl(" ITER LOGLIKELIHOOD ABS CHANGE", likelihoods))
- end <- which(grepl("FINAL STAGE ITERATIONS", likelihoods)) - 1
- llvalues <- list()
- for(i in seq_along(locations)){
- if(i < length(locations)){
- x <- likelihoods[locations[i]:locations[i + 1] - 6] # 6 blank rows between
- x <- x[grepl("EM", x)] # Select rows with useful data
- x <- gsub("\\s+", ",", x) # Replace spaces with a coma
- x <- paste(x, collapse = "") # Turn into 1 vector
- x <- substring(x, 2) # Lose the leading comma
- x <- unlist(strsplit(x, ",")) # Split into parts
- x <- as.data.frame(matrix(x, ncol = 7, byrow = TRUE)) # Turn into DF
- names(x) <- c("iteration", "loglikelihood", "abs_chng", "rel_chng",
- "class", "counts", "algorithm")
- x$start_value_set <- i
- x$loglikelihood <- as.numeric(gsub("D", "e", x$loglikelihood))
- llvalues[[i]] <- x
- }
- else{
- x <- likelihoods[locations[i]:end]
- x <- x[grepl("EM", x)]
- x <- gsub("\\s+", ",", x)
- x <- paste(x, collapse = "")
- x <- substring(x, 2)
- x <- unlist(strsplit(x, ","))
- x <- as.data.frame(matrix(x, ncol = 7, byrow = TRUE))
- names(x) <- c("iteration", "loglikelihood", "abs_chng", "rel_chng", "class",
- "counts", "algorithm")
- x$start_value_set <- i
- llvalues[[i]] <- x
- }
- }
- ll_start <- do.call(rbind, llvalues) %>%
- mutate(loglikelihood = as.numeric(gsub("D", "e", loglikelihood)),
- iteration = as.numeric(as.character(iteration)))
- ggplot(ll_start, aes(y = loglikelihood, x = iteration, group = start_value_set,
- color = start_value_set)) +
- geom_line() +
- scale_color_viridis(guide = FALSE) +
- xlab("Iteration") +
- ylab("log(likelihood)")
- ggsave("plots/ll_start.png", scale = 0.8)
- # Final stage iterations in TECH 8 output in .out files ####
- start <- which(grepl("FINAL STAGE ITERATIONS", likelihoods))
- locations <- which(grepl(" TECHNICAL 8 OUTPUT FOR STARTING VALUE SET",
- likelihoods))
- locations <- locations[locations > start]
- end <- which(grepl("SAVEDATA INFORMATION", likelihoods)) - 3
- llvalues <- list()
- for(i in seq_along(locations)){
- if(i < length(locations)){
- x <- likelihoods[(locations[i] + 3):(locations[i + 1] - 3)] # 3 blank rows
- x <- x[grepl("EM", x)] # Select rows with useful data
- x <- gsub("\\s+", ",", x) # Replace spaces with a coma
- x <- paste(x, collapse = "") # Turn into 1 vector
- x <- substring(x, 2) # Lose the leading comma
- x <- unlist(strsplit(x, ",")) # Split into parts
- x <- as.data.frame(matrix(x, ncol = 7, byrow = TRUE)) # Turn into DF
- names(x) <- c("iteration", "loglikelihood", "abs_chng", "rel_chng",
- "class", "counts", "algorithm")
- x$start_value_set <- i
- x$loglikelihood <- as.numeric(gsub("D", "e", x$loglikelihood))
- llvalues[[i]] <- x
- }
- else{
- x <- likelihoods[(locations[i] + 3):end]
- x <- x[grepl("EM", x)]
- x <- gsub("\\s+", ",", x)
- x <- paste(x, collapse = "")
- x <- substring(x, 2)
- x <- unlist(strsplit(x, ","))
- x <- as.data.frame(matrix(x, ncol = 7, byrow = TRUE))
- names(x) <- c("iteration", "loglikelihood", "abs_chng", "rel_chng", "class",
- "counts", "algorithm")
- x$start_value_set <- i
- llvalues[[i]] <- x
- }
- }
- ll_final <- do.call(rbind, llvalues) %>%
- mutate(loglikelihood = as.numeric(gsub("D", "e", loglikelihood)),
- iteration = as.numeric(as.character(iteration)),
- start_value_set = as.numeric(start_value_set))
- ggplot(ll_final, aes(y = loglikelihood, x = iteration, group = start_value_set,
- color = start_value_set)) +
- geom_line() +
- scale_color_viridis(guide = FALSE) +
- xlab("Iteration") +
- ylab("log(likelihood)")
- ggsave("plots/ll_final.png", scale = 0.8)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement