Topic: Growth

Topic Description:

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



File: growth/growthfit.graph.r

View File Source Download File No help file available

Function: compare.growthbinmodel

Function Description: compare.growthbinmodel

Calculates various metrics of fit: DIC, BIC, AIC based on the maximum likelihood, AIC based on the mean of the Gibbs sampler, from output of the model fit (all species, all bins 1:4).

Function Arguments:

ArgumentDefault Value
fit
bins1:4
makegraphTRUE
conflines0
newgraphTRUE
exportpdf
outfile"growth/linearbin.bestfit.pdf"
h8
w10

Function Source:


compare.growthbinmodel=function(fit,bins=1:4,makegraph=TRUE,conflines=0,newgraph=TRUE,export=pdf,out
        file="growth/linearbin.bestfit.pdf",h=8,w=10) {
spp=names(fit)

bestbin=numeric()
BIC=DIC=AICopt=AICgibbs=meanllike=optllike=matrix(ncol=length(bins),nrow=length(spp))

colnames(AICopt)=colnames(AICgibbs)=colnames(BIC)=colnames(DIC)=colnames(meanllike)=colnames(optlli
        ke)=pst("bin",bins)
rownames(AICopt)=rownames(AICgibbs)=rownames(BIC)=rownames(DIC)=rownames(meanllike)=rownames(optlli
        ke)=spp
for(i in 1:length(spp))
{
fit.onesp=fit[[spp[i]]]

for(j in 1:length(fit.onesp))
{
BIC[i,j]=calculateBinModel.BIC(fit.onesp[[j]])
DIC[i,j]=calculateBinModel.DIC(fit.onesp[[j]])
AICopt[i,j]=calculateBinModel.AIC(fit.onesp[[j]],type='optim')
AICgibbs[i,j]=calculateBinModel.AIC(fit.onesp[[j]],type='mean')
meanllike[i,j]=mean(fit.onesp[[j]]$llike[fit$keep])
optllike[i,j]=fit.onesp[[j]]$optimllike
}
bestbin[i]=which.max(AICgibbs[i,])
}

if(!is.null(export))
{
on.exit(graphics.off())
export(file=outfile,height=h,width=w)
}

slope=upper=lower=matrix(ncol=4,nrow=length(spp))
rownames(slope)=rownames(upper)=rownames(lower)=spp

for(i in 1:length(spp))
{
fit.onesp=fit[[spp[i]]]
best=bins[bestbin[i]]

if(best==1) plural='bin'
else plural='bins'

if(newgraph & makegraph) x11(height=5,width=9)
if(makegraph)

graph.growthmodel.spp(fit=fit.onesp[[best]],whichpred='pred',graphdiv=15,modelclr="blue",mainti
        tle=paste(spp[i],best,plural),conf=conflines)
if(best==1) slopecol=2
else if(best==2) slopecol=2:3
else if(best==3) slopecol=3:5
else if(best==4) slopecol=4:7

slope[i,1:length(slopecol)]=fit.onesp[[bestbin[i]]]$best[slopecol]
upper[i,1:length(slopecol)]=fit.onesp[[bestbin[i]]]$CI[2,slopecol]
lower[i,1:length(slopecol)]=fit.onesp[[bestbin[i]]]$CI[1,slopecol]

}

return(list(slopes=slope,upper=upper,lower=lower))
}