0

I am using R and trying to recreate the visualizations from this stackoveflow post, where the "path" taken by an optimization algorithm for some function is visualized:

visualizing the optimization path till convergence in R

I created some fake data:

#load libraries
library(dplyr)


# create some data for this example
a1 = rnorm(1000,100,10)
b1 = rnorm(1000,100,5)
c1 = sample.int(1000, 1000, replace = TRUE)
train_data = data.frame(a1,b1,c1)

Then, I defined the function:

  fitness <- function(x) {
    #bin data according to random criteria
    train_data <- train_data %>%
        mutate(cat = ifelse(a1 <= x[1] & b1 <= x[3], "a",
                            ifelse(a1 <= x[2] & b1 <= x[4], "b", "c")))
   
    train_data$cat = as.factor(train_data$cat)
   
    #new splits
    a_table = train_data %>%
        filter(cat == "a") %>%
        select(a1, b1, c1, cat)
   
    b_table = train_data %>%
        filter(cat == "b") %>%
        select(a1, b1, c1, cat)
   
    c_table = train_data %>%
        filter(cat == "c") %>%
        select(a1, b1, c1, cat)
   
   
   
   
    #calculate  quantile ("quant") for each bin
   
    table_a = data.frame(a_table%>% group_by(cat) %>%
                             mutate(quant = ifelse(c1 > x[5],1,0 )))
                         
                         table_b = data.frame(b_table%>% group_by(cat) %>%
                                                  mutate(quant = ifelse(c1 > x[6],1,0 )))
                                             
                                              table_c = data.frame(c_table%>% group_by(cat) %>%
                                                                       mutate(quant = ifelse(c1 > x[7],1,0 )))
                                                                   
                            }                                       
                                                                   
                                                                   
                                                                   #group all tables
                                                                   
                                                                   final_table = rbind(table_a, table_b, table_c)
                                                                   # calculate the total mean : this is what needs to be optimized
                                                                   mean = mean(final_table$quant)
}

I then used an optimization algorithm on this function:

library(optimization)
Output <- optim_nm(fitness, k = 7, trace = TRUE)

Problem : Now, I am trying to follow the instructions from the previous stackoverflow post (visualizing the optimization path till convergence in R) to visualize the results of this algorithm:

First, define the grid:

library(gganimate)

#define grid

 l.grid <- 200
  grid.b <- as.matrix(expand.grid(b0=seq(-3,0.5,length.out=l.grid), b1=seq(-6,6,length.out=l.grid)))
  grid.f <-apply(grid.b,1,logll)
  grid.b <- as.data.frame(grid.b)

Next, visualize the results (e.g. between any 2 variables) - But I am not sure how to adapt the code from the previous stackoverflow post to my example:

#animate/visualize - not working

op <- sapply(
  seq_len( optim_nm(fitness, k = 7, trace = TRUE)$counts['function']), 
  function(i) { 
    set.seed(1234)
    optim([1], [2] , control = list(maxit = i))$par
  }
)
op_df <- data.frame(b0 = op[1,], b1 = op[2,])
op_df$step <- 1:nrow(op_df)

p <- ggplot(grid.b, aes(b0, b1)) + 
  geom_raster(aes(fill = f)) +
  geom_contour(aes(z = f), col = 'grey40') +
  geom_path(data = op_df, col = 'white') +
  geom_point(data = op_df, col = 'white') +
  scale_fill_viridis_c() +
  coord_cartesian(expand = FALSE)


library(gganimate)
a <- p + transition_reveal(step)

Can someone please show me how to do this?

Thanks

stats_noob
  • 5,401
  • 4
  • 27
  • 83
  • 1
    Just following along with this post, not the referenced, you create `grid.f` then don't subsequently use it. – Chris Jul 09 '21 at 20:51
  • @Chris : Thank you for your comment! I am still trying to adapt the code from this example . – stats_noob Jul 09 '21 at 20:54
  • 1
    It's [wicked slick](https://stackoverflow.com/questions/56797713/visualizing-the-optimization-path-till-convergence-in-r). What part you got working and what errors? You might want to reconsider your use of `cat` as variable. – Chris Jul 09 '21 at 21:04
  • Thank you for your suggestion. I made some changes to the optimization function.. I am still trying to change some new things and see if this fixes the problem. Thank you for all your help! – stats_noob Jul 09 '21 at 21:17
  • @Chris: Here are some updates I made : https://stackoverflow.com/questions/68324429/r-replacement-has-1-row-data-has-0 – stats_noob Jul 10 '21 at 03:24

0 Answers0