Learn R Programming

rms (version 7.0-0)

recode2integer: recode2integer

Description

Create Ordinal Variables With a Given Precision

Usage

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

Value

a list with the following elements:

  • y: vector of integer-coded y

  • ylevels: vector of corresponding original y values, possibly rounded to precision. This vector is numeric unless y is factor or character, in which case it is a character vector.

  • freq: frequency table of rounded or categorical y, with names attribute for the (possibly rounded) y levels of the frequencies

  • median: median y from original values if numeric, otherwise median of the new integer codes for y

  • whichmedian: the integer valued y that most closely corresponds to median; for an ordinal regression model this represents one plus the index of the intercept vector corresponding to median.

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

Author

Cole Beck

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.

Examples

Run this code
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)

Run the code above in your browser using DataLab