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

`plot_correlations` does not accepted a mutated dataframe #46

Closed StevenCHowell closed 6 years ago

StevenCHowell commented 6 years ago

I created a design representing a discrete numeric factor instead as a categorical factor so that the design would include each level. After getting the design, I am using mutate() together with as.numeric() to convert the strings used for the categorical levels into integers.

c_design = gen_design(...)
n_design <- c_design %>% mutate(f1 = as.numeric(f1), f2 = as.numeric(f2))

When I try to plot the correlation map on the design with numeric factor levels, I get the following error regarding the data type

plot_correlations(n_design)
Error in array(x, c(length(x), 1L), if (!is.null(names(x))) list(names(x), : 'data' must be of a vector type, was 'NULL'
Traceback:

1. plot_correlations(n_design)
2. cov2cor(getPseudoInverse(t(mm) %*% solve(V) %*% mm))
3. getPseudoInverse(t(mm) %*% solve(V) %*% mm)
4. solve(V)
5. solve.default(V)
6. as.matrix(a)
7. as.matrix.default(a)
8. array(x, c(length(x), 1L), if (!is.null(names(x))) list(names(x), 
 .     NULL) else NULL)

Is the original output from gen_design() a different data type? Any recommendations on converting the string factor levels to integers in a way that is compatible with plot_correlations()?

tylermorganwall commented 6 years ago

There should be nothing special about the output of gen_design, other than some values we store so you don't have to recompute them. Looks like there's just a bug where one of these values has to be recomputed.

An easy workaround is to just pass the design to eval_design() and pass the output of that to plot_correlations().

library(dplyr)

expand.grid(f1=as.factor(c(1,-1)),f2=as.factor(c(1,-1))) -> cand

c_design = skpr::gen_design(cand,~.,12)
n_design = c_design %>% mutate(f1 = as.numeric(f1), f2 = as.numeric(f2))

n_design %>%
  skpr::eval_design(~.,0.2) %>%
  skpr::plot_correlations()
tylermorganwall commented 6 years ago

This is fixed in https://github.com/tylermorganwall/skpr/commit/76f8447c1f3178490e7e5dab183bdebbf59064a7