CTFSworkshop(data=TRUE) grwfull300$dbhctr=(grwfull300$dbh1-mean(grwfull300$dbh1)) mod=lmer(LnGrowth~1+dbhctr+(1+dbhctr|Species),data=grwfull300) xyplot(LnGrowth~dbhctr|Species,data=grwfull300) dotplot(ranef(mod),postVar=TRUE) dotplot(ranef(mod,postVar=TRUE)) xyplot(ht~dbh|species,data=treeht) xyplot(lht~ldb|species,data=treeht) xyplot(lht~ldb|species,data=treeht, panel = function(x,y) { panel.xyplot(x,y) panel.abline(lm(y~x)) } ) head(treemass) options(width=75) head(treemass) colnames(treemass) mod=lmer(logagb~1+logdbh+(1+logdbh|Latin)+(1+logagb|ForestType),data=treemass) treemass$ldbhctr=treemass$logdbh-mean(treemass$logdbh) mod=lmer(logagb~1+ldbhctr+(1+ldbhctr|Latin)+(1+ldbhctr|ForestType),data=treemass) fixef(mod) coef(mod)$ForestType head(coef(mod)$Latin) colnames(treemass) table(treemass$Locality) mod=lmer(logagb~1+ldbhctr+(1+ldbhctr|Latin)+(1+ldbhctr|Locality),data=treemass) display(mod) head(coef(mod)$Local) xyplot(logagb~logdbh|Locality,data=treemass) xyplot(logagb~logddbh|ForestType,data=treemass) xyplot(logagb~logdbh|ForestType,data=treemass) xyplot(logagb~logdbh|ForestType,data=treemass, panel = function(x,y) { panel.xyplot(x,y) panel.abline(lm(y~x)) } ) colnames(grwfull300) options(width=60) colnames(grwfull300) mod=lmer(LnGrowth=1+dbhctr+(1+dbhctr|Species),data=grwfull300) mod=lmer(LnGrowth~1+dbhctr+(1+dbhctr|Species),data=grwfull300) fixef(mod) dotplot(ranef(mod,postVar=TRUE)) dotplot(ranef(mod)) dotplot(ranef(mod,postVar=TRUE)) display(mod,digits=5) dotplot(ranef(mod,postVar=TRUE),xlim=c(-.005,.005)) xyplot(LnGrowth~dbhctr|Species,data=grwfull300) xyplot(ht~dbh|species,treeht) xyplot(lht~ldb|species,treeht) xyplot(lht~ldb|species,data=treeht, panel = function(x,y) { panel.xyplot(x,y) panel.abline(lm(y~x)) } ) mod2=lmer(LnGrowth~1+dbhctr+(1+dbhctr|Species)+(1+dbhctr|census),data=grwfull300) mod3=lmer(LnGrowth~1+dbhctr+(1|Species)+(1|census),data=grwfull300) display(mod2,digits=5) dotplot(ranef(mod2,postVar=TRUE)) dotplot(ranef(mod2,postVar=TRUE),xlim=c(-.005,.005)) dotplot(ranef(mod2,postVar=TRUE),xlim=c(-.001,.001)) coef(mod2)$census dotplot(ranef(mod2,postVar=TRUE)) lm(LnGrowth~census,data=grwfull300)$coef coef(mod2)$census head(coef(mod2)$Species) mod2=lmer(LnGrowth~1+dbhctr+(1+dbhctr|Species)+(1+dbhctr|census),data=grwfull300) str(sleepstudy) xyplot(Reaction~Days|Subject,data=sleepstudy) plot(Reaction~Days,data=sleepstudy) modslp=lmer(Reaction~1+Days+(1+Days|Subject),data=sleepstudy) coef(modslp)$Sub display(modslp) colnames(treemass) fit=lm(ldbhctr~logdbh,data=treemass) aic(fit) AIC(fit) fit=lm(ldbhctr~1+logdbh,data=treemass) AIC(fit) fit=lm(ldbhctr~1+ldbhctr,data=treemass) fit=lm(logagb~1+ldbhctr,data=treemass) AIC(fit) fit=lm(logagb~1+logdbh,data=treemass) AIC(fit) fit=lm(logagb~1+ldbhctr+I(ldbhctr^2),data=treemass) fit=lm(logagb~1+ldbhctr,data=treemass) fitq=lm(logagb~1+ldbhctr+I(ldbhctr^2),data=treemass) AIC(fit,fitq) mod=lmer(logagb~1+ldbhctr+I(ldbhctr^2)+(1+ldbhctr+I(ldbhctr^2)|Species),data=treemass) mod=lmer(logagb~1+ldbhctr+I(ldbhctr^2)+(1+ldbhctr+I(ldbhctr^2)|species),data=treemass) mod=lmer(logagb~1+ldbhctr+I(ldbhctr^2)+(1+ldbhctr+I(ldbhctr^2)|Latin),data=treemass) AIC(fit,fitq,mod) dim(subset(treemass,is.na(Latin))) dim(subset(treemass,!is.na(Latin))) mod=lmer(logagb~1+ldbhctr+(1+ldbhctr|Latin),data=treemass) AIC(mod,fit) fit=lm(logagb~1+ldbhctr,data=treemass) mod=lmer(logagb~1+logdbh+(1+logdbh|Latin),data=treemass) fit=lm(logagb~1+logdbh,data=treemass) AIC(mod,fit) fit=lm(logagb~1+ldbhctr,data=treemass) mod=lmer(logagb~1+logdbh+(1+logdbh|Latin),data=treemass) AIC(mod,fit) treemassSp=subset(treemass,!is.na(Latin)) dim(treemass) dim(treemassSp ) fit=lm(logagb~1+logdbh,data=treemassSp) mod=lmer(logagb~1+logdbh+(1+logdbh|Latin),data=treemassSp) AIC(fit,mod) plot(logagb~lodbh,data=treemassSp) plot(logagb~logdbh,data=treemassSp) modLoc=lmer(logagb~1+logdbh+(1+logdbh|Latin)+(1+logdbh|Locality),data=treemassSp) anova(mod,modLoc) fitFT=lm(logagb~1+logdbh+ForestType,data=treemassSp) coef(fitFT) fitFT=lm(logagb~1+logdbh+ForestType+logdbh*ForestType,data=treemassSp) fitFT=lm(logagb~1+logdbh+ForestType,data=treemassSp) fitFTInt=lm(logagb~1+logdbh+ForestType+logdbh*ForestType,data=treemassSp) coef(fitFTInt) cfdry=coef(fitFTInt)[1:2] cfdr cfdry cfmoist=cfdry+coef(fitFTInt)[c(3,5)] cfmoist cfwet=cfdry+coef(fitFTInt)[c(4,6)] plot(logagb~logdbh,data=treemassSp,cex=.3) abline(cfdry,col='red',lwd=2) abline(cfmoist,col='green',lwd=2) abline(cfwet,col='blue',lwd=2) modFT=lmer(logagb~1+logdbh+(1+logdbh|ForestType),data=treemassSp) coef(modFT) cfdry cfmoist cfwet table(treemassSp$ForestType) AIC(modFT,fitFTInt) modmej=lmer(logagb~1+logdbh+ForestType+ForestType*logdbh+ (1+logdbh|Latin)+(1+logdbh|Locality),data=treemassSp) (1+logdbh|Latin)+(1+logdbh|Locality),data=treemassSp,verbose=TRUE) modmej=lmer(logagb~1+logdbh+ForestType+ForestType*logdbh+ (1+logdbh|Latin)+(1+logdbh|Locality),data=treemassSp,verbose=TRUE) display(modmej) hmod6=lmer(lht ~ ldb + I(ldb^2) + (1+ldb+I(ldb^2)|species), data=treeht) str(treeht) treeht$species=as.factor(treeht$species) xyplot(lht~ldb|species, data=treeht, subset=species%in%levels(species)[1:25], ran=coef(hmod6)$species, fix = fixef(hmod6), panel = function(x, y, ran, fix) { panel.xyplot(x,y) x1 = seq(-3,3,0.5) i=packet.number() x2 = ran[i,1] * 1 + ran[i,2]*x1 + ran[i,3]*x1^2 x3 = fix[1] * 1 + fix[2]*x1 + fix[3]*x1^2 panel.lines(x2~x1, col='red') panel.lines(x3~x1, lty=3) } ) treemassSp=subset(treemass,!is.na(Latin)) savehistory('~/meetings_workshops/Rmodeling/Cenpat/history12OctAM.txt')