Topic: Utilities

Topic Description:

Basic R utilities used in many packages and functions, such as date and string manipulations, statistical distributions, geometry of lines and distances. The R package date is required for the two data functions.

Function: residual.llike.modelBayes

Function Description: residual.llike.modelBayes

Calculate likelihood of residual standard deviation, given observations plus the predicting model and data (to make predictions). This likelihood does not depend on the hyperparameters. It does require data and prediction.

Function Arguments:

ArgumentDefault Value
test
whichtest
data
trueN
model
sdpar
fullpar
sdmodel
errormodel'Gauss'
debugFALSE
...

Sample Usage:

residual.llike.modelBayes=function(test,whichtest,data,trueN,model,sdpar,fullpar,sdmodel,badparam,errormodel='Gauss',debug=FALSE,...){ param=arrangeParam.llike(test,sdpar,whichtest) if(!is.null(badparam)) if(badparam(x=data,param=param,...)) return(-Inf) norand=length(data) modeled=model(x=data,param=fullpar,...) if(errormodel=='Gauss' | errormodel=='NegBinom') withinSD=sdmodel(x=data,param=param) else if(errormodel=='GaussMultResid') withinSD=sdmodel(x=modeled,param=param) if(length(which(withinSD<=0))>0) return(-Inf) browser() if(errormodel=='Gauss' | errormodel=='GaussMultResid') all.llike=dnorm(x=drp(trueN),mean=modeled,sd=withinSD,log=TRUE) els

Function Source:

rormodel='Gauss',debug=FALSE,...) {
param=arrangeParam.llike(test,sdpar,whichtest)

norand=length(data)

modeled=model(x=data,param=fullpar,...)
if(errormodel=='Gauss' | errormodel=='NegBinom') withinSD=sdmodel(x=data,param=param)
else if(errormodel=='GaussMultResid') withinSD=sdmodel(x=modeled,param=param)
if(length(which(withinSD<=0))>0) return(-Inf)
# browser()

if(errormodel=='Gauss' | errormodel=='GaussMultResid') all.llike=dnorm(x=drp(trueN),mean=modeled,sd
=withinSD,log=TRUE) else if(errormodel=='NegBinom') all.llike=dnbinom(x=drp(trueN),mu=modeled,size=withinSD,log=TRUE)
else if(errormodel=='Pois') all.llike=dpois(x=drp(trueN),lambda=modeled,log=TRUE)
# all.llike=dnorm(x=drp(trueN),mean=modeled,sd=withinSD*modeled,log=TRUE)
if(is.na(sum(all.llike))) browser()
llike=sum(all.llike)

return(sum(llike))
}