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.



File: utilities/distributions.r

View File Source Download File No help file available

Function: fit.pdf

Function Description: fit.pdf

Fit a random variable x to any submitted probability distribution. The number of start parameters must match what the pdf needs.

Function Arguments:

ArgumentDefault Value
start.paramc(1,1)
data
pdfdnorm
xrangec(0,1)
badpardefault.badpar

Function Source:

fit.pdf=function(start.param=c(1,1),data,pdf=dnorm,xrange=c(0,1),badpar=default.badpar)
{
if(length(start.param)==1)
{
optim.pdf = function(param,x,func,bad)
{
if(bad(par)) return(-Inf)
return(sum(pdf(x,param,log=TRUE)))
}
fit=optimize(f=optim.pdf,x=data,interval=xrange,bad=badpar,maximum=TRUE)
return(fit$maximum)
}
if(length(start.param)==2)
{
optim.pdf = function(param,x,func,bad)
{
if(bad(param)) return(-Inf)
return(sum(pdf(x,param[1],param[2],log=TRUE)))
}
fit=optim(par=start.param,fn=optim.pdf,x=data,func=pdf,bad=badpar,control=list(fnscale=-1))
return(fit$par)
}

return('start.param must be length 1 or 2')
}