Title: | Construction of factorial designs |
---|---|
Description: | This small library contains a series of simple tools for constructing and manipulating confounded and fractional factorial designs. |
Authors: | Bill Venables |
Maintainer: | Bill Venables <[email protected]> |
License: | GPL-2 |
Version: | 2.0.0 |
Built: | 2024-11-10 06:40:45 UTC |
Source: | CRAN |
Construct confounded designs with specific contrasts confounded with
blocks. The package only directly handles the p^k
case, that is,
all treatment factors having the same (prime) number of levels. Some
simple facilities are provided for combining component designs into
larger ones, thus providing some facilities for generating interesting
designs for the more general case. Some fractional replication is also
possible with the tools proveded.
Package: | conf.design |
Type: | Package |
Version: | 2.0.0 |
Date: | 2013-02-23 |
License: | GPL-2 |
The key functions are conf.design
, rjoin
and
direct.sum
. The help information for these functions contain
some fairly detailed examples.
Some ancillary functions may be of independent interest, for example
primes
for generating prime numbers and factorize
, which
has an experimental design application, but a default method can be used
to factorize (not too large) integers into prime factors.
Bill Venables
Maintainer: Bill Venables <[email protected]>
Collings, B. J. (1989) Quick confounding. Technometrics, v31, pp107-110.
The CRAN task view on Design of Experiments
### Generate a half replicate of a 2^3 x 3^2 experiment. The factors ### are to be A, B, C, D, E. The fractional relation is to be I = ABC ### and the DE effect is to be confounded with blocks. ### First construct the 2^3 design, confounded in two blocks: d1 <- conf.design(c(A = 1, B = 1, C = 1), p=2) ### Next the 3^2 design, with DE confounded in blocks: d2 <- conf.design(c(D = 1, E = 1), p=3) ### Now extract the principal block from the 2^3 design and form the direct ### sum withthe 3^2 design dsn <- direct.sum(subset(d1, Blocks == "0"), d2) head(dsn) ### Blocks A B C Blocksa D E ### 1 0 0 0 0 0 0 0 ### 2 0 0 0 0 0 2 1 ### 3 0 0 0 0 0 1 2 ### 4 0 0 0 0 1 1 0 ### 5 0 0 0 0 1 0 1 ### 6 0 0 0 0 1 2 2 ### ### Combine the two "Blocks" factors into a single block factor: dsn <- within(dsn, { Blocks <- join(Blocks, Blocksa) Blocksa <- NULL }) ### Now to do some checks. as.matrix(replications( ~ .^2, dsn)) ### Blocks 12 ### A 18 ### B 18 ### C 18 ### D 12 ### E 12 ### Blocks:A 6 ### Blocks:B 6 ### Blocks:C 6 ### Blocks:D 4 ### Blocks:E 4 ### A:B 9 ### A:C 9 ### A:D 6 ### A:E 6 ### B:C 9 ### B:D 6 ### B:E 6 ### C:D 6 ### C:E 6 ### D:E 4 ### We can check the confounding by analysing some dummy data: dsn$y <- rnorm(nrow(dsn)) dummyAov <- aov(y ~ A*B*C*D*E + Error(Blocks), data=dsn) summary(dummyAov) ### Error: Blocks ### Df Sum Sq Mean Sq ### D:E 2 8.915 4.458 ### ### Error: Within ### Df Sum Sq Mean Sq ### A 1 2.077 2.077 ### B 1 1.111 1.111 ### C 1 3.311 3.311 ### D 2 1.929 0.964 ### E 2 0.848 0.424 ### A:D 2 3.421 1.711 ### B:D 2 3.231 1.615 ### C:D 2 2.484 1.242 ### A:E 2 0.214 0.107 ### B:E 2 0.006 0.003 ### C:E 2 0.349 0.174 ### D:E 2 1.442 0.721 ### A:D:E 4 2.560 0.640 ### B:D:E 4 4.454 1.114 ### C:D:E 4 7.942 1.986 ### Two of the D:E degrees of freedom are confounded with Blocks, as desired.
### Generate a half replicate of a 2^3 x 3^2 experiment. The factors ### are to be A, B, C, D, E. The fractional relation is to be I = ABC ### and the DE effect is to be confounded with blocks. ### First construct the 2^3 design, confounded in two blocks: d1 <- conf.design(c(A = 1, B = 1, C = 1), p=2) ### Next the 3^2 design, with DE confounded in blocks: d2 <- conf.design(c(D = 1, E = 1), p=3) ### Now extract the principal block from the 2^3 design and form the direct ### sum withthe 3^2 design dsn <- direct.sum(subset(d1, Blocks == "0"), d2) head(dsn) ### Blocks A B C Blocksa D E ### 1 0 0 0 0 0 0 0 ### 2 0 0 0 0 0 2 1 ### 3 0 0 0 0 0 1 2 ### 4 0 0 0 0 1 1 0 ### 5 0 0 0 0 1 0 1 ### 6 0 0 0 0 1 2 2 ### ### Combine the two "Blocks" factors into a single block factor: dsn <- within(dsn, { Blocks <- join(Blocks, Blocksa) Blocksa <- NULL }) ### Now to do some checks. as.matrix(replications( ~ .^2, dsn)) ### Blocks 12 ### A 18 ### B 18 ### C 18 ### D 12 ### E 12 ### Blocks:A 6 ### Blocks:B 6 ### Blocks:C 6 ### Blocks:D 4 ### Blocks:E 4 ### A:B 9 ### A:C 9 ### A:D 6 ### A:E 6 ### B:C 9 ### B:D 6 ### B:E 6 ### C:D 6 ### C:E 6 ### D:E 4 ### We can check the confounding by analysing some dummy data: dsn$y <- rnorm(nrow(dsn)) dummyAov <- aov(y ~ A*B*C*D*E + Error(Blocks), data=dsn) summary(dummyAov) ### Error: Blocks ### Df Sum Sq Mean Sq ### D:E 2 8.915 4.458 ### ### Error: Within ### Df Sum Sq Mean Sq ### A 1 2.077 2.077 ### B 1 1.111 1.111 ### C 1 3.311 3.311 ### D 2 1.929 0.964 ### E 2 0.848 0.424 ### A:D 2 3.421 1.711 ### B:D 2 3.231 1.615 ### C:D 2 2.484 1.242 ### A:E 2 0.214 0.107 ### B:E 2 0.006 0.003 ### C:E 2 0.349 0.174 ### D:E 2 1.442 0.721 ### A:D:E 4 2.560 0.640 ### B:D:E 4 4.454 1.114 ### C:D:E 4 7.942 1.986 ### Two of the D:E degrees of freedom are confounded with Blocks, as desired.
Construct designs with specified treatment contrasts confounded with blocks. All treatment factors must have the sampe (prime) number of levels.
conf.design(G, p, block.name = "Blocks", treatment.names = NULL)
conf.design(G, p, block.name = "Blocks", treatment.names = NULL)
G |
Matrix whose rows define the contrasts to be confounded. The number of columns of |
p |
The common number of levels for each factor. Must be a prime number. |
block.name |
Name to be given to the factor defining the blocks of the design. |
treatment.names |
Name to be given to the treatment factors of the design. If
|
For example in a 3^4
experiment with AB^2C
and
BCD
confounded with blocks (together with their generalized
interactions), the matrix G
could be given by
rbind(c(A = 1, B = 2, C = 1, D = 0), c(A = 0, B = 1, C = 1, D =
1))
For this example, p = 3
Having column names for the G
matrix implicitly supplies the
treatment factor names.
For a single replicate of treatments, blocks are calculated using the confounded contrasts in the standard textbook way. The method is related to that of Collings (1989).
A design with a Blocks
factor defining the blocks and treatment
factors defining the way treatments are allocated to each plot. (Not
in randomised order!)
None.
Collings, B. J. (1989) Quick confounding. Technometrics, v31, pp107-110.
### ### Generate a 3^4 factorial with A B^2 C and B C D confounded with blocks. ### d34 <- conf.design(rbind(c(A = 1, B = 2, C = 1, D = 0), c(A = 0, B = 1, C = 1, D = 1)), p = 3) head(d34) ### Blocks A B C D ### 1 00 0 0 0 0 ### 2 00 1 2 1 0 ### 3 00 2 1 2 0 ### 4 00 2 2 0 1 ### 5 00 0 1 1 1 ### 6 00 1 0 2 1 as.matrix(replications(~ .^2, d34)) ### [,1] ### Blocks 9 ### A 27 ### B 27 ### C 27 ### D 27 ### Blocks:A 3 ### Blocks:B 3 ### Blocks:C 3 ### Blocks:D 3 ### A:B 9 ### A:C 9 ### A:D 9 ### B:C 9 ### B:D 9 ### C:D 9
### ### Generate a 3^4 factorial with A B^2 C and B C D confounded with blocks. ### d34 <- conf.design(rbind(c(A = 1, B = 2, C = 1, D = 0), c(A = 0, B = 1, C = 1, D = 1)), p = 3) head(d34) ### Blocks A B C D ### 1 00 0 0 0 0 ### 2 00 1 2 1 0 ### 3 00 2 1 2 0 ### 4 00 2 2 0 1 ### 5 00 0 1 1 1 ### 6 00 1 0 2 1 as.matrix(replications(~ .^2, d34)) ### [,1] ### Blocks 9 ### A 27 ### B 27 ### C 27 ### D 27 ### Blocks:A 3 ### Blocks:B 3 ### Blocks:C 3 ### Blocks:D 3 ### A:B 9 ### A:C 9 ### A:D 9 ### B:C 9 ### B:D 9 ### C:D 9
Find minimal complete sets of confounded effects from a defining set for symmetric confounded factorial designs. Useful for checking if a low order interaction will be unintentionally confounded with blocks. As in the usual convention, only effects whose leading factor has an index of one are listed.
All factors must have the same (prime) number of levels.
conf.set(G, p)
conf.set(G, p)
G |
Matrix whose rows define the effects to be confounded with blocks, in the
same way as for |
p |
Number of levels for each factor. Must be a prime number. |
The function constructs all linear functions of the rows of G
(over GF(p)), and removes those rows whose leading non-zero component is
not one.
A matrix like G
with a minimal set of confounded with blocks
defined in the rows.
None
### If A B^2 C and B C D are confounded with blocks, then so are A C^2 D ### and A B D^2. G <- rbind(c(A = 1, B = 2, C = 1, D = 0), c(A = 0, B = 1, C = 1, D = 1)) conf.set(G, 3) ### A B C D ### [1,] 1 2 1 0 ### [2,] 0 1 1 1 ### [3,] 1 0 2 1 ### [4,] 1 1 0 2 ### Only three-factor interactions are confounded, so the design is ### presumably useful. as.matrix(replications( ~ .^2, conf.design(G, 3))) ### [,1] ### Blocks 9 ### A 27 ### B 27 ### C 27 ### D 27 ### Blocks:A 3 ### Blocks:B 3 ### Blocks:C 3 ### Blocks:D 3 ### A:B 9 ### A:C 9 ### A:D 9 ### B:C 9 ### B:D 9 ### C:D 9
### If A B^2 C and B C D are confounded with blocks, then so are A C^2 D ### and A B D^2. G <- rbind(c(A = 1, B = 2, C = 1, D = 0), c(A = 0, B = 1, C = 1, D = 1)) conf.set(G, 3) ### A B C D ### [1,] 1 2 1 0 ### [2,] 0 1 1 1 ### [3,] 1 0 2 1 ### [4,] 1 1 0 2 ### Only three-factor interactions are confounded, so the design is ### presumably useful. as.matrix(replications( ~ .^2, conf.design(G, 3))) ### [,1] ### Blocks 9 ### A 27 ### B 27 ### C 27 ### D 27 ### Blocks:A 3 ### Blocks:B 3 ### Blocks:C 3 ### Blocks:D 3 ### A:B 9 ### A:C 9 ### A:D 9 ### B:C 9 ### B:D 9 ### C:D 9
Constructs the direct sum of two or more designs. Each plot of one design is matched with every plot of the other. (This might also be called the Cartesian product of two designs).
direct.sum(D1, ..., tiebreak=letters)
direct.sum(D1, ..., tiebreak=letters)
D1 |
First component design. |
... |
Additional component designs, if any. |
tiebreak |
Series of characters or digits to be used for breaking ties (or repeats) in the variable names in the component designs. Augmented if necessary. |
Each plot of one design is matched with every plot of the next, (if any), and so on recursively.
The direct sum of all component designs.
None.
### Generate a half replicate of a 2^3 x 3^2 experiment. The factors are ### to be A, B, C, D, E. The fractional relation is to be I = ABC and the ### DE effect is to be confounded with blocks. ### First construct the 2^3 design, confounded in two blocks: d1 <- conf.design(cbind(A = 1, B = 1, C = 1), p = 2) ### Next the 3^2 design, with DE partially confounded in blocks: d2 <- conf.design(cbind(D = 1, E = 1), p = 3) ### Now extract the principal block from the 2^3 design and form the direct ### sum withthe 3^2 design dsn <- direct.sum(subset(d1, Blocks == "0"), d2) ### combine block factors into one dsn <- within(dsn, { Blocks <- join(Blocks, Blocksa) Blocksa <- NULL }) head(dsn)
### Generate a half replicate of a 2^3 x 3^2 experiment. The factors are ### to be A, B, C, D, E. The fractional relation is to be I = ABC and the ### DE effect is to be confounded with blocks. ### First construct the 2^3 design, confounded in two blocks: d1 <- conf.design(cbind(A = 1, B = 1, C = 1), p = 2) ### Next the 3^2 design, with DE partially confounded in blocks: d2 <- conf.design(cbind(D = 1, E = 1), p = 3) ### Now extract the principal block from the 2^3 design and form the direct ### sum withthe 3^2 design dsn <- direct.sum(subset(d1, Blocks == "0"), d2) ### combine block factors into one dsn <- within(dsn, { Blocks <- join(Blocks, Blocksa) Blocksa <- NULL }) head(dsn)
The default
method factorizes positive numeric integer arguments,
returning a vector of prime factors. The factor
method can be used to
generate pseudo-factors. It accepts a factor, f
, as principal
argument and returns a data frame with factors fa
, fb
,
... each with a prime number of levels such that f
is model
equivalent to join(fa, fb, ...)
.
## Default S3 method: factorize(x, divisors = primes(max(x)), ...) ## S3 method for class 'factor' factorize(x, name = deparse(substitute(x)), extension = letters, ...)
## Default S3 method: factorize(x, divisors = primes(max(x)), ...) ## S3 method for class 'factor' factorize(x, name = deparse(substitute(x)), extension = letters, ...)
x |
Principal argument. The The |
divisors |
Candidate prime divisors for all numers to be factorized. |
name |
Stem of the name to be given to component pseudo-factors. |
extension |
Distinguishing strings to be added to the stem to nominate the pseudo-factors. |
... |
Additional arguments, if any. (Presently ignored.) |
Primarily intended to split a factor with a non-prime number of levels into a series of pseudo-factors, each with a prime number of levels and which jointly define the same classes as the factor itself.
The main reason to do this would be to confound one or more of the pseudo-factors, or their interactions, with blocks using constructions that only apply for prime numbers of levels. In this way the experiment can be made smaller, at the cost of some treatment contrasts being confounded with blocks.
The default method factorizes integers by a clumsy, though effective enough way for small integers. The function is vectorized in the sense that if a vector of integers to factorize is specified, the object returned is a list of numeric vectors, giving the prime divisors (including repeats) of the given integers respectively.
As with any method of factorizing integers, it may become very slow if the prime factors are large.
For the default
method a vector, or list of vectors, of prime
integer divisors of the components of x
, (including repeats).
For the factor
method, a design with factors having prime
numbers of levels for factor arguments.
None.
conf.design, join
factorize(12321) ### [1] 3 3 37 37 f <- factor(1:6) data.frame(f, factorize(f)) ### f fa fb ### 1 1 0 0 ### 2 2 1 0 ### 3 3 0 1 ### 4 4 1 1 ### 5 5 0 2 ### 6 6 1 2 des <- with(list(f = factor(rep(6:1, 1:6))), data.frame(f, factorize(f))) head(des, 7) ## f fa fb ## 1 6 1 2 ## 2 5 0 2 ## 3 5 0 2 ## 4 4 1 1 ## 5 4 1 1 ## 6 4 1 1 ## 7 3 0 1
factorize(12321) ### [1] 3 3 37 37 f <- factor(1:6) data.frame(f, factorize(f)) ### f fa fb ### 1 1 0 0 ### 2 2 1 0 ### 3 3 0 1 ### 4 4 1 1 ### 5 5 0 2 ### 6 6 1 2 des <- with(list(f = factor(rep(6:1, 1:6))), data.frame(f, factorize(f))) head(des, 7) ## f fa fb ## 1 6 1 2 ## 2 5 0 2 ## 3 5 0 2 ## 4 4 1 1 ## 5 4 1 1 ## 6 4 1 1 ## 7 3 0 1
Joins two or more factors together into a single composite factor defining
the subclasses. In a model formula join(f1, f2, f3)
is equivalent to
f1:f2:f3
.
join(...)
join(...)
... |
Two or more factors or numeric vectors, or lists containing these kinds of component. |
Similar in effect to paste
, which it uses.
A single composite factor with levels made up of the distinct combinations of levels or values of the arguments which occur.
None.
:
, paste
, rjoin
,
direct.sum
within(data.frame(f = gl(2, 3)), { g <- gl(3,2,length(f)) fg <- join(f, g) }) ### f fg g ### 1 1 1:1 1 ### 2 1 1:1 1 ### 3 1 1:2 2 ### 4 2 2:2 2 ### 5 2 2:3 3 ### 6 2 2:3 3
within(data.frame(f = gl(2, 3)), { g <- gl(3,2,length(f)) fg <- join(f, g) }) ### f fg g ### 1 1 1:1 1 ### 2 1 1:1 1 ### 3 1 1:2 2 ### 4 2 2:2 2 ### 5 2 2:3 3 ### 6 2 2:3 3
Generate a table of prime numbers.
primes(n)
primes(n)
n |
A positive integer value, or vector or such values. |
Uses an elementary sieve method.
A vector of all prime numbers less than the max(n)
.
NB: 1 is not a prime number!
None
primes(1:50) ### [1] 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
primes(1:50) ### [1] 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Combine two or more designs with the same names into a single design by row concatenation.
rjoin(..., part.name="Part")
rjoin(..., part.name="Part")
... |
Two or more designs with identical component names. |
part.name |
Name for an additional factor to identify the original components in the result. |
Almost the same as rbind
, but an additional factor in the result
separates the original components.
A single design with the arguments stacked above each other (in a similar
manner to rbind
), together with an additional factor whose levels
identify the original component designs, or parts
.
None.
### A two replicate partially confounded factorial design. d1 <- conf.design(c(A = 1, B = 1, C = 1), 2) d2 <- conf.design(c(A = 0, B = 1, C = 1), 2) dsn <- within(rjoin(d1, d2), { Blocks <- join(Part, Blocks) Part <- NULL }) as.matrix(replications(~ .^2, dsn)) ### [,1] ### Blocks 4 ### A 8 ### B 8 ### C 8 ### Blocks:A 2 ### Blocks:B 2 ### Blocks:C 2 ### A:B 4 ### A:C 4 ### B:C 4
### A two replicate partially confounded factorial design. d1 <- conf.design(c(A = 1, B = 1, C = 1), 2) d2 <- conf.design(c(A = 0, B = 1, C = 1), 2) dsn <- within(rjoin(d1, d2), { Blocks <- join(Part, Blocks) Part <- NULL }) as.matrix(replications(~ .^2, dsn)) ### [,1] ### Blocks 4 ### A 8 ### B 8 ### C 8 ### Blocks:A 2 ### Blocks:B 2 ### Blocks:C 2 ### A:B 4 ### A:C 4 ### B:C 4