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 |
precision |
number of places to the right of the decimal place to round |
ftable |
set to |
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:
-
y
: vector of integer-codedy
-
ylevels
: vector of corresponding originaly
values, possibly rounded toprecision
. This vector is numeric unlessy
isfactor
or character, in which case it is a character vector. -
freq
: frequency table of rounded or categoricaly
, withnames
attribute for the (possibly rounded)y
levels of the frequencies -
median
: mediany
from original values if numeric, otherwise median of the new integer codes fory
-
whichmedian
: the integer valuedy
that most closely corresponds tomedian
; for an ordinal regression model this represents one plus the index of the intercept vector corresponding tomedian
.
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)