Diferenças
Aqui você vê as diferenças entre duas revisões dessa página.
Ambos lados da revisão anteriorRevisão anteriorPróxima revisão | Revisão anterior | ||
publications:papercompanions:ejpjmstaa [2008/05/09 05:56] – ernesto | publications:papercompanions:ejpjmstaa [2008/05/09 06:39] (atual) – ernesto | ||
---|---|---|---|
Linha 1: | Linha 1: | ||
==== Jardim, E. and Ribeiro Jr., P.J. Submitted. Modelling Spatio-temporal Abundance at Age with Bayesian Geostatistics and Compositional Data Analysis. Canadian Journal of Fisheries and Aquatic Science ==== | ==== Jardim, E. and Ribeiro Jr., P.J. Submitted. Modelling Spatio-temporal Abundance at Age with Bayesian Geostatistics and Compositional Data Analysis. Canadian Journal of Fisheries and Aquatic Science ==== | ||
- | === Paper companion === | + | ==== Paper companion |
- | == R code == | + | === R code === |
+ | <code R> | ||
+ | require(geoR) | ||
+ | require(MASS) | ||
+ | require(FLCore) | ||
+ | ############################################################################ | ||
+ | # => DESIGN ESTIMATES <= # | ||
+ | ############################################################################ | ||
+ | |||
+ | inds.nhat <- matrix(inds.orig$nhat[inds.orig$age< | ||
+ | inds.nhat <- FLQuant(inds.nhat, | ||
+ | inds.varnhat <- matrix(inds.orig$varnhat[inds.orig$age< | ||
+ | inds.varnhat <- FLQuant(inds.varnhat, | ||
+ | inds.cv <- sqrt(inds.varnhat)/ | ||
+ | |||
+ | ############################################################################ | ||
+ | # => COMPOSITIONAL DATA ANALYSIS <= # | ||
+ | ############################################################################ | ||
+ | require(compositions) | ||
+ | |||
+ | # data (age 2 last) | ||
+ | data.comp <- split(cd8906[, | ||
+ | data.acomp <- lapply(data.comp, | ||
+ | |||
+ | # compute compositions | ||
+ | data.acomp <- lapply(data.acomp, | ||
+ | |||
+ | # parametric bootstrap | ||
+ | data.comps <- lapply(data.acomp, | ||
+ | xt <- alr(x)[] | ||
+ | mu <- apply(xt, | ||
+ | sig <- sqrt(apply(xt, | ||
+ | rho <- cor(xt) | ||
+ | sigmu <- rho*sig%*%t(sig) | ||
+ | mat <- mvrnorm(1000, | ||
+ | alr.inv(mat) | ||
+ | }) | ||
+ | |||
+ | detach() | ||
+ | |||
+ | ############################################################################ | ||
+ | # => GEOSTATISTICS <= # | ||
+ | ############################################################################ | ||
+ | |||
+ | # | ||
+ | # GLM | ||
+ | # | ||
+ | |||
+ | # data object | ||
+ | cd8906.glm <- transform(cd8906, | ||
+ | n05h=a0+a1+a2+a3+a4+a5, | ||
+ | n05o=(a0+a1+a2+a3+a4+a5)*dur/ | ||
+ | n05d=((a0+a1+a2+a3+a4+a5)*dur/ | ||
+ | |||
+ | cd8906.glm <- cd8906.glm[, | ||
+ | |||
+ | # glm | ||
+ | n05h.glm <- glm(round(n05h, | ||
+ | |||
+ | # residuals and predictions | ||
+ | n05h.res <- residuals(n05h.glm) | ||
+ | newdata <- expand.grid(y=factor(1989: | ||
+ | n05h.pred <- predict(n05h.glm, | ||
+ | names(n05h.pred) <- 1989:2006 | ||
+ | n05h.pred <- apply(cd8906.glm, | ||
+ | |||
+ | # check normality | ||
+ | sum(unlist(lapply(tapply(log(cd8906.glm$n05h+0.1), | ||
+ | sum(unlist(lapply(tapply(n05h.pred, | ||
+ | |||
+ | # | ||
+ | # GEOSTATS | ||
+ | # | ||
+ | |||
+ | # geodata | ||
+ | n05hp.gd <- mkgd(cbind(cd8906.glm[, | ||
+ | |||
+ | # likfit | ||
+ | n05hp.lf <- lapply(n05hp.gd, | ||
+ | |||
+ | # bayes | ||
+ | require(VGAM) | ||
+ | PC <- prior.control( | ||
+ | phi.prior = " | ||
+ | tausq.rel.prior = dzipois(0: | ||
+ | ) | ||
+ | detach() | ||
+ | |||
+ | MC <- model.control() | ||
+ | OC <- output.control(n.predictive = 1000, messages=FALSE) | ||
+ | kbtg <- split(as.character(1989: | ||
+ | |||
+ | n05hp.kb <- lapply(kbtg, | ||
+ | v <- date() | ||
+ | cat(x,":", | ||
+ | gd <- n05hp.gd[[x]] | ||
+ | krige.bayes(gd, | ||
+ | }) | ||
+ | |||
+ | # | ||
+ | # SPATIAL PREDICTIONS: | ||
+ | # | ||
+ | |||
+ | # non rotated data | ||
+ | gd.lst <- mkgd(cbind(y=cd8906.glm$y, | ||
+ | OC <- output.control(n.predictive=1, | ||
+ | |||
+ | n05hp.kr <- split(as.character(1989: | ||
+ | n05hp.kr <- lapply(n05hp.kr, | ||
+ | cat(x, sep="," | ||
+ | gd <- gd.lst[[x]] | ||
+ | pars <- n05hp.kb[[x]]$posterior$sample | ||
+ | v <- apply(pars, 1, function(y){ | ||
+ | cat(" | ||
+ | KC <- krige.control(cov.model = " | ||
+ | nugget = y[2]*y[4], beta=y[1]) | ||
+ | krige.conv(gd, | ||
+ | }) | ||
+ | sims <- lapply(v," | ||
+ | sims <- do.call(" | ||
+ | list(locations=gr, | ||
+ | }) | ||
+ | |||
+ | ############################################################################ | ||
+ | # => ALL TOGETHER NOW ! ESTIMATE ABUNDANCE AT AGE <= # | ||
+ | ############################################################################ | ||
+ | |||
+ | # | ||
+ | # SIMULATIONS | ||
+ | # | ||
+ | |||
+ | # initialize objects | ||
+ | iay.arr <- array(dim=c(1, | ||
+ | iaya.arr <- array(dim=c(6, | ||
+ | |||
+ | # beta simulations | ||
+ | n05hp.iay <- lapply(n05hp.kb, | ||
+ | n05.iay <- iay.arr | ||
+ | for(i in names(n05hp.iay)) n05.iay[, | ||
+ | |||
+ | # abundance at age simulations | ||
+ | lst <- split(as.character(1989: | ||
+ | n05hp.iaya <- lapply(lst, function(x) n05hp.iay[[x]]*data.comps[[x]][, | ||
+ | |||
+ | n05.iaya <- iaya.arr | ||
+ | for(i in names(n05hp.iaya)) n05.iaya[, | ||
+ | |||
+ | # | ||
+ | # SUMMARY STATS & ABUNDANCE INDEX | ||
+ | # | ||
+ | |||
+ | n05.iay.med <- apply(n05.iay, | ||
+ | n05.iay.mad <- apply(n05.iay, | ||
+ | n05.iay.madmed <- n05.iay.mad/ | ||
+ | |||
+ | n05.iaya.med <- apply(n05.iaya, | ||
+ | n05.iaya.mad <- apply(n05.iaya, | ||
+ | n05.iaya.madmed <- n05.iaya.mad/ | ||
+ | |||
+ | ############################################################################ | ||
+ | # => THAT'S ALL FOLKS ! <= # | ||
+ | ############################################################################ | ||
+ | |||
+ | </ | ||