# R/stepAICmle.fun.r In NHPoisson: Modelling and Validation of Non Homogeneous Poisson Processes

#### Documented in stepAICmle.fun

stepAICmle.fun<-function(ImlePP, covariatesAdd=NULL, startAdd=NULL, direction='forward',...)
{

if ((direction!='backward')&(direction!='forward')&(direction!='both'))
stop('Direction argument not valid, it must be one  of: backward, forward,
or both')

ncov<-dim(ImlePP@covariates)[2]
if ( is.null(ncov)) ncov<-0
if ( (is.null(dimnames(ImlePP@covariates)[[2]]))&(ncov>=1) ) dimnames(ImlePP@covariates)<-list(NULL, paste('Covariate',c(1:ncov)) )

i<-1
dropnames<-NULL

tind<-ImlePP@tind
if(direction=='backward')
{

AICold<-Inf
AICnew<--Inf
mlePPaux<-ImlePP
covariates<-ImlePP@covariates

while ((AICold>=AICnew)&( (dim(covariates)[2]+(tind==TRUE)) >1))
{
cat(fill=T)
cat(fill=T)
cat('Step Backward', i, fill=T)
cat(fill=T)
auxdrop<-dropAIC.fun(mlePPaux,...)
AICold<-auxdrop$AICcurrent j<-auxdrop$posminAIC
AICnew<-auxdrop$AICdrop[j] if (AICold>=AICnew) { covariates<-checkdim(covariates,j=j) start<-as.list(mlePPaux@coef[-j]) names(start)<-paste('b',(c(0:(length(start)-1))+(tind==FALSE)),sep='') mlePPaux<-update(mlePPaux, covariates=covariates, start=start,modCI=FALSE,modSim=TRUE, dplot=FALSE) cat('This drop improves the model', fill=T) dropnames<-c(dropnames,auxdrop$namecov)
}
else cat('This drop  does not improve the model', fill=T)
i<-i+1

} #end while
cat(fill=T)
cat(fill=T)
cat('Final model', fill=T)
cat(fill=T)
cat('The covariates dropped from the initial model are: ', fill=T)
print(dropnames)
cat(fill=T)
}

if(direction=='forward')
{

if (is.null(covariatesAdd)) stop('For the forward direction, a matrix   must be provided in the argument covariatesAdd')

AICold<-Inf
AICnew<--Inf
mlePPaux<-ImlePP

{
cat(fill=T)
cat(fill=T)
cat('Step Forward', i, fill=T)

AICold<-auxadd$AICcurrent j<-auxadd$posminAIC
AICnew<-auxadd$AICadd[j] if (AICold>=AICnew) { covariates<-cbind(mlePPaux@covariates,covariatesAdd[,j]) addnames<-c(addnames,auxadd$namecov)
dimnames(covariates)<-list(NULL, c(dimnames(mlePPaux@covariates)[[2]],auxadd$namecov) ) start<-c(as.list(mlePPaux@coef), auxadd$newCoef)
mlePPaux<-update(mlePPaux, covariates=covariates, start=start,modCI=FALSE,modSim=TRUE, dplot=FALSE)

cat('This covariate improves the model', fill=T)

}
else cat('This covariate does not improve the model', fill=T)
i<-i+1

} #end while
cat(fill=T)
cat(fill=T)
cat('Final model', fill=T)
cat(fill=T)
cat('The covariates added to the initial model are: ', fill=T)
cat(fill=T)

}

if(direction=='both')
{

if (is.null(covariatesAdd)) stop('For the both direction, a matrix   must be provided in the argument covariatesAdd')
mlePPaux<-ImlePP
covariates<-ImlePP@covariates
changeF<-1
changeB<-1
mlePPaux<-ImlePP

while (max(changeF, changeB)>0)
{
# Forward step
{
cat(fill=T)
cat(fill=T)
cat('Step Forward', i, fill=T)
AICold<-auxadd$AICcurrent j<-auxadd$posminAIC
AICnew<-auxadd$AICadd[j] if (AICold>=AICnew) { covariates<-cbind(mlePPaux@covariates,covariatesAdd[,j]) dropaddnames<-c(dropaddnames,auxadd$namecov)
dimnames(covariates)<-list(NULL, c(dimnames(mlePPaux@covariates)[[2]],auxadd$namecov) ) start<-c(as.list(mlePPaux@coef), auxadd$newCoef)
mlePPaux<-update(mlePPaux, covariates=covariates, start=start,modCI=FALSE,modSim=TRUE, dplot=FALSE)
cat('This covariate improves the model', fill=T)

}
else
{
cat('This covariate does not improve the model', fill=T)
changeF<-0
}
}

#Backward step

if ((dim(covariates)[2]+(tind==TRUE)) >1)
{
cat(fill=T)
cat('Step Backward', i, fill=T)
auxdrop<-dropAIC.fun(mlePPaux,...)
AICold<-auxdrop$AICcurrent j<-auxdrop$posminAIC
AICnew<-auxdrop$AICdrop[j] if (AICold>=AICnew) { covariatesAdd<-cbind(covariatesAdd, covariates[,j]) dropaddnames<-c(dropaddnames,auxdrop$namecov)
covariates<-checkdim(covariates,j=j)
start<-as.list(mlePPaux@coef[-j])
names(start)<-paste('b',(c(0:(length(start)-1))+(tind==FALSE)),sep='')
mlePPaux<-update(mlePPaux, covariates=covariates, start=start,modCI=FALSE,modSim=TRUE, dplot=FALSE)
cat('This drop improves the model', fill=T)
}
else
{
cat('This drop  does not improve the model', fill=T)
changeB<-0
}
}
i<-i+1
} #end while
cat(fill=T)
cat(fill=T)
cat('Final model', fill=T)
cat(fill=T)
cat('The covariates added and dropped to the initial model are: ', fill=T)
cat(fill=T)

} #end if both

cat(fill=T)
cat('Summary of the model: ', fill=T)
print(summary(mlePPaux))
cat(fill=T)
return(mlePPaux)
}


## Try the NHPoisson package in your browser

Any scripts or data that you put into this service are public.

NHPoisson documentation built on Feb. 19, 2020, 5:07 p.m.