Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Including categorical factors in a design #2

Open
rvlenth opened this issue Aug 8, 2018 · 0 comments
Open

Including categorical factors in a design #2

rvlenth opened this issue Aug 8, 2018 · 0 comments

Comments

@rvlenth
Copy link
Owner

rvlenth commented Aug 8, 2018

A user noted that design-generation functions only accommodate quantitative predictors. It'd be nice to have some provision for a response-surface design that can be used with additional categorical factors, e.g., suppliers.

I suggested as a short-term remedy to create a design for the categorical factor(s) and cross it with an RS design. I provided a quick-and-dirty function for this purpose:

# Cross a response-surface design `design` with another design `extras`
# `extras` may be a data frame or a list. If a list, it is converted to a data frame 
# via expand.grid() (thus creating all combinations of the provided factor levels)
# This function does NOT randomize the design!
cross.with = function(design, extras) {
    if (missing(extras))
        return(design)
    if (!is.list(extras))
        stop("'extras' must be a list or data.frame")
    if(!inherits(extras, "data.frame"))
        extras = do.call(expand.grid, extras)
    pieces = lapply(seq_len(nrow(extras)), function(i) {
        des = design
        for (nm in names(extras))
            des[[nm]] = extras[[nm]][i]
        des
    })
    do.call(rbind, pieces)
}

Illustration of use:

> des = rsm::ccd(2, random = FALSE)
> cross.with(des, list(trt = c("A", "B")))
   run.order std.order  x1.as.is  x2.as.is Block trt
1          1         1 -1.000000 -1.000000     1   A
2          2         2  1.000000 -1.000000     1   A
3          3         3 -1.000000  1.000000     1   A
4          4         4  1.000000  1.000000     1   A
5          5         5  0.000000  0.000000     1   A
6          6         6  0.000000  0.000000     1   A
7          7         7  0.000000  0.000000     1   A
8          8         8  0.000000  0.000000     1   A
9          1         1 -1.414214  0.000000     2   A
10         2         2  1.414214  0.000000     2   A
11         3         3  0.000000 -1.414214     2   A
12         4         4  0.000000  1.414214     2   A
13         5         5  0.000000  0.000000     2   A
14         6         6  0.000000  0.000000     2   A
15         7         7  0.000000  0.000000     2   A
16         8         8  0.000000  0.000000     2   A
17         1         1 -1.000000 -1.000000     1   B
18         2         2  1.000000 -1.000000     1   B
19         3         3 -1.000000  1.000000     1   B
20         4         4  1.000000  1.000000     1   B
21         5         5  0.000000  0.000000     1   B
22         6         6  0.000000  0.000000     1   B
23         7         7  0.000000  0.000000     1   B
24         8         8  0.000000  0.000000     1   B
25         1         1 -1.414214  0.000000     2   B
26         2         2  1.414214  0.000000     2   B
27         3         3  0.000000 -1.414214     2   B
28         4         4  0.000000  1.414214     2   B
29         5         5  0.000000  0.000000     2   B
30         6         6  0.000000  0.000000     2   B
31         7         7  0.000000  0.000000     2   B
32         8         8  0.000000  0.000000     2   B

Data are stored in coded form using these coding formulas ...
x1 ~ x1.as.is
x2 ~ x2.as.is
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant