constant: Test for Nested Factors

Description Usage Arguments Details Value Note Author(s) References Examples

Description

Various Tests for Nested Factors.

Usage

1
2
3
4
5
6
7
crosses(a, b, all = FALSE)
a %nests% b
a %nested.in% b
a %crosses% b
a %crossed.on% b
## Default S3 method:
constant(x,within=rep(TRUE,length(x)),...)

Arguments

a

a factor, something coercible to a factor, or a list of such having equal lengths (e.g. data.frame)

b

like a, possibly a different number of factors, but same length(s)

all

whether to include first instances: see details

x

an object like a

within

An object like b

...

extra arguments passed to other methods

Details

We propose an idiom pertaining to two factors (or interactions) of equal length, in which either a crosses b, or b nests a. Equivalently, in the former case we could say b is crossed on/by a; in the latter we could say a is nested in b. In the case that b nests a, it must be true that b is constant within a. For example, if treatment nests replicate, then for multiple observations of a single level of replicate, all observations of treatment must be the same. The value of replicate implies the value of treatment (though the reverse need not be true).

Formally, a crosses b if any level of a is associated with more than one level of b. In this case, b is not constant within a. If you are wondering why !constant(x,y), examine crosses(y,x).

crosses() is the basis for all other functions, and is useful for determining positions at which crossing occurs (or nesting fails). For each level of a, it returns FALSE for the first unique value of b, and TRUE for any others. If all is TRUE, however, it returns TRUE for the first unique value of b when there are others.

For the following equivalency table, all assertions in a row are true if any of them is true.

crosses crossed.on nests nested.in constant
a %crosses% b b %crossed.on% a !b %nests% a !a %nested.in% b !constant(b,within = a)
b %crosses% a a %crossed.on% b !a %nests% b !b %nested.in% a !constant(a,within = b)
!a %crosses% b !b %crossed.on% a b %nests% a a %nested.in% b constant(b,within = a)
!b %crosses% a !a %crossed.on% b a %nests% b b %nested.in% a constant(a,within = b)

Value

Logical vector for crosses(); scalar logical for all others.

Note

All these functions are NA-safe; i.e., they treat NA as a distinct level.

Author(s)

Tim Bergsma

References

http://metrumrg.googlecode.com

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
CO2[,c('Type','Treatment')] %nests% CO2$Plant #TRUE
CO2$Plant %nested.in% CO2$Type #TRUE
with(CO2,constant(list(Type,Treatment),within=Plant)) #TRUE
with(ChickWeight,constant(Diet,within=Chick)) #TRUE
with(ChickWeight,constant(weight,within=list(Chick,Time))) #TRUE
crosses(c(1,1,1),c(1,2,3)) # FALSE TRUE TRUE
crosses(c(1,1,1),c(1,2,2)) # FALSE TRUE TRUE
crosses(c(1,1,1),c(2,2,1)) # FALSE FALSE TRUE
crosses(c(1,1,1),c(1,2,3), all=TRUE) # TRUE TRUE TRUE
crosses(c(1,2,3),c(1,1,1)) # FALSE FALSE FALSE
crosses(c(NA,2,3),c(NA,NA,NA)) # FALSE FALSE FALSE
constant(Theoph$Wt,within=Theoph$Subject)#TRUE
trt <- c('a','a','a','b')
rep <- c(1,1,2,3)
trt %crosses% rep # TRUE
rep %nests% trt # FALSE
trt %nests% rep # TRUE
rep %nested.in% trt # TRUE
rep %crosses% trt # FALSE
trt %crossed.on% rep #FALSE
constant(trt,within=rep) # TRUE

metrumrg documentation built on May 2, 2019, 5:55 p.m.