fac.multinested: Creates several factors, one for each level of nesting.fac...

fac.multinestedR Documentation

Creates several factors, one for each level of nesting.fac and each of whose values are either generated within those of a level of nesting.fac or using the values of nested.fac within a levels of nesting.fac.

Description

Creates several factors, one for each level of nesting.fac and each of whose values are either (i) generated within those of the level of nesting.fac or (ii) using the values of nested.fac within the levels of the nesting.fac. For (i), all elements having the same level of nesting.fac are numbered from 1 to the number of different elements having that level. For (ii), the values of nested.fac for a level of nesting.fac are copied. In both cases, for the values of nested.fac not equal to the level of the values of nested.fac for which a nested factor is being created, the levels are set to outlevel and labelled using outlabel. A factor is not created for a level of nesting.fac with label equal to outlabel. The names of the factors are equal to the levels of nesting.fac; optionally fac.prefix is added to the beginning of the names of the factors. The function is used to split up a nested term into separate terms for each level of nesting.fac.

Usage

fac.multinested(nesting.fac, nested.fac = NULL, fac.prefix = NULL, 
                nested.levs = NA, nested.labs = NA, 
                outlevel = 0, outlabel = "rest", ...)

Arguments

nesting.fac

The factor for each of whose levels a nested facor is to be generated, except one is not generated for the outlabel level.

nested.fac

The factor whose values for a level are to be used for the factor being created for that level. If nested.fac is NULL, then the values of the levels is the the list of numbers from 1 to the replication of the level of nesting.fac, represented as characters.

fac.prefix

The prefix to be added to a level in naming a nested factor.

nested.levs

Optional vector of levels for the nested factors. Any data value that does not match a value in nested.levs will be NA in the nested factor being generated. The default value of nested.levs is the list of numbers from 1 to the replication of the levels of nesting.fac, represented as characters. When nested.fac is not NULL, nested.levs is not used.

nested.labs

Optional vector of values to use as labels for the levels of the new nested factors; they are combined with outlabel. The default is as.character(levels).

outlevel

The level to use in the new factor for all values of old factor that do not correspond to the level of the nesting.fac to which the new factor corresponds.

outlabel

The label to use the outlevel level.

...

Further arguments passed to the factor call creating a new factor.

Value

A data.frame containing a factor for each level of nesting.fac.

Note

The levels of nesting.fac do not have to be equally replicated.

Author(s)

Chris Brien

See Also

fac.gen, fac.nested in package dae, factor.

Examples

  lay <- data.frame(A = factor(rep(c(1:3), c(3,6,4)), labels = letters[1:3]))
  lay$B <-fac.nested(lay$A)

  #Add factors for B within each level of A
  lay2 <- cbind(lay, fac.multinested(lay$A))
  canon2 <- designAnatomy(list(~A/(a+b+c)), data = lay2)
  summary(canon2)

  #Add factors for B within each level of A, but with levels and outlabel given
  lay2 <- cbind(lay, fac.multinested(lay$A, nested.levs = seq(10,60,10), outlabel = "other"))
  
  canon2 <- designAnatomy(list(~A/(a+b+c)), data = lay2)
  summary(canon2)
  

  #Replicate the combinations of A and B three times and index them with the factor sample
  lay3 <- rbind(lay,lay,lay)
  lay3$sample <- with(lay3, fac.nested(fac.combine(list(A,B))))
  
  #Add factors for B within each level of A
  lay4 <- cbind(lay3, fac.multinested(nesting.fac = lay$A, nested.fac = lay$B))
  
  canon4 <- designAnatomy(list(~(A/(a+b+c))/sample), data = lay4)
  summary(canon4)
  

  #Add factors for sample within each combination of A and B
  lay5 <- with(lay4, cbind(lay4, 
                           fac.multinested(nesting.fac = a, fac.prefix = "a"),
                           fac.multinested(nesting.fac = b, fac.prefix = "b"),
                           fac.multinested(nesting.fac = c, fac.prefix = "c")))
  
  canon5 <- designAnatomy(list(~A/(a/(a1+a2+a3)+b/(b1+b2+b3+b4+b5+b6)+c/(c1+c2+c3))), data = lay5)
  summary(canon5)

  #Add factors for sample within each level of A
  lay6 <- cbind(lay4, 
                fac.multinested(nesting.fac = lay4$A, nested.fac = lay$sample, fac.prefix = "samp"))
  canon6 <- designAnatomy(list(~A/(a/sampa+b/sampb+c/sampc)), data = lay6)
  summary(canon6)

dae documentation built on Aug. 7, 2023, 5:08 p.m.