Typically, coding formulas are of the form x ~ (var - center) / halfwd
where x
and var
are variable names, and center
and
halfwd
are numbers.
The left-hand side gives the name of the coded variable, and the right-hand side
should be a linear expression in the uncoded variable (linearity is not explicitly
checked, but nonlinear expressions will not decode correctly.) If coded.data
is called without formulas, automatic codings are created (along with a warning message). Automatic codings are based on transforming all non-block variables having five or fewer unique values to the interval [-1,1]. If no formulas are provided in as.coded.data
, default coding formulas like those for cube
are created all numeric variables with mean zero -- again with a warning message.
An S3 print
method is provided for the coded.data
class;
it displays the data.frame in either coded or
decoded form, along with the coding formulas. Some users may prefer print.data.frame
or as.data.frame
in lieu of print
with decode=FALSE; they produce the
same output without displaying the coding formulas.
Use coded.data
to convert a data.frame
in which the variables
are on their original scales. The variables named in the formulas are
coded and replaced with their coded versions (and also renamed).
In contrast, as.coded.data
does not modify any of the data; it assumes the variables
are already coded, and the coding information is simply added. In addition, if data
is
already a coded.data
object from a pre-1.41 version of rsm,
it is converted to
be compatible with new capabilities such as djoin
(no formulas
argument
is needed in this case). Any blocking factors should be specified in the blocks
argument.
decode.data
converts a dataset of class coded.data
and
returns a data.frame
containing the original variables.
recode.data
is used to convert a coded.data
object to new codings.
Important: this changes the coded values to match the new coding formulas. If you want to keep the coded values the same, but change the levels they represent, use codings(object) <- \dots or dupe
.
code2val
converts coded values to the original scale using the codings provided,
and returns an object of the same class as X
.
val2code
converts the other direction. When using these functions, it is
essential that the names (or column names in the case of matrices) match those of the
corresponding coded or uncoded variables.
codings
is a generic function for accessing codings. It
returns the list of coding formulas from a coded.data
object. One may use an
expression like codings(object) <- list(\dots) to change the codings (without changing
the coded values themselves). See also codings.rsm
.
is.coded.data(x)
returns TRUE
if x
inherits from coded.data
, and FALSE otherwise.
The extraction function x[...]
and the naming functions names<-
,
truenames
, and truenames<-
are provided to preserve the integrity of
codings. For example, if x[, 1:3]
excludes any coded columns, their coding formulas
are also excluded. If all coded columns are excluded, the return value is unclassed
from coded.data
. When variable names are changed using names(x) <- ...
, the coding
formulas are updated accordingly. The truenames
function returns the names of the
variables in the decoded dataset. We can change the decoded names using
truenames(x) <- ...
, and the coding formulas are updated. Note that truenames
and truenames<-
work the same as names
and names<-
for
unencoded variables in the object.
Another convenient way to copy and change the coding formulas a coded dataset (and optionally re-randomize it) is to use the dupe
function with a coding
argument.
When a design is created in another package, some of the variables may be factor
s, in which case they are converted using as.numeric
(values of 1, 2, ...). These levels may be regarded as a yet different coding of the variables, and so it may take two steps to get it in the desired form: one to convert the supplied levels to the desired range (often -1 to 1), and the other to replace the coding formulas to correspond to the real values of the variables to be used. See the examples.