Learn R Programming

polyreg (version 0.0.0.9600)

getPoly: Get polynomial terms

Description

Generate polynomial terms of predictor variables for a data frame or data matrix.

Usage

getPoly(xydata, deg, maxInteractDeg = deg, del0cols=TRUE)

Arguments

xydata

Data matrix or data frame without response variable.

deg

The max degree of power terms.

maxInteractDeg

The max degree of nondummy interaction terms.

del0cols

Delete all-0 columns from output. If TRUE, determine such columns during computation; if a character vector, delete the columns with those names.

Value

The return value of getPoly is a polyMatrix object. This is an S3 class containing a data frame xdata of the generated polynomial terms. The predictor variables have column names V1, V2, etc. The object also contains a vector retainedNames, the names of the non-all-0 columns.

Details

The getPoly function takes in a data frame or data matrix and generates polynomial terms of predictor variables.

Note the subtleties involving dummy variables. The square, cubic and so on terms are the same as the original variable, and the various duplicates must be eliminated.

Similarly, after dummy variable are created from a categorical variable having more than two levels, the resulting columns will be orthogonal to each other. In almost all cases, the resulting all-0 columns should be eliminated, hence the argument del0cols. In almost all cases, this argument should be set to TRUE at the training stage, and then in predictions one should use the vector of names in the retainedNames component in the return value; predict.polyFit does the latter automatically, so getPoly must set del0cols to TRUE in the training phase.

Examples

Run this code
# NOT RUN {
x1 <- 1:4
z1 <- c(0,1,0,0)
z2 <- c(1,0,0,0)
z3 <- c(0,0,1,0)
xz <- cbind(x1,z1,z2,z3)
getPoly(xz,2)
# xdata component of output:
#   V1 V2 V3 V4 V5 V9 V10 V11
# 1  1  0  1  0  1  0   1   0
# 2  2  1  0  0  4  2   0   0
# 3  3  0  0  1  9  0   0   3
# 4  4  0  0  0 16  0   0   0
# V1-V4 are copies of x1,z1,z2,z3, 1st-degree terms; V5 is x1^2; V9 is
# product x1 * z2; etc.; note that, V6-V8 were  not retained

# }

Run the code above in your browser using DataLab