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:46] – 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 |
| - | set.seed(111) | + | |
| - | gd <- grf(grid=gs, | + | |
| - | + | ||
| - | for(i in 1:niter){ | + | |
| - | cat(i) | + | |
| - | ggdd <- gd | + | |
| - | ggdd$data <- gd$data[, | + | |
| - | lf <- likfit(ggdd, | + | |
| - | arr[i,,1] <- c(lf$beta, lf$beta.var) | + | |
| - | } | + | |
| - | + | ||
| - | # gau independente | + | |
| - | set.seed(111) | + | |
| - | gd <- grf(grid=gs, | + | |
| - | + | ||
| - | for(i in 1:niter){ | + | |
| - | cat(i) | + | |
| - | ggdd <- gd | + | |
| - | ggdd$data <- gd$data[, | + | |
| - | lf <- likfit(ggdd, | + | |
| - | arr[i,,2] <- c(lf$beta, lf$beta.var) | + | |
| - | } | + | |
| - | + | ||
| - | pdf(" | + | |
| - | par(mfrow=c(2, | + | |
| - | hist(arr[, | + | |
| - | hist(arr[, | + | |
| - | hist(arr[, | + | |
| - | hist(arr[, | + | |
| - | dev.off() | + | |
| - | </ | + | |
| - | + | ||
| - | < | + | |
| - | 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. | + | |
| - | </ | + | |
| - | + | ||
| - | ===== Algorítmo ===== | + | |
| - | < | + | |
| - | ## 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) | + | |
| - | ## mu1 = mu2 = mu3 = 1 | + | |
| - | ## | + | |
| - | </ | + | |
| - | + | ||
| - | **i indexa localizações | + | |
| - | j indexa idades | + | |
| - | ** | + | |
| - | + | ||
| - | * Definir grid | + | |
| - | * Definir sig00 (usado para gerir associação espacial entre idades) | + | |
| - | * Simular Ij gaussiana | + | |
| - | * set sig00 e phi00 = sig00/5 | + | |
| - | * set sig## = 1-sig00 e phi## = sig##/5 | + | |
| - | * set mu## = 0.4 | + | |
| - | * sim S ~ MVN(0,Sig) Sig=f(sig, phi) | + | |
| - | * sim mu ~ MVN({mu1, | + | |
| - | * set Ij = muj + S00 + Sjj | + | |
| - | * Construir Y = prod(exp{Ij}) | + | |
| - | * Construir Pj = exp{Ij}/ | + | |
| - | * Construir Ij = Y*Pj para garantir que Y = sum_j(Ij) | + | |
| - | * Aleatorizar vector de localizações (100) | + | |
| - | * Construir amostras de Y, P e I | + | |
| - | * Ajustar modelo geo | + | |
| - | * Estimar Y | + | |
| - | * Yhat = exp{beta+beta.var/ | + | |
| - | * Ybar = sum_i(Yi)/ | + | |
| - | * Estimar Ij | + | |
| - | * Ihatj = Yhat * 1/n sum_i(Pij) sendo Pij = Iij/ | + | |
| - | * Ibarj = sum_i(Iij)/ | + | |
| - | + | ||
| - | <code R> | + | |
| - | ## INI | + | |
| - | # libraries | + | |
| library(geoR) | library(geoR) | ||
| library(lattice) | library(lattice) | ||
| library(MASS) | library(MASS) | ||
| library(RandomFields) | library(RandomFields) | ||
| - | source(" | + | library(compositions) |
| - | # definindo | + | #============================================================================================== |
| + | # SIMULATING THE PROCESSES | ||
| + | # | ||
| + | # grid | ||
| gs <- expand.grid((0: | gs <- expand.grid((0: | ||
| + | # size of processes | ||
| n <- nrow(gs) | n <- nrow(gs) | ||
| - | niter <- 100 | + | # number of replicates |
| - | spcor <- seq(0, | + | nsim <- 250 |
| - | # objectos | + | #---------------------------------------------------------------------------------------------- |
| - | Isim <- array(NA, dim=c(n, | + | # STEP 1 : gaussian processes to build abundance and compositions |
| - | Isim.ln <- array(NA, dim=c(n, | + | # |
| - | Psim <- array(NA, dim=c(n, | + | |
| - | Ysim <- array(NA, dim=c(n, | + | |
| - | Yres <- array(NA, dim=c(2, | + | |
| - | Isim.lnhat <- array(NA, dim=c(3, | + | |
| - | Ibar <- array(NA, dim=c(3, | + | |
| - | Ihat <- array(NA, dim=c(3, | + | |
| - | ## SIM (função isim abaixo) | + | # object |
| - | for(i in 1:length(spcor)){ | + | arr0 <- array(NA, dim=c(n, |
| - | Isim[,,,i,] <- isim(gs, spcor[i], niter, 0.2) | + | # 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), c(1,0,0,0,0,0,0), Sig) | ||
| } | } | ||
| + | |||
| + | # | ||
| + | # STEP 2: generate 250 replicates of a log-gaussian spatial process (Diggle and Ribeiro Jr, 2007) | ||
| + | # | ||
| + | # | ||
| - | Isim.mean <- apply(Isim, c(2,3,4,5), mean) | + | # Generate a spatial Gaussian process Z |
| - | Isim.var <- apply(Isim, c(2,3,4,5), var) | + | phi <- 0.2 |
| + | sigmasq | ||
| + | set.seed(222) | ||
| + | Z <- grf(grid=gs, cov.pars=c(sigmasq, phi), nsim=nsim) | ||
| + | # build a log gausian process Y | ||
| + | Ysim <- Z | ||
| + | Ysim$data <- exp(Z$data+arr0[,1,]) | ||
| + | # Y characteristics | ||
| + | Ysim.lmean <- apply(log(Ysim$data), 2, mean) | ||
| + | Ysim.lvar <- apply(log(Ysim$data), 2, var) | ||
| + | Ysim.lnhat <- exp(Ysim.lmean+Ysim.lvar/ | ||
| - | # Ysim como produto de lognormais | + | #---------------------------------------------------------------------------------------------- |
| - | Ysim[,1,,,] <- apply(exp(Isim), | + | # STEP 3: build compositions |
| - | # cractarerísticas de Y | + | #---------------------------------------------------------------------------------------------- |
| - | Ysim.smean <- apply(Ysim, c(3,4,5), mean) | + | |
| - | Ysim.svar <- apply(Ysim, c(3,4,5), var) | + | |
| - | Ysim.lmean <- apply(log(Ysim), | + | |
| - | Ysim.lvar <- apply(log(Ysim), | + | |
| - | # composições | + | |
| - | Psim[] <- aperm(apply(exp(Isim), | + | |
| - | # observações das marginais | + | |
| - | Isim.ln[, | + | |
| - | Isim.ln[, | + | |
| - | Isim.ln[, | + | |
| - | Isim.lnmean <- apply(log(Isim.ln), | + | |
| - | Isim.lnvar <- apply(log(Isim.ln), | + | |
| - | Isim.lnhat <- exp(Isim.lnmean+Isim.lnvar/ | + | |
| - | ## running our model | + | # objects |
| - | xd <- expand.grid(dimnames(Ihat)[-1]) | + | arr00 <- array(1, dim=c(n, |
| - | samp <- sample(1:n, 100) | + | 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[,1:2,] <- exp(arr0[, | ||
| + | Psim[,,,3] <- aperm(apply(arr00, | ||
| + | # P characteristics | ||
| - | for(i in 1: | + | # |
| - | x <- xd[i,] | + | # STEP 4: build abundance at age |
| - | cat(as.character(unlist(x))," | + | # |
| - | Isamp <- Isim.ln[samp,, | + | |
| - | locsamp <- gs[samp,] | + | # objects |
| - | Ysamp <- Ysim[samp,, | + | Isim.ln <- array(NA, dim=c(n,3,nsim,3), dimnames=list(loc=1:n, age=1:3, nsim=1:nsim, mucor=c(" |
| - | # geodata | + | # sim |
| - | gd <- as.geodata(cbind(locsamp, | + | for(i in 1:3){ |
| - | lf <- likfit(gd, lambda=0, ini.cov.pars=c(1,0.2), messages=FALSE) | + | for(j in 1:3){ |
| - | Yhat <- exp(lf$beta+lf$beta.var/ | + | Isim.ln[,i,,j] <- Psim[,i,,j]*Ysim$data |
| - | # store means | + | } |
| - | Yres[,x[[1]],x[[2]],x[[3]]] <- c(mean(Ysamp), Yhat) | + | |
| - | # compositions | + | |
| - | prop <- apply(Isamp,1, | + | |
| - | prop[is.na(prop)]<-0 | + | |
| - | Ihat[,x[[1]],x[[2]],x[[3]]] <- Yhat*apply(prop, | + | |
| - | Ibar[,x[[1]],x[[2]],x[[3]]] <- apply(Isamp, | + | |
| } | } | ||
| - | </code> | + | # I characteristics |
| + | Isim.lnmean | ||
| + | Isim.lnvar <- apply(log(Isim.ln), | ||
| + | Isim.lnhat <- exp(Isim.lnmean+Isim.lnvar/2) | ||
| - | <code R> | + | # |
| - | isim <- function(gs, | + | # ESTIMATION |
| - | set.seed(seed) | + | #============================================================================================== |
| - | arr <- array(NA, dim=c(n=nrow(gs), | + | # number of samples to be drawn from each of the 250 replicate |
| - | m.arr <- array(NA, dim=c(n=nrow(gs), | + | ns <- 2 |
| - | # parâmetros do componente comum (S00) | + | #---------------------------------------------------------------------------------------------- |
| - | phi00 <- sig00/5 | + | # STEP 5: estimation with methodology proposed by the paper |
| + | # | ||
| - | # parâmetros dos componentes individuais | + | # objects |
| - | sig11 <- sig22 <- sig33 <- 1-sig00 | + | Yres <- array(NA, dim=c(ns, |
| - | phi11 <- phi22 <- phi33 <- (1-sig00)/5 | + | Ihat <- array(NA, dim=c(ns, |
| - | mu1 <- mu2 <- mu3 <- 0.4 | + | xd <- expand.grid(dimnames(Ihat)[-c(1,2)]) |
| + | # 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 | ||
| + | locsamp <- gs[samp,] | ||
| + | Ysamp <- apply(Isamp,1,sum) | ||
| + | # estimation of age aggregated abundance | ||
| + | lf <- likfit(as.geodata(cbind(locsamp, | ||
| + | Yhat <- exp(lf$beta+lf$tausq/2+lf$sigmasq/ | ||
| + | Yhatvar | ||
| + | Yres[j, | ||
| + | # estimation of abundance-at-age | ||
| + | prop | ||
| + | Ihat[j,, | ||
| + | } | ||
| + | } | ||
| - | ## simulando S | + | #---------------------------------------------------------------------------------------------- |
| - | S00 <- grf(grid=gs, | + | # STEP 6: estimation with design-based estimators |
| - | S11 <- grf(grid=gs, | + | # |
| - | S22 <- grf(grid=gs, | + | |
| - | S33 <- grf(grid=gs, | + | |
| - | ## simulando mu | + | # objects |
| - | # independent | + | Ibar <- array(NA, dim=c(ns, |
| - | set.seed(111) | + | # estimation |
| - | Cor <- diag(c(1,1,1)) | + | set.seed(222) |
| - | for(i in 1:niter){ | + | for(j in 1:ns){ |
| - | m.arr[,,i] <- mvrnorm(n, c(mu1, mu2, mu3), Cor*tsq00) | + | cat(" |
| + | samp <- sample(1:n, 100) | ||
| + | for(i in 1:nrow(xd)){ | ||
| + | x <- xd[i,] | ||
| + | cat(" | ||
| + | # building the sample | ||
| + | Isamp <- Isim.ln[samp,,x[[1]],x[[2]]] | ||
| + | Ysamp | ||
| + | # store means | ||
| + | Yres[j,c(" | ||
| + | # estimation of abundance-at-age | ||
| + | Ibar[j,, | ||
| } | } | ||
| - | ## construindo Y = S+e | ||
| - | arr[,1,,1] <- m.arr[,1,] + S00$data + S11$data | ||
| - | arr[,2,,1] <- m.arr[,2,] + S00$data + S22$data | ||
| - | arr[,3,,1] <- m.arr[,3,] + S00$data + S33$data | ||
| - | |||
| - | # dependent | ||
| - | set.seed(111) | ||
| - | Cor[2,1] <- -0.9 | ||
| - | Cor[1,2] <- -0.9 | ||
| - | for(i in 1:niter){ | ||
| - | m.arr[,, | ||
| - | } | ||
| - | ## construindo Y = S+e | ||
| - | arr[,1,,2] <- m.arr[,1,] + S00$data + S11$data | ||
| - | arr[,2,,2] <- m.arr[,2,] + S00$data + S22$data | ||
| - | arr[,3,,2] <- m.arr[,3,] + S00$data + S33$data | ||
| - | |||
| - | # out | ||
| - | arr | ||
| } | } | ||
| - | </ | ||
| - | ===== Resultados | + | # |
| + | # RESULTS | ||
| + | # | ||
| - | <code R> | + | #---------------------------------------------------------------------------------------------- |
| - | Ihat.bias <- apply(Ihat | + | # STEP 7: summary statistics |
| - | Ihat.mse <- apply(Ihat | + | # |
| - | Ibar.bias <- apply(Ibar | + | |
| - | Ibar.mse <- apply(Ibar | + | |
| - | > Ihat.bias | + | # objects |
| - | , , mucor = indep | + | sim.res <- array(NA, dim=c(nsim, |
| + | # computation | ||
| + | for(i in 1:nsim){ | ||
| + | lmusim <- Isim.lnhat[, | ||
| + | # geo | ||
| + | m <- Ihat[,, | ||
| + | q025 <- apply(m, | ||
| + | q975 <- apply(m, | ||
| + | sim.res[i,,," | ||
| + | for(j in 1:ns){ | ||
| + | m[j,,] <- m[j,, | ||
| + | } | ||
| + | sim.res[i,,," | ||
| + | sim.res[i,,," | ||
| - | spcor | + | # samp |
| - | age 0 | + | m <- Ibar[,,i,] |
| - | | + | q025 <- apply(m,c(2,3), quantile, probs=0.025, na.rm=T) |
| - | | + | q975 <- apply(m, |
| - | | + | sim.res[i,,," |
| - | + | for(j in 1:ns){ | |
| - | , , mucor = dep | + | m[j,,] <- m[j,,]-lmusim |
| - | + | } | |
| - | | + | sim.res[i,,," |
| - | age 0 0.25 0.5 0.75 1 | + | sim.res[i,,," |
| - | | + | } |
| - | 2 -0.1859164 | + | # table |
| - | | + | tab.res <- apply(sim.res, c(2,3,4,5), mean) |
| - | + | ||
| - | > Ibar.bias | + | |
| - | , , mucor = indep | + | |
| - | + | ||
| - | | + | |
| - | age 0 | + | |
| - | 1 1.347212 | + | |
| - | 2 1.460882 | + | |
| - | | + | |
| - | + | ||
| - | , , mucor = dep | + | |
| - | + | ||
| - | spcor | + | |
| - | age | + | |
| - | 1 0.8780813 | + | |
| - | 2 0.8637434 2.198764 3.656170 | + | |
| - | 3 1.2216741 2.651468 4.023795 | + | |
| + | ################################################################################################# | ||
| + | # THE END (or the beginning ...) | ||
| + | ################################################################################################# | ||
| </ | </ | ||
| + | ===== Results ===== | ||