tylermorganwall/skpr

Bug in eval_design

ugroempi opened this issue · 3 comments

Hi Tyler,

plan is a data frame with only numeric variables. The code below throws an error:

> eval_design(plan, formula(~.), 0.01)
Error in [.design(design, lapply(design, class) %in% c("character", :
i has wrong length

This is due to the presetting of contrasts in function eval_design:
for (x in names(design[lapply(design, class) %in% c("character",
"factor")])) {
if (!is.null(attr(design[[x]], "contrasts"))) {
presetcontrasts[[x]] = attr(design[[x]], "contrasts")
}
}

Proposed fix: names(design)[...]) {, i.e. move ")".

Best, Ulrike

Hi Ulrike,

Thank you for the bug report! I am having trouble reproducing this on my end. Can you provide are more specific example for the plan data.frame that results in the error?

> install.packages("skpr")
packageskprsuccessfully unpacked and MD5 sums checked
> library(skpr)
> expand.grid(a=c(1,-1),b=c(1,-1),c=c(1,-1)) %>% eval_design(formula(~.), 0.01)
    parameter            type     power
1 (Intercept)    effect.power 0.2157059
2           a    effect.power 0.2157059
3           b    effect.power 0.2157059
4           c    effect.power 0.2157059
5 (Intercept) parameter.power 0.2157059
6           a parameter.power 0.2157059
7           b parameter.power 0.2157059
8           c parameter.power 0.2157059

I am not getting an error. Confirming the variables are all type numeric:

> expand.grid(a=c(1,-1),b=c(1,-1),c=c(1,-1)) %>% lapply(class)
$a
[1] "numeric"

$b
[1] "numeric"

$c
[1] "numeric"

Hi Tyler,
sorry, my problem description was wrong: the design has numeric factors only but comes with a cube and star block captured in a single factor, i.e. the problem occurs for designs with only one non-numeric variable. I tried to reproduce it with an expand.grid example but failed. Thus, there must be something special about applying the code to a data.frame object produced with my package, which has class design (first) and class data.frame and comes with some attributes.
Here is the entire code:

require(DoE.wrapper)
require(skpr)
plan <- ccd.design(4)
str(plan)
plan %>% lapply(class)

## produces the error
plan[,-1] %>% eval_design(formula(~.), 0.01)
## because of current code line
names(plan[lapply(plan, class) %in% c("character", "factor")])
## would work
names(plan)[lapply(plan, class) %in% c("character", "factor")]
## or
names(plan[, lapply(plan, class) %in% c("character", "factor"), drop=FALSE])

Thank you for the clarification. This should be fixed in 0.63.1 (commit 2c30d4f).