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 | ||
artigos:ernesto3:sim [2008/09/26 08:11] – ernesto | artigos:ernesto3:sim [2008/10/13 13:00] (atual) – ernesto | ||
---|---|---|---|
Linha 1: | Linha 1: | ||
======Simulation study ====== | ======Simulation study ====== | ||
- | ===== Test beta bias ===== | + | ===== Algorithm |
<code R> | <code R> | ||
- | gs <- expand.grid((0:10)/10, (0:10)/10) | + | ################################################################################################# |
- | niter <- 100 | + | # |
- | arr <- array(NA, dim=c(niter, 2, 2), dimnames=list(niter=1:niter, stat=c(" | + | # PAPER COMPANION: SIMULATION STUDY |
+ | # | ||
+ | # Paulo Ribeiro Jr. < | ||
+ | # Date: 20081013 | ||
+ | # Objective: Test paper methodology against design-based estimators regarding | ||
+ | # association between age compositions and the age aggregated abundance. | ||
+ | # Tree options are simulated: | ||
+ | # (iii) strong association. | ||
+ | # Note: The association is induced by considering correlated multivariate gaussian | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | ################################################################################################# | ||
- | # gau | + | # R libraries |
+ | library(geoR) | ||
+ | library(lattice) | ||
+ | library(MASS) | ||
+ | library(RandomFields) | ||
+ | library(compositions) | ||
+ | |||
+ | # | ||
+ | # SIMULATING THE PROCESSES | ||
+ | # | ||
+ | # grid | ||
+ | gs <- expand.grid((0: | ||
+ | # size of processes | ||
+ | n <- nrow(gs) | ||
+ | # number of replicates | ||
+ | nsim <- 250 | ||
+ | |||
+ | # | ||
+ | # STEP 1 : gaussian processes to build abundance and compositions | ||
+ | # | ||
+ | |||
+ | # object | ||
+ | arr0 <- array(NA, dim=c(n, | ||
+ | # 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) | set.seed(111) | ||
- | gd <- grf(grid=gs, cov.pars=c(1,0.2), mean=2, nsim=niter) | + | for(i in 1:nsim){ |
+ | arr0[,, | ||
+ | } | ||
+ | |||
+ | # | ||
+ | # STEP 2: generate 250 replicates of a log-gaussian spatial process (Diggle and Ribeiro Jr, 2007) | ||
+ | # | ||
+ | # | ||
- | for(i in 1:niter){ | + | # Generate a spatial Gaussian process Z |
- | cat(i) | + | phi <- 0.2 |
- | ggdd <- gd | + | sigmasq <- 0.5 |
- | ggdd$data <- gd$data[,i] | + | set.seed(222) |
- | lf <- likfit(ggdd, ini.cov.pars=c(1,0.2), messages=FALSE) | + | Z <- grf(grid=gs, cov.pars=c(sigmasq, |
- | arr[i,,1] <- c(lf$beta, lf$beta.var) | + | # build a log gausian process Y |
+ | Ysim <- Z | ||
+ | Ysim$data <- exp(Z$data+arr0[,1,]) | ||
+ | # Y characteristics | ||
+ | Ysim.lmean | ||
+ | Ysim.lvar <- apply(log(Ysim$data), | ||
+ | Ysim.lnhat <- exp(Ysim.lmean+Ysim.lvar/ | ||
+ | |||
+ | # | ||
+ | # STEP 3: build compositions | ||
+ | # | ||
+ | |||
+ | # objects | ||
+ | arr00 <- array(1, dim=c(n,3,nsim), dimnames=list(loc=1:n, age=1:3, nsim=1:nsim)) | ||
+ | Psim <- array(NA, dim=c(n, | ||
+ | # option 1: no association between compositions and age aggregated abundance | ||
+ | arr00[,1:2,] <- exp(arr0[, | ||
+ | Psim[,,,1] <- aperm(apply(arr00, | ||
+ | # option 2: week association between compositions and age aggregated abundance | ||
+ | arr00[, | ||
+ | Psim[,,,2] <- aperm(apply(arr00, | ||
+ | # option 3: strong association between compositions and age aggregated abundance | ||
+ | arr00[, | ||
+ | Psim[,,,3] <- aperm(apply(arr00, | ||
+ | # P characteristics | ||
+ | |||
+ | # | ||
+ | # STEP 4: build abundance at age | ||
+ | # | ||
+ | |||
+ | # objects | ||
+ | Isim.ln <- array(NA, dim=c(n, | ||
+ | # sim | ||
+ | for(i in 1:3){ | ||
+ | for(j in 1:3){ | ||
+ | Isim.ln[, | ||
+ | } | ||
} | } | ||
+ | # I characteristics | ||
+ | Isim.lnmean <- apply(log(Isim.ln), | ||
+ | Isim.lnvar <- apply(log(Isim.ln), | ||
+ | Isim.lnhat <- exp(Isim.lnmean+Isim.lnvar/ | ||
- | # gau independente | + | #============================================================================================== |
- | set.seed(111) | + | # ESTIMATION |
- | gd <- grf(grid=gs, cov.pars=c(0,0), mean=2, nsim=niter, nugget=1) | + | #============================================================================================== |
+ | # number of samples to be drawn from each of the 250 replicate | ||
+ | ns <- 2 | ||
- | for(i in 1:niter){ | + | # |
- | cat(i) | + | # STEP 5: estimation with methodology proposed by the paper |
- | ggdd <- gd | + | # |
- | ggdd$data | + | |
- | lf <- likfit(ggdd, ini.cov.pars=c(0.5,0.5), messages=F) | + | # objects |
- | arr[i,, | + | 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:nrow(xd)){ | ||
+ | x <- xd[i,] | ||
+ | cat(" | ||
+ | # building the sample | ||
+ | Isamp | ||
+ | locsamp <- gs[samp,] | ||
+ | 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, | ||
+ | # estimation of abundance-at-age | ||
+ | prop <- acomp(Isamp) | ||
+ | Ihat[j,, | ||
+ | } | ||
} | } | ||
- | pdf(" | + | # |
- | par(mfrow=c(2, | + | # STEP 6: estimation with design-based estimators |
- | hist(arr[, | + | # |
- | hist(arr[, | + | |
- | hist(arr[, | + | |
- | hist(arr[, | + | |
- | dev.off() | + | |
- | </ | + | |
- | <note> | + | # objects |
- | O problema está na variância do beta que não é um estimador da variância do processo. Logo quando fazemos a backtrans há uma subestimação da média do processo. | + | Ibar <- array(NA, dim=c(ns, |
- | </note> | + | # 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 | ||
+ | Ysamp <- apply(Isamp, | ||
+ | # store means | ||
+ | Yres[j, | ||
+ | # estimation of abundance-at-age | ||
+ | Ibar[j,, | ||
+ | } | ||
+ | } | ||
+ | # | ||
+ | # RESULTS | ||
+ | # | ||
+ | # | ||
+ | # STEP 7: summary statistics | ||
+ | # | ||
+ | |||
+ | # objects | ||
+ | sim.res <- array(NA, dim=c(nsim, | ||
+ | # computation | ||
+ | for(i in 1:nsim){ | ||
+ | lmusim <- Isim.lnhat[, | ||
+ | # geo | ||
+ | m <- Ihat[,,i,] | ||
+ | q025 <- apply(m, | ||
+ | q975 <- apply(m, | ||
+ | sim.res[i,,," | ||
+ | for(j in 1:ns){ | ||
+ | m[j,,] <- m[j,, | ||
+ | } | ||
+ | sim.res[i,,," | ||
+ | sim.res[i,,," | ||
+ | |||
+ | # samp | ||
+ | m <- Ibar[,,i,] | ||
+ | q025 <- apply(m, | ||
+ | q975 <- apply(m, | ||
+ | sim.res[i,,," | ||
+ | for(j in 1:ns){ | ||
+ | m[j,,] <- m[j,, | ||
+ | } | ||
+ | sim.res[i,,," | ||
+ | sim.res[i,,," | ||
+ | } | ||
+ | # table | ||
+ | tab.res <- apply(sim.res, | ||
+ | |||
+ | ################################################################################################# | ||
+ | # THE END (or the beginning ...) | ||
+ | ################################################################################################# | ||
+ | </ | ||
+ | ===== Results ===== | ||