A coarsened factor is
an extended version of a factor or ordered factor whose elements
may be fully observed, partially observed or missing. The
partially-observed and missing states are represented by extra levels
which are interpreted as groupings of the fully observed states.
Coarsened factors are specifically designed for modeling with the
cvam
package.
coarsened(obj, levelsList = list(), warnIfCoarsened = TRUE)is.coarsened(x)
# S3 method for coarsened
print(x, quote = FALSE, max.levels = NULL,
width = getOption("width"), ...)
# S3 method for coarsened
droplevels(x, ...)
# S3 method for coarsened
relevel(x, ...)
# S3 method for coarsened
reorder(x, ...)
# S3 method for coarsened
rep(x, ...)
# S3 method for coarsened
[(x, ...)
# S3 method for coarsened
[[(x, ...)
# S3 method for coarsened
[(x, ...) <- value
# S3 method for coarsened
[[(x, ...) <- value
coarsened
returns a coarsened factor.
is.coarsened
returns TRUE
if x
is a coarsened
factor and FALSE
otherwise.
a factor or ordered factor to be converted to a coarsened factor
a named list that defines the groupings of
levels(obj)
to indicate states of partial knowledge
if TRUE
, a warning is issued if
obj
is already a coarsened factor
a coarsened factor or other object
logical, indicating whether or not strings should be printed with surrounding quotes
integer, indicating how many base levels and
coarse levels should be printed for a coarsened factor; if 0
, no
extra base levels or coarse levels lines will be printed. The
default, NULL
, entails choosing max.levels
such that
the base levels and coarse levels each print on one line of width
width
only used when max.levels
is NULL
; see
above
additional arguments passed to or from other methods
character: a set of levels for replacement
Joe Schafer Joseph.L.Schafer@census.gov
A coarsened factor, which inherits from class "factor"
or
c("ordered", "factor")
, has two types of levels: base
levels, which represent states of complete knowledge, and
coarse levels, which represent states of incomplete
knowledge. Each coarse level maps to two or more
base levels. The mapping is defined by the argument levelsList
.
For example, consider a factor whose levels are
c("red", "notRed", "green", "yellow")
, where "notRed"
denotes an
observation that is either "green"
or "yellow"
. When the
factor is converted to a coarsened factor, c("red", "green",
"yellow")
becomes the baseLevels
, and "notRed"
becomes
an element of coarseLevels
. To produce this result, the
argument levelsList
should have a component named
"notRed"
, whose value is c("green", "yellow")
.
The last coarse level is NA
, denoting an observation
that could belong to any of the base levels. The NA
coarse level is
created automatically. Calling coarsened
with an empty
levelsList
(the default) produces a coarsened factor with
NA
as its only coarse level.
If the main argument to coarsened
is already a coarsened
factor, then a warning is issued (if warnIfCoarsened
is
TRUE
) and the coarsened factor is returned unchanged.
The generic functions droplevels
, relevel
,
and reorder
should not be applied to coarsened factors;
the S3 methods droplevels.coarsened
, relevel.coarsened
,
and reorder.coarsened
will prevent this from happening.
rep.coarsened
is a method for the generic function rep
that ensures the special attributes of a coarsened factor are preserved.
Extraction and replacement methods `[`
and `[[`
are also
provided to preserve the special attributes of coarsened factors.
For more information, refer to the package vignette Understanding Coarsened Factors in cvam.
cvam
,
is.naCoarsened
,
baseLevels
,
dropCoarseLevels
fac <- factor( c("red", "green", NA, "yellow", "notRed", "green") )
cFac <- coarsened( fac,
levelsList = list("notRed" = c("green", "yellow")) )
print(cFac)
# extraction and replacement
print( cFac[2:3] )
cFac[2:3] <- c("NA", "green")
Run the code above in your browser using DataLab