View source: R/recode2integer.r
| recode2integer | R Documentation |
Create Ordinal Variables With a Given Precision
recode2integer(y, precision = 7, ftable = TRUE)
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 |
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.
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.
Cole Beck
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.