R/My_predict.R

Defines functions My_predict

Documented in My_predict

#'Use the model generated by "LogReg" to predict new test data
#'
#'@param newx input matrix, of dimension n (sample number) by p (variable number);
#' each row is an observation vector.
#'@param fit the model generated by "LogReg".
#'@return the prediction for every row of test data newx. And the label will be the same as that of training data.
#'@examples
#'
#'### install and library package
#'#devtools::install_github("hobbitish1028/Logistic")
#'library(Logistic)
#'
#'### Generate training data
#'sigma<-4
#'set.seed(123)
#'n<-1e4
#'p<-1e2
#'mu1<-rnorm(p)
#'mu2<-rnorm(p)
#'X1<-matrix(mu1+rnorm(n*p,0,sigma),n,p,byrow = TRUE)
#'X2<-matrix(mu2+rnorm(n*p,0,sigma),n,p,byrow = TRUE)
#'
#'### Train data
#'X<-rbind(X1,X2)
#'y<-rep(c(1,0),each=n)
#'
#'
#'### Test data
#'test_x<-rbind( matrix(mu1+rnorm(n*p,0,sigma),n,p,byrow = TRUE), 
#'               matrix(mu2+rnorm(n*p,0,sigma),n,p,byrow = TRUE)  )
#'test_y<-rep(c(1,0),each=n)
#'
#'### Fit model
#'fit<-Logreg(X,y)
#'
#'### Make prediction on test data
#'pred0<-My_predict(fit,newx = test_x)
#'
#'@export
#'   

My_predict<-function(fit,newx){
  n<-dim(newx)[1]
  X<-cbind(rep(1,n),newx)
  p<-dim(X)[2]
  result <- X%*%fit$x
  tmp <-rep(fit$label[2],n)
  tmp[which(result>0)]<-fit$label[1]
  return( tmp)
}
hobbitish1028/Logistic documentation built on Nov. 26, 2019, 8:58 a.m.