1predict.coxnet=function(object,newx,s=NULL,type=c("link","response","coefficients","nonzero"),exact=FALSE,offset,...){ 2 type=match.arg(type) 3 ###coxnet has no intercept, so we treat it separately 4 if(missing(newx)){ 5 if(!match(type,c("coefficients","nonzero"),FALSE))stop("You need to supply a value for 'newx'") 6 } 7 if(exact&&(!is.null(s))){ 8 lambda=object$lambda 9 which=match(s,lambda,FALSE) 10 if(!all(which>0)){ 11 lambda=unique(rev(sort(c(s,lambda)))) 12 object=update(object,lambda=lambda) 13 } 14 } 15 16 nbeta=object$beta 17 if(!is.null(s)){ 18 vnames=dimnames(nbeta)[[1]] 19 dimnames(nbeta)=list(NULL,NULL) 20 lambda=object$lambda 21 lamlist=lambda.interp(lambda,s) 22 nbeta=nbeta[,lamlist$left,drop=FALSE]*lamlist$frac +nbeta[,lamlist$right,drop=FALSE]*(1-lamlist$frac) 23 dimnames(nbeta)=list(vnames,paste(seq(along=s))) 24 } 25 if(type=="coefficients")return(nbeta) 26 if(type=="nonzero")return(nonzeroCoef(nbeta,bystep=TRUE)) 27 nfit=as.matrix(newx%*%nbeta) 28 if(object$offset){ 29 if(missing(offset))stop("No offset provided for prediction, yet used in fit of glmnet",call.=FALSE) 30 nfit=nfit+array(offset,dim=dim(nfit)) 31 } 32 switch(type, 33 response=exp(nfit), 34 link=nfit 35 ) 36} 37