quantify_x {lazy.tree}R Documentation

Quantification of a Categorical Regressor Variable

Description

This function creates a set of quantified regressor variables from a categorical regressor variable.
Japanese help file: quantify_x_JPH

Usage

quantify_x(y, x, ux, loc, Y, method = 1, remove_redundant = 1)

Arguments

y

The criterion variable

x

The categorical regressor variable (as numeric)

ux

The unique values of x

loc

The locations of the unique values (ux) in x.

Y

The dichotomized criterion matrix for method=4

method

Treatment of the categorical regressor variable.
= 0 to dummy expand the categorical regressor,
= 1 to use the first canonical varialbe,
= 2 to use up to the 2nd canonical variables and their sum and difference, cr = 3 to use one-versus-rest dichotomization.
= 4 to use dichotomization of the criterion variable on the basis of all possible bipartitions of the levels of the criterion variable.

remove_redundant

= 0 not to remove the redundant and zero variance columns.

Details

If the criterion is continuous or the # of levels of the criterion is equal to two, the conditional mean of the criterion given the level of regressor will be used as the quantified regressor.
(The # of quantified regressor = 1.)

Regardless of the method, if the criterion is continuous or the # of levels of the criterion is equal to two, the conditional mean of the criterion given the level of regressor will be used as the quantified regressor.
(The # of quantified regressor = 1.)

If method=0 and if the # of levels of the criterion is greater than 2, a categorical regressor will be dummy expanded and the resulting dummy variables will be used as the quantified regressor variables. (The # of quantified regressor = # of levels of the regressor)

If method=1 When the # of levels of the criterion is greater than 2, the first canonical variable of the canonical discriminant analysis of the criterion on the dummy expanded regressor will be used.
(The # of quantified regressor = 1.)

If method=2, in addition to the above, the 2nd canonical variable, if exists, and the sum and difference of those two canonical variables will be added as the quantified regressors.
(The # of quantified regressors = 4 or 1.)

If method=3, the one-versus-rest dichotomization of the criterion variable is used and the conditional means will be used as the quantified regressors. If any of the resulting quantified variables has zero variance or duplicated, the redundant quantified variables will be removed.
(The # of quantified regressors = # of levels of the criterion or less.)

If method=4, all possible bipartitions of the levels of the criterion is used for the dichotomization and the conditional means will be used as the quantified regressors.
If any of the resulting quantified variables has zero variance or duplicated, the redundant quantified variables will be removed.
The columns of the quantified regressor matrices correspond to the set of all bipartitions of the levels of the criterion variable.
(The # of quantified regressors = 2^(nc-1)-1 or less where nc is the # of levels of the criterion variable.)
See the example below for details.

method=4 seems to be the best option when the # of levels of the criterion is not so large ( <= 10 or so. ) and the # of levels of the regressor is very large.

Value

A list of:

\code{ncatx} # of levels of the regressor
\code{ncaty} # of levels of the criterion
\code{newux} Quantified regressor variable in a matrix with
 dyplicated and zero-variance columns removed.
\code{candisc} = -2 if \code{method=4} is used.
= -1 if no quantification is done.
= 0 if the conditional mean of the criterion is used.
= 1 if the canonical variables is used.

Examples

# iris: categorical y, categorical X
Species <- iris$Species
levels(Species) <- c("set","ver","vir")
Xc <- iris[,-5]
Xc[,3] <- cut(Xc[,3],9, labels=1:9)
Xc[,4] <- cut(Xc[,4],9, labels=1:9)

y <- Species
x <- as.numeric(Xc[,3])
temp <- Unique(x)
ux <- temp$value
loc <- temp$loc

q1 <- quantify_x( y, x, ux, loc, method=1 )$newux
q2 <- quantify_x( y, x, ux, loc, method=2 )$newux

Print(q1,q2, fmt="5.2")



# explanatory example of the method.
#
# criterion y as factor.
y <- as.factor( LETTERS[c(1,2,3,4,4,4,2,2,1,1,2,3,4,3,1,3,4,2,2,2)] )
# regressor x as numeric even if it is categorical.
x <- c(1,2,3,4,5, 1,2,3,4,5, 1,2,3,4,5, 1,2,3,4,5 )

# # of criterion categories and # of obs
ncaty <- length(unique(y))
n <- length(y)

# unique value of x and their locations
temp <- Unique(x)
ux <- temp$value
loc <- temp$loc

# method 1 and 2
q1 <- quantify_x( y, x, ux, loc, method=1 )$newux
q2 <- quantify_x( y, x, ux, loc, method=2 )$newux

Print(ux,q1,q2, fmt="6.3")

# convert them to rank order to check redundancy.
q1 <- t(unique(t(apply( round(q1,3), 2, rank, ties.method="min" ))))
q2 <- t(unique(t(apply( round(q2,3), 2, rank, ties.method="min" ))))

cat("Note that q3 was dropped since its rank order is the same as q2.\n")
Print(ux,q1,q2, fmt="4.0")

# Details of method=3 and method=4 quantification:
#
# In reality, Y will be generated in DecTree function, and
# conversion to rank will be done in Divide function.
#

# 1) preparation of Y matrix: all the bipartitions of y levels
id <- gen01pat( ncaty, sort=-2, partition=1 ) == 1  # logical matrix
npart <- nrow(id)
uy <- unique(y)
Y <- matrix(0,n,npart)
for(k in 1:npart ){
 Y[,k] <- y %in% uy[id[k,]]
}
colnames(Y) <- paste("qx",1:ncol(Y), sep="")
cat("\nThe Base of the Dichotomization of the criterion\n")
printm(y,Y, x)

# 2) conditional mean of dichotomized y according to Y matrix
uxnew <- matrix(0,length(ux),ncol(Y))
for( i in 1:length(ux) ){
 uxnew[i,] <- colMeans(Y[loc[[i]],,drop=0])
}
cat("\nNote that some columns may have zero variance and some columns may"
    , " be duplicated.\n")
colnames(uxnew) <- paste("qx",1:ncol(uxnew), sep="")
rownames(uxnew) <- paste("x",1:nrow(uxnew), sep="")
Print(ux, uxnew)

# By quantify_x function (Redundant columns removed.)
q3 <- quantify_x( y, x, ux, loc, Y[,1:ncaty], method=3 )$newux
q4 <- quantify_x( y, x, ux, loc, Y, method=4 )$newux
cat("Note that q4 was removed.\n")
Print(ux,q3,q4, fmt="5.2")

# convert them to rank order
q3 <- t(unique(t(apply( q3, 2, rank, ties.method="min" ))))
q4 <- t(unique(t(apply( q4, 2, rank, ties.method="min" ))))
cat("In this case, all the rank orders are different.\n")
Print(ux,q3,q4, fmt="4.0")



[Package lazy.tree version 0.1.6 ]