Topic: Growth

Topic Description:

Functions for analyzing tree growth, designed for the standard CTFS R Analytical Tables.



File: growth/growthfit.bin.r

View File Source Download File No help file available

Function: growth.flexbin

Function Description: growth.flexbin

Fitting a regression line through log growth against log dbh, binning on dbh. Growth table has 2 columns, one named size (ie, dbh), other growth. The function now calls optim first to find a set of parameters, then, if Gibbs is selected, runs the Gibbs sampler starting with those parameters. Since optim fails if the initial likelihood is -Inf, it became necessary to check the parameters with bad.binparam before running.

Function Arguments:

ArgumentDefault Value
growthtable
sizecol"dbh"
nobin2
startNULL
startsdNULL
sdmodellinear.model.ctr
badsdfuncNULL
method'Gibbs'
rep1200
show100
burn200
...

Function Source:


growth.flexbin=function(growthtable,sizecol="dbh",nobin=2,start=NULL,startsd=NULL,sdmodel=linear.mod
        el.ctr,badsdfunc=NULL, method='Gibbs',rep=1200,show=100,burn=200,...)
{
size=growthtable[,sizecol]
growthtable$growth

if(is.null(start)) start=c(defineBinBreaks(size,nobin,...),rep(0.1,nobin),0)
if(is.null(startsd)) startsd=c(0.02,0)

bad=bad.binparam(param=start,x=size,pred=NULL,...)
if(bad) start=c(defineBinBreaks(size,nobin,...),rep(0.1,nobin),0)
bad=FALSE
if(!is.null(badsdfunc)) bad=badsdfunc(size,param=startsd)
if(bad) startsd=defineSDpar(size,length(startsd))

fit=optim(par=c(start,startsd),fn=llike.linearbin.optim,control=list(fnscale=(-1)),
x=size,y=growthtable$growth,predfunc=linearmodel.bin,nomainpar=length(start),

llikefunc=llike.GaussModel,sdfunc=sdmodel,badpredpar=bad.binparam,badsdpar=badsdfunc,...)
        
fit$best=best.optim=fit$par[1:length(start)]
fit$bestSD=bestSD.optim=fit$par[-(1:length(start))]
bestllike.optim=fit$value
bestpred=linearmodel.bin(size,param=best.optim,...)
fit$model=data.frame(x=size,y=growthtable$growth,pred=bestpred)
fit$model=fit$model[order(fit$model$x),]

if(method=='Gibbs')

fit=model.xy(x=size,y=growthtable$growth,predfunc=linearmodel.bin,llikefunc=llike.GaussModel,badpr
        edpar=bad.binparam,
start.predpar=best.optim,sdfunc=sdmodel,start.sdpar=bestSD.optim,llikefuncSD=llike.Ga
        ussModelSD,badsdpar=badsdfunc, steps=rep,burnin=burn,showstep=show,...)

fit$llike.best=

llike.linearbin.optim(param=c(fit$best,fit$bestSD),x=size,y=growthtable$growth,predfunc=linearmod
        el.bin,nomainpar=length(start),
llikefunc=llike.GaussModel,sdfunc=sdmodel,badpredpar=bad.binparam,badsdpar=
        badsdfunc,...)
fit$growth=growthtable
fit$bins=nobin
fit$optimpar=best.optim
fit$optimSD=bestSD.optim
fit$optimllike=bestllike.optim
optimal=which.max(fit$llike[fit$keep])
fit$max.param=fit$fullparam[fit$keep,][optimal,]

return(fit)
}