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:40] – 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, | + | 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) | ||
+ | # | ||
+ | # | ||
+ | |||
+ | # Generate a spatial Gaussian process Z | ||
+ | phi <- 0.2 | ||
+ | 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 <- apply(log(Ysim$data), 2, mean) | ||
+ | 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: | ||
+ | Psim <- array(NA, dim=c(n, | ||
+ | # option 1: no association between compositions and age aggregated abundance | ||
+ | arr00[, | ||
+ | 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 | ||
+ | # | ||
- | for(i in 1:niter){ | + | # objects |
- | cat(i) | + | Isim.ln <- array(NA, dim=c(n, |
- | ggdd <- gd | + | # sim |
- | ggdd$data <- gd$data[,i] | + | for(i in 1:3){ |
- | lf <- likfit(ggdd, | + | for(j in 1:3){ |
- | arr[i,,1] <- c(lf$beta, lf$beta.var) | + | Isim.ln[,i,,j] <- Psim[,i,,j]*Ysim$data |
+ | } | ||
} | } | ||
+ | # 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,, | ||
+ | } | ||
+ | } | ||
- | ===== Algorítmo | + | # |
- | <code R> | + | # RESULTS |
- | ## Modelo: variáveis com mu dependente e correlação componente espacial parcialmente comum | + | #============================================================================================== |
- | ## Y1 = mu1 + S00 + S11 + e1 = mu1 + sig00 R + sig11 R11 + e1 | + | |
- | ## 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) | + | |
- | ## | + | |
- | ## | + | |
- | </ | + | |
- | **i indexa localizações | + | # |
- | j indexa idades | + | # STEP 7: summary statistics |
- | ** | + | # |
- | * Definir grid | + | # objects |
- | * Definir sig00 (usado para gerir associação espacial entre idades) | + | sim.res <- array(NA, dim=c(nsim,3,3,2,3), dimnames=list(iter=1: |
- | * Simular Ij gaussiana | + | # computation |
- | * set sig00 e phi00 = sig00/5 | + | for(i in 1:nsim){ |
- | * set sig## = 1-sig00 e phi## = sig##/5 | + | lmusim <- Isim.lnhat[,i,] |
- | * set mu## = 0.4 | + | # geo |
- | * sim S ~ MVN(0,Sig) Sig=f(sig, phi) | + | m <- Ihat[,,i,] |
- | * sim mu ~ MVN({mu1,mu2,m3}, Sigmu) Sigmu = {indep, dep} | + | q025 <- apply(m, |
- | * set Ij = muj + S00 + Sjj | + | q975 <- apply(m,c(2,3), quantile, probs=0.975, |
- | * Construir Y = prod(exp{Ij}) | + | sim.res[i,,," |
- | * Construir Pj = exp{Ij}/ | + | for(j in 1:ns){ |
- | * Construir Ij = Y*Pj para garantir que Y = sum_j(Ij) | + | m[j,,] <- m[j,, |
- | * Aleatorizar vector de localizações | + | } |
- | * Construir amostras de Y, P e I | + | sim.res[i,,," |
- | * Ajustar modelo | + | sim.res[i,,," |
- | * Estimar Y | + | |
- | * Yhat = exp{beta+beta.var/2} | + | |
- | * Ybar = sum_i(Yi)/n | + | |
- | * Estimar Ij | + | |
- | * Ihatj = Yhat * 1/n sum_i(Pij) sendo Pij = Iij/sum_j(Iij) | + | |
- | * Ibarj = sum_i(Iij)/n | + | |
- | <code R> | + | # samp |
+ | m <- Ibar[,, | ||
+ | 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 ===== | ||
- | |||
- | |||