# BINARY LOGISTIC REGRESSION ESTIMATION USING A QUADRATIC PENALTY. Returns # the maximum penalized likelihood estimate for the parameters of a logistic # regression model. The inputs in the training cases are in X, which should # be a matrix (or convertable to a matrix) with one row per training case, # and one column per input variable. The training targets are in y, which # should be a vector of 0/1 values. The coefficients (but not the intercept) # are penalized by lambda (default 0) times the sum of their squares. # # The first element of the parameter vector returned is the intercept; the # remaining elements are the coefficients of the inputs. lr.est = function (y, X, lambda=0) { X = as.matrix(X) # Convert from data frame, if necessary result = nlm (function (beta) -lr.log.like(y,X,beta) + lambda*sum(beta[-1]^2), rep(0,ncol(X)+1), iterlim=200) beta = result$estimate attr(beta,"max.log.lik") = -result$minimum # tack on max value as attr beta } # LOGISTIC REGRESSION LOG LIKELIHOOD. Computes the log probability of the # training data with inputs X and targets y, using the parameters in beta. lr.log.like = function (y, X, beta) { X = as.matrix(X) ys = 2*y-1 lin = as.vector(beta[1] + X %*% beta[-1]) sum (-log(1+exp(-lin*ys))) } # MAKE PREDICTIONS USING LOGISTIC REGRESSION COEFFICIENTS. Returns the # probability that y=1 for each of the test cases whose inputs are in X, # using the intercept and coefficient parameters in beta. lr.pred = function (X, beta) { X = as.matrix(X) lin = as.vector (beta[1] + X %*% beta[-1]) 1/(1+exp(-lin)) }