Showing posts with label degrees of freedom. Show all posts
Showing posts with label degrees of freedom. Show all posts

Sunday, September 25, 2016

Effect of step size in forward stagewise regression

Forward stagewise regression is a linear model selection algorithm. It is a modification of least angle regression (LARS; Efron, Hastie, Johnstone & Tibshirani, 2002). It aims to select only a subset of X (predictor) variables, for efficient prediction of a Y (response) variable.

The forward stagewise regression  (FSR) algorithm roughly works as follows:
  1. Initilize by setting i = 0, coefficient vector b_i = 0, and the residual vector rY_i = Y - X * b_i.
  2. Increase (or decrease*) the coefficient of the predictor variable most highly correlated with rY_i by a small step, yielding the current coefficient vector b_{i+1}.
  3. Eat potato chips (optional)
  4. Calculate rY_{i+1} = Y - X * b_{i+1}.
  5. Set i = i + 1, repeat steps 2 - 4 until convergence (i.e., consecutive b_i no longer change).
In step 2, the coefficient is increased or decreased, depending on the sign of the correlation between the predictor variable and rY_i. The step size, that is, the size of the increase (or decrease) in step 2, can be provided by the user, or the optimal value can be determined by cross validation. Step 3 is optional, and should only be performed on a small subset of the iterations.

Although FSR can be performed with the R package lars, I  wanted to program it myself, to check out the effect of different step sizes, and to see how the results compare to ordinary least squares regression (OLS).  I created a function for performing FSR (called swReg), a function for plotting the coefficient paths (called plot.swReg), and applied it on the Boston housing dataset. Here are the code and the results:


R functions for performing FSR, plotting coefficient paths and computing cross-validated prediction error
swReg <- function(X, Y, stepsize = .1, threshold = .1) {
  sX <- scale(X)
  sY <- scale(Y)
  b <- vector(length = ncol(X))
  rY <- sY
  sgn <- 0
  lastsgn <- 0
  index <- 0
  lastindex <- 0
  iteration <- 0
  path <- list()
  while(abs(max(t(rY) %*% sX)) > threshold & (index != lastindex | sgn == lastsgn)) {
    iteration <- iteration + 1
    lastindex <- index
    lastsgn <- sgn
    index <- which(abs(t(rY) %*% sX) == max(abs(t(rY) %*% sX)))
    sgn <- sign((t(rY) %*% sX)[index])
    b[index] <- b[index] + sgn*stepsize
    rY <- sY - sX %*% b
    path[[iteration]] <- b
  }
  bUnstand <- (b/attr(sX, "scaled:scale"))*attr(sY, "scaled:scale") 
  intercept <- attr(sY, "scaled:center") - bUnstand %*% attr(sX, "scaled:center")
  bUnstand <- c(intercept, bUnstand)
  names(bUnstand) <- c("(Intercept)", colnames(X))
  
  return(list(unstandardized.coef = bUnstand, standardized.coef = b,
            iteration = iteration, stepsize = stepsize, threshold = threshold, 
            coef.path = data.frame(matrix(
              unlist(path), ncol = ncol(X), nrow = iteration, byrow = TRUE, 
              dimnames = list(1:iteration, colnames(X)))),
            data = list(X = X, Y = Y))
         )  
}

plot.swReg <- function(object, legend = TRUE) {
  plot(object$coef.path[,1], type = "l", 
  ylim = c(min(object$coef.path), max(object$coef.path)), 
  ylab = "coefficient", xlab = "iteration", 
  main = "Standardized coefficient paths", 
  sub = paste("stepsize =", object$stepsize))
  for (i in 2:ncol(X)) {lines(object$coef.path[,i], col = i)}
  if(legend) {
    legend("topleft", legend = colnames(object$data$X), cex = .5, 
    lty = 1, col = 1:ncol(object$coef.path), y.intersp = .25, 
    x.intersp = .25, bty = "n", seg.len = .5)
  }
}

predict.swReg <- function(object, newdata = object$data$X) {
  cbind(rep(1, times = nrow(newdata)), as.matrix(newdata)) %*% object$unstandardized.coef
}

swReg.xval <- function(object, k = 10, seed = 42) {
  set.seed(seed)
  ids <- peperr::resample.indices(n = nrow(object$data$X), sample.n = 10, method = "cv")
  models <- list()
  xvaldatasets <- list()
  for (i in 1:k){
    xvaldatasets[[i]] <- list(train = list(X = object$data$X[ids$sample.index[[i]],],
                                           Y = object$data$Y[ids$sample.index[[i]]]),
                              test = list(X = object$data$X[ids$not.in.sample[[i]],],
                                          Y = object$data$Y[ids$not.in.sample[[i]]]))
    models[[i]] <- swReg(xvaldatasets[[i]]$train$X, xvaldatasets[[i]]$train$Y, 
                         stepsize = object$stepsize, threshold = object$threshold)
    xvaldatasets[[i]]$test$xvalpredY <- predict.swReg(models[[i]], newdata = xvaldatasets[[i]]$test$X)
  }
  dataset <- cbind(object$data$X, fold = NA, Y = object$data$Y, Ycvpred = NA)
  coefficients <- list()
  for(i in 1:k){
    dataset[,"fold"][ids$not.in.sample[[i]]] <- i
    dataset[,"Ycvpred"][ids$not.in.sample[[i]]] <- xvaldatasets[[i]]$test$xvalpredY 
    coefficients[[i]] <- list(unstandardized.coef = models[[i]]$unstandardized.coef, 
                              standardized.coef = models[[i]]$standardized.coef)
  }
  return(list(dataset = dataset, cv.coefficients = coefficients))
} 

Example: Boston housing data
## Data preparation:
library(MASS)
X <- as.matrix(Boston[,-14])
Y <- Boston$medv

## Run forward stagewise regression:
tmp1 <- swReg(X, Y, st = .2, thr = .2)
tmp2 <- swReg(X, Y, st = .1, thr = .1)
tmp3 <- swReg(X, Y, st = .01, thr = .01)
tmp4 <- swReg(X, Y, st = .001, thr = .001)

## Plot coefficient paths:
par(mfrow=c(2,2))
plot.swReg(tmp1)
plot.swReg(tmp2)
plot.swReg(tmp3)
plot.swReg(tmp4)


Coefficient paths


What we see is that the paths actually look very similar, but less iterations are required before convergence with larger step sizes. Also, larger steps sizes provide sparser solutions (i.e., less non-zero coefficients in the final solution). Note that the algorithm is applied on standardized X and Y values, so the step size could be interpreted as the minimal correlation between a predictor and the response variable that the user finds 'relevant'. The FSR function above provides both standardized and unstandardized regression coefficients.


Comparison with OLS


What we see is: the larger the step size, the sparser the final solution. With step size equal to .2, only 5 predictor variables are selected. With step size equal to .001, all variables are selected. Also, the smaller the step size, the more the final solution resembles the OLS solution. The absolute distances between the OLS and FSR solutions decrease with step size:







References 

Efron, B., Hastie, T., Johnstone, I., & Tibshirani, R. (2004). Least angle regression. The Annals of Statistics, 32(2), 407-499. link to pdf

Tuesday, June 12, 2012

Notes on RuleFit

Edit (dec 24 2017): I have developed an R package for deriving prediction rule ensembles: pre (available from CRAN). It provides most of the functionality of the Rulefit package, with some improvements/adjustments. The main differences with Rulefit are: 1) pre derives prediction rules using unbiased tree growing algorithms from package partykit, instead of CART trees, which have biased variable selection; 2) pre is completely R-based, so easier to use the rules, coefficients, importance etc. for further computations (but also a bit slower); 3) Rulefit allowed for a bagging and/or boosting approach to generating the initial ensemble of prediction rules, pre allows for bagging and/or boosting and/or random forest approaches.

 

Original post:

RuleFit provides an ensemble of regression functions and CART tree derived prediction rules. It's one of the very few rule prediction rule ensemble methods that can be used for both classification and regression (c.f., C4.5). The paper describing the methodology and program can be found here (Friedman, J. H., & Popescu, B. E. (2008). Predictive learning via rule ensembles. The Annals of Applied Statistics, 916-954). The TMVA (Toolkit for Multivariate Analysis) package in R provides an implementation of rulefit as well, but only for classification.

  

Arguments of the rulefit function

sparse relates to \lambda in Friedman & Popescu (2005)
memory.par is \nu in Friedman & Popescu (2005)
samp.fract is \eta in Friedman & Popescu (2005)
tree.size is \overline{L} in Friedman & Popescu (2005)
inter.supp is \kappa in Friedman & Popescu (2005)

The penalty function in the Friedman & Popescu (2005) paper shows only the lasso penalty (equation 4), but elastic net penalty is implemented in RuleFit, as well. If you set the sparse argument of the rulefit function to low values, (e.g., sparse=.0000001), the model will be built using the ridge penalty, resulting in a much larger number of terms in the model. 

samp.fract=min(1,(11*sqrt(neff)+1)/neff), by default. When all observation weights are equal, neff = n for regression; so samp.fract=min(1,(11*sqrt(n)+1)/n). By default, samp.fract becomes smaller when n increases. It becomes less than 1 about when neff>122.
 
test.frac should be a value between 0.1 and 0.5, and is 0.2 by default. Test sample is used for what some would refer to as validation sample: determining the optimal value of \lambda, the penalty parameter.

If test.reps is set to a value >0, then [value]-fold cross validation is used for determining the optimal value of \lambda. By default, test.reps=round(min(20,max(0.0,5200/neff-2))): it is at most 20 and at least 0. By default, the value of test.resps decreases when neff (n) increases. By default, test.reps < 20 when neff > 237, and test.reps < 4 when neff < 866.  

Using the max.trms argument is very useful in controlling the size of the final ensemble. However, the maximum number specified is an approximate maximum number of terms: in most cases, the ensemble will be somewhat bigger, especially when test.reps > 1.

The sparse argument selects the sparse regression method used determining the weights of prediction functions in the final ensemble (thus, it also selects the prediction functions: prediction functions with zero weight are not included in the final ensemble). For ensembles with human-interpretable sizes, use sparse=3: it uses forward stepwise (regression) or stagewise (classification) regression (see Hastie & Taylor, 2007).

Hastie, T., Taylor, J., Tibshirani, R., & Walther, G. (2007). Forward stagewise regression and the monotone lasso. Electronic Journal of Statistics, 1, 1-29.


Other functions

The rfxeval function uses all data, to estimate the expected extra-sample error Err, the average generalization error when the method f^{hat}(X) is applied to an independent test sample (see paragraph 7.10 from Hastie, Tibshirani and Friedman, 2008).



Troubleshooting

Do not use rfxeval() before rfpred() or rules(): it seems to change the current model in the RuleFit home directory. If rfxval() is used, rebuild the rulefit model using rulefit(x,y), before using rules() and rfpred(). I'm not sure this only involves the rules and rfpred functions.

You can obtain a list (instead of a plot) of the variable importances by using the following code:
imp <- varimp(plot=F); imp

The rules() function provides a list of prediction functions, printed in a command prompt window. This is not very convenient for editing, copying and pasting. However, when the rules() function is used, a file named "rulesout.hlp" is created in the working directory, which can be opened using any text editor. Be aware that R and Rulefit will hang, when the final ensemble consists of < 10 prediction functions. I think this is due to the defaults for the rules function (rules(begin=1, end=begin+9). There's three ways to solve this:
1) open windows task manager, and end the process called 'rf_go.exe'; find the file 'rules.out' in the working directory and open it in a text editor.
2) open windows task manager, and end the process called 'rf_go.exe'; make sure the R working directory is the dierctory from which rulefit is run; type 'readLines("rulesout.hlp")' in R.
3) type "rules(1, [some number])", where [some number] is the number of terms in the current RuleFit model.


Lambda value

According to Hastie, Tibshirani and Friedman (2009), cross validation should be used to estimate the shrinkage, or smoothing, parameters. To use cross-validation for determining the lambda parameter (i.o.w., the size of the final model) in the rulefit function, use the test.reps argument. It provides the shrinkage parameter used for model selection, but this is done implicitly, and currently there's no way to obtain the actual lambda value used.

Hastie, T., Tibshirani, R., Friedman, J., Hastie, T., Friedman, J., & Tibshirani, R. (2009). The elements of statistical learning (Vol. 2, No. 1). New York: Springer.


Range of prediction rules: missings

According to the rulefit help file, missing predictor variable values (NAs) are coded 9.0e30 (observations with missing output variable values are of no use to the algorithm, and are excluded from the analysis). In the description of the prediction rules, you may come across something like:

                                                                        
"Rule   1:     2  variables"                                               
"     support =  0.3496      coeff =   1.538      importance =   100.00          "     V1:  range = -0.9900E+36   1.500"                             
"     V2:  range =   1.500      0.9000E+31"                        
"     V3:  range = 0.9000E+31   0.9900+36" 
 

This rule would not apply for observations with missing values for V2 (range does not include values equal to or higher than 0.9e31), but does apply for observations with missing values for V3 (range includes values equal to or higher than 0.9e31).

Tuesday, August 2, 2011

Small sample df's with multiple imputation


When Multiple Imputation (MI) is used in SPSS, output of subsequent analyses of datasets may show huge df-values.
Barnard and Rubin (1999) suggested an adjustment of df-values. Abstract:
"An appealing feature of multiple imputation is the simplicity of the rules for combining the multiple complete-data inferences into a final inference, the repeated-imputation inference (Rubin, 1987). This inference is based on a t distribution and is derived from a Bayesian paradigm under the assumption that the complete-data degrees of freedom, \nu_{com}, are infinite, but the number of imputations, m, is finite. When \nu_{com} is small and there is only a modest proportion of missing data, the calculated repeated-imputation degrees of freedom, \nu_{m}, for the t reference distribution can be much larger than \nu_{com}, which is clearly inappropriate. Following the Bayesian paradigm, we derive an adjusted degrees of freedom, \tilde{\nu_{m}} with the following three properties: for fixed m and estimated fraction of missing information, \tilde{\nu_{m}} monotonically increases in \nu_{com}; \tilde{\nu_{m}} is always less than or equal to \nu_{com}; and\tilde{\nu_{m}} equals \nu_{m} when \nu_{com} is infinite. A small simulation study demonstrates the superior frequentist performance when using \tilde{\nu_{m}} , rather than \nu_{m}."

Formulae
\nu_{com} is complete data df's
m is number of imputations
\nu_{m} is the repeated imputation df's
\tilde{\nu_{m}} is always less than or equal to \nu_{com}

\tilde\nu_{m} = \nu_{m} * ( 1 + \nu_{m}) / (\hat{\nu_{obs}} )^(-1)
in which:
\hat{\nu_{obs}} = \lambda (\nu_{com}) * \nu{com} * (1- \hat{\gamma_{m}} )
\lambda (\nu) = (\nu+1) / (nu+3)
\hat{\gamma_{m}} is approximately the Bayesian fraction of missing information for the unknown quantity of interest. Hard to calculate by hand. An SPSS macro can be found here.



Barnard, J. and Rubin, D.B. (1999). Small-Sample Degrees of Freedom with Multiple Imputation. Biometrika, 86, 4, 948-955.
See also: Van Ginkel, J. R., & Van der Ark, L. A. (2005). SPSS syntax for missing value imputation in test and questionnaire data. Applied Psychological Measurement, 29, 152-153.