Diferenças
Aqui você vê as diferenças entre duas revisões dessa página.
Próxima revisão | Revisão anterior | ||
artigos:ernesto3:sim [2008/09/23 10:18] – criada ernesto | artigos:ernesto3:sim [2008/10/13 13:00] (atual) – ernesto | ||
---|---|---|---|
Linha 1: | Linha 1: | ||
======Simulation study ====== | ======Simulation study ====== | ||
+ | |||
+ | ===== Algorithm ===== | ||
<code R> | <code R> | ||
+ | ################################################################################################# | ||
+ | # | ||
+ | # PAPER COMPANION: SIMULATION STUDY | ||
+ | # | ||
+ | # Paulo Ribeiro Jr. < | ||
+ | # Date: 20081013 | ||
+ | # | ||
+ | # association between age compositions and the age aggregated abundance. | ||
+ | # Tree options are simulated: (i) no association, | ||
+ | # (iii) strong association. | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | ################################################################################################# | ||
+ | |||
+ | # R libraries | ||
library(geoR) | library(geoR) | ||
library(lattice) | library(lattice) | ||
library(MASS) | library(MASS) | ||
+ | library(RandomFields) | ||
+ | library(compositions) | ||
- | ## definindo | + | #============================================================================================== |
- | gs <- expand.grid((0: | + | # SIMULATING THE PROCESSES |
+ | # | ||
+ | # grid | ||
+ | gs <- expand.grid((0: | ||
+ | # size of processes | ||
n <- nrow(gs) | n <- nrow(gs) | ||
+ | # number of replicates | ||
+ | nsim <- 250 | ||
- | ## Modelo: variáveis com mu dependente e correlação componente espacial parcialmente comum | + | #---------------------------------------------------------------------------------------------- |
- | ## Y1 = mu1 + S00 + S11 + e1 = mu1 + sig00 R + sig11 R11 + e1 | + | # STEP 1 : gaussian processes to build abundance and compositions |
- | ## Y2 = mu2 + S00 + S22 + e2 = mu1 + sig00 R + sig22 R22 + e2 | + | #---------------------------------------------------------------------------------------------- |
- | ## Y3 = mu3 + S00 + S33 + e3 = mu1 + sig00 R + sig33 R33 + e3 | + | |
- | ## | + | |
- | ## sig00 + sig## = 1; e# = 1 | + | |
- | ## mu ~ MVG(c(mu1, mu2, mu3), Sigma) | + | |
- | ## mu1 = mu2 = mu3 = 1 | + | |
- | ## | + | |
- | nsim <- 100 | + | # object |
- | set.seed(475) | + | arr0 <- array(NA, dim=c(n,7,nsim), dimnames=list(loc=1: |
+ | # variance-covariance matrix | ||
+ | s2 <- 0.5 | ||
+ | Sig <- diag(c(1, | ||
+ | Sig[1,4] <- 0.45; Sig[4,1] <- 0.45; Sig[1,6] <- 0.9; Sig[6,1] <- 0.9; Sig[6,4] <- 0.5; Sig[4,6] <- 0.5 | ||
+ | Sig <- Sig*s2 | ||
+ | # simulation | ||
+ | set.seed(111) | ||
+ | for(i in 1:nsim){ | ||
+ | arr0[,,i] <- mvrnorm(nrow(gs), | ||
+ | } | ||
+ | |||
+ | # | ||
+ | # STEP 2: generate 250 replicates of a log-gaussian spatial process (Diggle and Ribeiro Jr, 2007) | ||
+ | # | ||
+ | # | ||
- | ## parâmetros do componente comum (S00) | + | # Generate a spatial Gaussian process Z |
- | sig00 <- 0.9 | + | phi <- 0.2 |
- | phi00 <- sig00 | + | sigmasq <- 0.5 |
+ | set.seed(222) | ||
+ | Z <- grf(grid=gs, | ||
+ | # build a log gausian process Y | ||
+ | Ysim <- Z | ||
+ | Ysim$data <- exp(Z$data+arr0[, | ||
+ | # Y characteristics | ||
+ | Ysim.lmean | ||
+ | Ysim.lvar <- apply(log(Ysim$data), | ||
+ | Ysim.lnhat | ||
- | ## parâmetros dos componentes individuais | + | #---------------------------------------------------------------------------------------------- |
- | sig11 <- sig22 <- sig33 <- 1-sig00 | + | # STEP 3: build compositions |
- | phi11 <- phi22 <- phi33 <- 1-phi00 | + | # |
- | mu1 <- mu2 <- mu3 <- 1 | + | |
- | ## simulando S | + | # objects |
- | S00 <- grf(grid=gs, cov.pars=c(sig00, phi00), nsim=nsim) | + | arr00 <- array(1, dim=c(n,3,nsim), dimnames=list(loc=1: |
- | S11 <- grf(grid=gs, cov.pars=c(sig11, phi11), nsim=nsim) | + | Psim <- array(NA, dim=c(n,3,nsim,3), dimnames=list(loc=1: |
- | S22 <- grf(grid=gs, cov.pars=c(sig22, phi22), nsim=nsim) | + | # option 1: no association between compositions and age aggregated abundance |
- | S33 <- grf(grid=gs, cov.pars=c(sig33, phi33), nsim=nsim) | + | arr00[, |
+ | Psim[,,,1] <- aperm(apply(arr00,c(1,3),function(x) x/ | ||
+ | # option 2: week association between compositions and age aggregated abundance | ||
+ | arr00[, | ||
+ | Psim[,,,2] <- aperm(apply(arr00,c(1,3),function(x) x/ | ||
+ | # option 3: strong association between compositions and age aggregated abundance | ||
+ | arr00[, | ||
+ | Psim[,,,3] <- aperm(apply(arr00, | ||
+ | # P characteristics | ||
- | ## simulando mu | + | #---------------------------------------------------------------------------------------------- |
- | ## ruídos | + | # STEP 4: build abundance at age |
- | tsq0 <- tsq1 <- tsq2 <- tsq3 <- 1 | + | # |
- | # independente | + | # objects |
- | set.seed(111) | + | Isim.ln <- array(NA, dim=c(n, |
- | Cor <- diag(c(1, | + | # sim |
- | m1 <- mvrnorm(n, c(mu1, mu2, mu3), Cor*tsq0) | + | for(i in 1:3){ |
- | # dependent | + | for(j in 1:3){ |
- | Cor[2,1] <- -0.75 | + | Isim.ln[, |
- | Cor[1,2] <- -0.75 | + | } |
- | m2 <- mvrnorm(n, c(mu1, mu2, mu3), Cor*tsq0) | + | } |
+ | # I characteristics | ||
+ | Isim.lnmean <- apply(log(Isim.ln), c(2,3,4), mean) | ||
+ | Isim.lnvar <- apply(log(Isim.ln), | ||
+ | Isim.lnhat <- exp(Isim.lnmean+Isim.lnvar/ | ||
+ | |||
+ | #============================================================================================== | ||
+ | # ESTIMATION | ||
+ | # | ||
+ | # number of samples to be drawn from each of the 250 replicate | ||
+ | ns <- 2 | ||
+ | |||
+ | # | ||
+ | # STEP 5: estimation with methodology proposed by the paper | ||
+ | # | ||
+ | |||
+ | # objects | ||
+ | Yres <- array(NA, dim=c(ns, | ||
+ | Ihat <- array(NA, dim=c(ns, | ||
+ | xd <- expand.grid(dimnames(Ihat)[-c(1, | ||
+ | # estimation | ||
+ | set.seed(222) | ||
+ | for(j in 1:ns){ | ||
+ | cat(" | ||
+ | samp <- sample(1:n, 100) | ||
+ | for(i in 1: | ||
+ | x <- xd[i,] | ||
+ | cat(" | ||
+ | # building the sample | ||
+ | Isamp <- Isim.ln[samp,, | ||
+ | locsamp | ||
+ | Ysamp <- apply(Isamp, | ||
+ | # estimation of age aggregated abundance | ||
+ | lf <- likfit(as.geodata(cbind(locsamp, | ||
+ | Yhat <- exp(lf$beta+lf$tausq/ | ||
+ | Yhatvar <- exp(2*lf$beta+lf$tausq+lf$sigmasq)*(exp(lf$tausq+lf$sigmasq)-1) | ||
+ | Yres[j,c(" | ||
+ | # estimation of abundance-at-age | ||
+ | prop <- acomp(Isamp) | ||
+ | Ihat[j,, | ||
+ | } | ||
+ | } | ||
+ | |||
+ | # | ||
+ | # STEP 6: estimation with design-based estimators | ||
+ | # | ||
- | m <- m1 | + | # objects |
+ | Ibar <- array(NA, dim=c(ns, | ||
+ | # estimation | ||
+ | set.seed(222) | ||
+ | for(j in 1:ns){ | ||
+ | cat(" | ||
+ | samp <- sample(1:n, 100) | ||
+ | for(i in 1: | ||
+ | x <- xd[i,] | ||
+ | cat(" | ||
+ | # building the sample | ||
+ | Isamp <- Isim.ln[samp,, | ||
+ | Ysamp <- apply(Isamp, | ||
+ | # store means | ||
+ | Yres[j, | ||
+ | # estimation of abundance-at-age | ||
+ | Ibar[j,, | ||
+ | } | ||
+ | } | ||
- | ## construindo Y = log(S)+e | + | #============================================================================================== |
- | Y <- Y1 <- Y2 <- Y3 <- S00 | + | # RESULTS |
- | Y1$data <- exp(m[,1] + S00$data + S11$data) | + | # |
- | Y2$data <- exp(m[,2] + S00$data + S22$data) | + | |
- | Y3$data <- exp(m[,3] + S00$data + S33$data) | + | |
- | # abundância total | + | #---------------------------------------------------------------------------------------------- |
- | Y$data <- Y1$data + Y2$data + Y3$data | + | # STEP 7: summary statistics |
+ | # | ||
- | ## use plots to check dependency | + | # objects |
- | Yfac <- apply(Y$data, 2, function(x) as.numeric(cut(x, c(-1,quantile(x, probs=c(0.2,0.4,0.6,0.8,1)))))) | + | sim.res |
- | df0 <- data.frame(expand.grid(list(loc=1:n, samp=1:100, age=1:3)), I=c(Y1$data, Y2$data, Y3$data), prop=c(Y1$data, Y2$data, Y3$data)/c(Y$data), Yfac=factor(Yfac)) | + | # computation |
+ | for(i in 1:nsim){ | ||
+ | lmusim | ||
+ | # geo | ||
+ | m <- Ihat[,, | ||
+ | q025 <- apply(m,c(2,3), quantile, probs=0.025, na.rm=T) | ||
+ | q975 <- apply(m,c(2,3), quantile, probs=0.975, na.rm=T) | ||
+ | sim.res[i,,," | ||
+ | for(j in 1:ns){ | ||
+ | m[j,,] <- m[j,,]-lmusim | ||
+ | } | ||
+ | sim.res[i,,," | ||
+ | sim.res[i,,," | ||
- | df0.mean <- tapply(df0$prop, list(age=df0$age, Yfac=df0$Yfac, samp=df0$samp), mean) | + | # samp |
- | df0.mean <- data.frame(expand.grid(dimnames(df0.mean)), prop=c(df0.mean)) | + | m <- Ibar[,, |
- | bwplot(prop~age|Yfac, | + | q025 <- apply(m, |
+ | q975 <- apply(m,c(2,3), quantile, probs=0.975, na.rm=T) | ||
+ | sim.res[i,,,"samp"," | ||
+ | for(j in 1:ns){ | ||
+ | m[j,,] <- m[j,, | ||
+ | } | ||
+ | sim.res[i,,," | ||
+ | sim.res[i,,," | ||
+ | } | ||
+ | # table | ||
+ | tab.res <- apply(sim.res, c(2,3,4,5), mean) | ||
+ | ################################################################################################# | ||
+ | # THE END (or the beginning ...) | ||
+ | ################################################################################################# | ||
</ | </ | ||
+ | ===== Results ===== | ||