tylermorganwall / skpr

Generates and evaluates D, I, A, Alias, E, T, G, and custom optimal designs. Supports generation and evaluation of mixture and split/split-split/N-split plot designs. Includes parametric and Monte Carlo power evaluation functions. Provides a framework to evaluate power using functions provided in other packages or written by the user.
https://tylermorganwall.github.io/skpr/
GNU General Public License v3.0
115 stars 15 forks source link

How do you manually generate a "colored" design matrix, and what do the colors mean? #73

Closed mr-september closed 2 years ago

mr-september commented 2 years ago

For example, with the GUI, clicking on "Generate Design" gives a matrix of runs, usually with boxes colored purple/yellow.

However, for large runs, the GUI window size crops the table, preventing a whole "screenshot".

  1. Is it possible to output a similarly colored matrix with "manual code" where we can freely re-size the output plot?
  2. What do the colors mean?
tylermorganwall commented 2 years ago
  1. The below code is from the shiny application: you can run it yourself to get a gt table with the same style:
library(gt)
pal_option = function(n, colorchoice = "A") {
    if(colorchoice == "A") {
      viridis::magma(n)
    } else if(colorchoice == "B") {
      viridis::inferno(n)
    } else if(colorchoice == "C") {
      viridis::plasma(n)
    } else if(colorchoice == "D") {
      viridis::viridis(n)
    } else {
      "white"
    }
  }

style_matrix = function(run_matrix, order_vals = FALSE, alpha = 0.3) {
    . = NULL
    if(order_vals) {
      new_runmat = run_matrix[do.call(order, run_matrix),, drop=FALSE ]
      rownames(new_runmat) = 1:nrow(new_runmat)

      display_rm = gt(new_runmat[do.call(order, new_runmat),, drop=FALSE ],
                      rownames_to_stub = TRUE) %>%
        tab_stubhead("Run") %>%
        tab_options(data_row.padding = px(10))  %>%
        tab_spanner(
          label = "Factors",
          columns = colnames(.)
        ) %>% tab_header(
          title = "Design"
        )
    } else {
      display_rm = gt(run_matrix,rownames_to_stub = TRUE) %>%
        tab_stubhead("Run") %>%
        tab_options(data_row.padding = px(10)) %>%
        tab_spanner(
          label = "Factors",
          columns = colnames(.)
        ) %>% tab_header(
          title = "Design"
        )
    }
    cols_rm = colnames(run_matrix)
    for(i in seq_len(length(cols_rm))) {
      if(is.numeric(run_matrix[,i])) {
        display_rm = display_rm %>%
          data_color(
            columns = cols_rm[i],
            colors = pal_option(100),
            alpha = alpha,
            autocolor_text = FALSE)
      } else {
        display_rm = display_rm %>%
          data_color(
            columns = cols_rm[i],
            colors = pal_option(length(unique(run_matrix[,i]))),
            alpha = alpha,
            autocolor_text = FALSE)
      }
    }
    display_rm
  }
  1. The colors just denote either unique factor levels (for categorical factors) or a gradient going from low-high for continuous factors.
mr-september commented 2 years ago

Thank you!