Learn R Programming

npmr (version 1.3.1)

predict.cv.npmr: Make predictions from a ``cv.npmr'' object

Description

Return predicted reponse class probabilities from a cross-validated NPMR model, using the value of the regularization parameter that led to the minimum cross validation error

Usage

# S3 method for cv.npmr
predict(object, newx, ...)

Value

a matrix giving the predicted probability that each row of newx belongs to each class, corresponding the value of the regularization parameter that led to minimum cross validation error.

Arguments

object

an object of class cv.npmr

newx

covariate matrix on which for which to make response class probability predictions. Must have same number of columns as X used original to fit object.

...

ignored

Author

Scott Powers, Trevor Hastie, Rob Tibshirani

References

Scott Powers, Trevor Hastie and Rob Tibshirani (2016). ``Nuclear penalized multinomial regression with an application to predicting at bat outcomes in baseball.'' In prep.

See Also

cv.npmr, predict.npmr

Examples

Run this code
#   Fit NPMR to simulated data

K = 5
n = 1000
m = 10000
p = 10
r = 2

# Simulated training data
set.seed(8369)
A = matrix(rnorm(p*r), p, r)
C = matrix(rnorm(K*r), K, r)
B = tcrossprod(A, C)            # low-rank coefficient matrix
X = matrix(rnorm(n*p), n, p)    # covariate matrix with iid Gaussian entries
eta = X 
P = exp(eta)/rowSums(exp(eta))
Y = t(apply(P, 1, rmultinom, n = 1, size = 1))
fold = sample(rep(1:10, length = nrow(X)))

# Simulate test data
Xtest = matrix(rnorm(m*p), m, p)
etatest = Xtest 
Ptest = exp(etatest)/rowSums(exp(etatest))
Ytest = t(apply(Ptest, 1, rmultinom, n = 1, size = 1))

# Fit NPMR for a sequence of lambda values without CV:
fit2 = cv.npmr(X, Y, lambda = exp(seq(7, -2)), foldid = fold)

# Compute mean test error using the predict function:
-mean(log(rowSums(Ytest*predict(fit2, Xtest))))

Run the code above in your browser using DataLab