R/logSMpred.R

logSMpred <-
function(x,y,pts,fr=2,LP=TRUE,xout=FALSE,outfun=outpro,SEED=TRUE,...){
#
# A smoother designed specifically for binary outcomes
# LP=TRUE: With two independent variables, smooth the initial smooth using LOESS
# Return predicted probability of 1 for points in
# pts
# based on the data in x and y
#
#  SEED=TRUE, so that cov.mve always gives the same result. Otherwise
#  esimates can differ slightly when using this function again using
#  the same data, due to the random number generator used by cov.mve
#
if(SEED)set.seed(2)
x=as.matrix(x)
pts=as.matrix(pts)
p=ncol(x)
p1=p+1
xx<-elimna(cbind(x,y))
x<-xx[,1:p]
y<-xx[,p1]
if(xout){
m<-cbind(x,y)
flag<-outfun(x,plotit=FALSE,...)$keep
m<-m[flag,]
x<-m[,1:p]
y<-m[,p1]
}
x=as.matrix(x)
library(MASS)
m=cov.mve(x)
phat<-NA
m1=matrix(NA,nrow=nrow(pts),ncol=nrow(pts))
yhat=NULL
for(i in 1:nrow(pts)){
d<-mahalanobis(x,pts[i,],m$cov)
flag=sqrt(d)<=fr
w=flag*exp(-1*d)
yhat[i]=sum(w*y)/sum(w)
}
yhat
}
musto101/wilcox_R documentation built on May 23, 2019, 10:52 a.m.