recode2integer {rms}R Documentation

recode2integer

Description

Create Ordinal Variables With a Given Precision

Usage

recode2integer(y, precision = 7, ftable = TRUE)

Arguments

y

a numeric, factor, or character vector with no NAs

precision

number of places to the right of the decimal place to round y if y is numeric but not integer, for the purpose of finding the distinct values. Real values rounding to the same values under precision are mapped to the same integer output y

ftable

set to FALSE to suppress creation of freq

Details

For a factor variable y, uses existing factor levels and codes the output y as integer. For a character y, converts to factor and does the same. For a numeric y that is integer, leaves the levels intact and codes y as consecutive positive integers corresponding to distinct values in the data. For numeric y that contains any non-integer values, rounds y to precision decimal places to the right before finding the distinct values.

This function is used to prepare ordinal variables for orm.fit() and lrm.fit(). It was written because just using factor() creates slightly different distinct y levels on different hardware because factor() uses unique() which functions slightly differently on different systems when there are non-significant digits in floating point numbers.

Value

a list with the following elements:

Author(s)

Cole Beck

Examples

w <- function(y, precision=7) {
  v <- recode2integer(y, precision);
  print(v)
  print(table(y, ynew=v$y))
}
set.seed(1)
w(sample(1:3, 20, TRUE))
w(sample(letters[1:3], 20, TRUE))
y <- runif(20)
w(y)
w(y, precision=2)

[Package rms version 8.0-0 Index]