diff --git a/inst/doc/lhreg.R b/inst/doc/lhreg.R index 160b1ee..832babf 100644 --- a/inst/doc/lhreg.R +++ b/inst/doc/lhreg.R @@ -1,13 +1,13 @@ -## ----eval=FALSE,results='hide'------------------------------------------- +## ----update-stuff,eval=FALSE,results='hide',echo=FALSE------------------- # devtools::install_github("borealbirds/lhreg") # devtools::build_vignettes("~/repos/lhreg") -## ------------------------------------------------------------------------ +## ----trait-data---------------------------------------------------------- library(lhreg) data(lhreg_data) str(lhreg_data) -## ----eval=FALSE---------------------------------------------------------- +## ----phylo-corr,eval=FALSE----------------------------------------------- # library(ape) # mph <- read.nexus("11960.tre") # 1000 trees with Ericson backbone # cph <- consensus(mph) @@ -28,12 +28,12 @@ str(lhreg_data) # vvv <- vvv[spp,spp] # cor_matrix <- as.matrix(nearPD(vvv, corr=TRUE)$mat) -## ------------------------------------------------------------------------ +## ----heatmap------------------------------------------------------------- data(cor_matrix) str(cor_matrix) heatmap(cor_matrix) -## ------------------------------------------------------------------------ +## ----screening-sr-------------------------------------------------------- y <- lhreg_data$logphi m4 <- lm(y ~ logmass + Mig2 + Nesthm + xMaxFreqkHz + Hab4, lhreg_data) m3 <- lm(y ~ logmass + Mig2 + Nesthm + xMaxFreqkHz + Hab3, lhreg_data) @@ -53,7 +53,7 @@ mpx <- lm(y ~ logmass + Mig2 + Nesthm + xMaxFreqkHz + Hab2 + mpx2 <- step(mpx) summary(mpx2) -## ------------------------------------------------------------------------ +## ----screening-dd-------------------------------------------------------- y <- lhreg_data$logtau m4 <- lm(y ~ logmass + Mig2 + Nesthm + xMaxFreqkHz + Hab4, lhreg_data) m3 <- lm(y ~ logmass + Mig2 + Nesthm + xMaxFreqkHz + Hab3, lhreg_data) @@ -73,7 +73,7 @@ mtx <- lm(y ~ logmass + Mig2 + Nesthm + xMaxFreqkHz + Hab2 + mtx2 <- step(mtx) summary(mtx2) -## ----eval=FALSE---------------------------------------------------------- +## ----models,eval=FALSE--------------------------------------------------- # met <- "DE" # can use "SANN", "Nelder-Mead" is quickest # x <- lhreg_data # vc <- cor_matrix @@ -129,7 +129,7 @@ summary(mtx2) # cbind(aicp, t(sapply(list(pM0, pMp0, pMl0, pMx, pMpx, pMlx), # function(z) z$summary[c("sigma","lambda"), 1]))) -## ----eval=FALSE---------------------------------------------------------- +## ----profile-lik,eval=FALSE---------------------------------------------- # library(parallel) # # lam <- seq(0, 2, by=0.01) @@ -160,7 +160,7 @@ summary(mtx2) # # stopCluster(cl) -## ----eval=FALSE---------------------------------------------------------- +## ----loo,eval=FALSE------------------------------------------------------ # ## makes sense to use lm for EDR LOO # tM0$coef # c(coef(mt0), log(summary(mt0)$sigma)) @@ -208,6 +208,175 @@ summary(mtx2) # MSEt <- SSEt / n # MSEp <- SSEp / n -## ------------------------------------------------------------------------ -#load(system.file("extdata", "lhreg-results-DE.Rdata", package = "lhreg")) +## ----load-results-------------------------------------------------------- +load(system.file("extdata", "lhreg-results-DE.rda", package = "lhreg")) + +Crit <- -0.5*qchisq(0.95, 1) +ltmp <- seq(0, 1, by=0.0001) +## red-yl-blue +Col <- c("#2C7BB6", "#6BAACF", "#ABD9E9", "#D4ECD3", "#FFFFBF", "#FED690", + "#FDAE61", "#EA633E", "#D7191C") +Col1 <- Col[1] +Col2 <- rgb(171/255, 217/255, 233/255, 0.5) # Col[3] +Col3 <- Col[9] +Col4 <- rgb(253/255, 174/255, 97/255, 0.5) # Col[7] +prt <- exp(pr_tMx) +prp <- exp(pr_pMlx) + +## ----table-1------------------------------------------------------------- +ff <- function(z) { + zz <- z$summary + + pcut <- function(p) { + factor(c("***", "**", "*", "+", "ns")[as.integer(cut(p, + c(1, 0.1, 0.05, 0.01, 0.001, 0), include.lowest=TRUE, right=FALSE))], + levels=c("***", "**", "*", "+", "ns")) + } + structure(sapply(1:nrow(zz), function(i) + paste0(round(zz[i,1], 3), " (SE +/- ", round(zz[i,2], 3), pcut(zz[i,4]), ")")), + names=rownames(zz)) +} +zzz <- lapply(list(pM0, pMp0, pMl0, pMx, pMpx, pMlx), ff) +m <- matrix("", length(zzz[[6]]), 6) +rownames(m) <- names(zzz[[6]]) +colnames(m) <- c("M0", "Mp0", "Ml0", "Mx", "Mpx", "Mlx") +for (i in 1:6) { + j <- match(names(zzz[[i]]), rownames(m)) + m[j,i] <- zzz[[i]] +} +ii <- grep("(SE +/- NANA)", m,fixed=TRUE) +m[ii] <- gsub("(SE +/- NANA)", "(fixed)", m[ii], fixed=TRUE) +m[m==""] <- "n/a" +m1 <- m + +zzz <- lapply(list(tM0, tMp0, tMl0, tMx, tMpx, tMlx), ff) +m <- matrix("", length(zzz[[6]]), 6) +rownames(m) <- names(zzz[[6]]) +colnames(m) <- c("M0", "Mp0", "Ml0", "Mx", "Mpx", "Mlx") +for (i in 1:6) { + j <- match(names(zzz[[i]]), rownames(m)) + m[j,i] <- zzz[[i]] +} +ii <- grep("(SE +/- NANA)", m,fixed=TRUE) +m[ii] <- gsub("(SE +/- NANA)", "(fixed)", m[ii], fixed=TRUE) +m[m==""] <- "n/a" +m2 <- m + +m1 <- rbind(m1, df=aicp$df, dAIC=round(aicp$dAIC, 3), + XV_MSE=round(MSEp, 3)) +m2 <- rbind(m2, df=aict$df, dAIC=round(aict$dAIC, 3), + XV_MSE=round(MSEt, 3)) + +print.default(m1, quote=FALSE) # SR results +print.default(m2, quote=FALSE) # DD results + +## ----fig-1,width=14,height=6--------------------------------------------- +#pdf("Fig1.pdf", width=14, height=6) +op <- par(mfrow=c(1,2)) + +Res1 <- pl_pMl0 +Res2 <- pl_pMlx +yv <- exp(Res1-max(Res1)) +plot(lam, yv, type="n", lwd=1, ylim=exp(c(2*Crit, 0)), + xlab=expression(lambda), ylab="Profile Likelihood Ratio") +tmp1 <- splinefun(lam, yv)(ltmp) +i1 <- tmp1 > exp(Crit) +yv <- exp(Res2-max(Res2)) +tmp2 <- splinefun(lam, yv)(ltmp) +i2 <- tmp2 > exp(Crit) +polygon(c(ltmp[i1], rev(ltmp[i1])), + c(tmp1[i1], rep(-1, sum(i1))), + col=Col2, border=NA) +polygon(c(ltmp[i2], rev(ltmp[i2])), + c(tmp2[i2], rep(-1, sum(i2))), + col=Col4, border=NA) +abline(h=exp(Crit), col="grey") +lines(ltmp, tmp1, col=Col1, lwd=1) +lines(ltmp[i1], tmp1[i1], col=Col1, lwd=3) +lines(rep(ltmp[which.max(tmp1)], 2), c(1, -1), col=Col1) +lines(ltmp, tmp2, col=Col3, lwd=1) +lines(ltmp[i2], tmp2[i2], col=Col3, lwd=3) +lines(rep(ltmp[which.max(tmp2)], 2), c(1, -1), col=Col3) +box() +legend(0.1, 1, bty="n", border=c(Col1, Col3), fill=c(Col2, Col4), pt.cex=2, pt.lwd=2, + legend=c(expression(M[lambda*0]-SR), expression(M[lambda*beta]-SR))) +round(c(Max_Ml0=ltmp[which.max(tmp1)], CI=range(ltmp[i1])), 3) +round(c(Max_Mlx=ltmp[which.max(tmp2)], CI=range(ltmp[i2])), 3) + +Res1 <- pl_tMl0 +Res2 <- pl_tMlx +yv <- exp(Res1-max(Res1)) +plot(lam, yv, type="n", lwd=1, ylim=exp(c(2*Crit, 0)), + xlab=expression(lambda), ylab="Profile Likelihood Ratio") +tmp1 <- splinefun(lam, yv)(ltmp) +i1 <- tmp1 > exp(Crit) +yv <- exp(Res2-max(Res2)) +tmp2 <- splinefun(lam, yv)(ltmp) +i2 <- tmp2 > exp(Crit) +polygon(c(ltmp[i1], rev(ltmp[i1])), + c(tmp1[i1], rep(-1, sum(i1))), + col=Col2, border=NA) +polygon(c(ltmp[i2], rev(ltmp[i2])), + c(tmp2[i2], rep(-1, sum(i2))), + col=Col4, border=NA) +abline(h=exp(Crit), col="grey") +lines(ltmp, tmp1, col=Col1, lwd=1) +lines(ltmp[i1], tmp1[i1], col=Col1, lwd=3) +lines(rep(ltmp[which.max(tmp1)], 2), c(1, -1), col=Col1) +lines(ltmp, tmp2, col=Col3, lwd=1) +lines(ltmp[i2], tmp2[i2], col=Col3, lwd=3) +lines(rep(ltmp[which.max(tmp2)], 2), c(1, -1), col=Col3) +box() +legend(0.1, 1, bty="n", border=c(Col1, Col3), fill=c(Col2, Col4), pt.cex=2, pt.lwd=2, + legend=c(expression(M[lambda*0]-DD), expression(M[lambda*beta]-DD))) +round(c(Max_Ml0=ltmp[which.max(tmp1)], CI=range(ltmp[i1])), 3) +round(c(Max_Mlx=ltmp[which.max(tmp2)], CI=range(ltmp[i2])), 3) + +par(op) + +#dev.off() + +## ----fig-2,width=14,height=7--------------------------------------------- +#pdf("Fig2.pdf", width=14, height=7) + +op <- par(mfrow=c(1,2)) + +Max <- 0.7 +plot(prp, xaxs = "i", yaxs = "i", type="n", + ylim=c(0, Max), xlim=c(0, Max), + xlab="Time-removal SR Estimate", ylab="LOO SR Estimate") +abline(0,1,lty=1, col=1) +abline(0,2,lty=2, col="grey") +abline(0,1/2,lty=2, col="grey") +abline(0,1.5,lty=2, col="grey") +abline(0,1/1.5,lty=2, col="grey") +points(prp, xaxs = "i", yaxs = "i", + col=c(Col1,Col3)[as.integer(x$Mig2)], + cex=0.2+2*x$MaxFreqkHz/10) +box() +legend("topleft", pch=21, col=c(Col1,Col3,1,1), + pt.cex=c(1.5,1.5,2,1), bty="n", + legend=c("Migrant", "Resident", "Song Picth: High", "Song Pitch: Low")) +text(0.9*Max, 0.05*Max, expression(M[lambda*beta]-SR)) + +Max <- 2.1 +plot(prt, xaxs = "i", yaxs = "i", type="n", + ylim=c(0, Max), xlim=c(0, Max), + xlab="Distance-sampling DD Estimate", ylab="LOO DD Estimate") +abline(0,1,lty=1, col=1) +abline(0,2,lty=2, col="grey") +abline(0,1/2,lty=2, col="grey") +abline(0,1.5,lty=2, col="grey") +abline(0,1/1.5,lty=2, col="grey") +points(prt, xaxs = "i", yaxs = "i", + cex=0.2+2*x$logmass/5, + col=c(Col1,Col3)[as.integer(x$Hab2)]) +box() +legend("topleft", pch=21, col=c(Col1,Col3,1,1), pt.cex=c(1.5,1.5,2,1), bty="n", + legend=c("Habitat: Closed", "Habitat: Open", "Body Mass: Large", "Body Mass: Small")) +text(0.9*Max, 0.05*Max, expression(M[0*beta]-DD)) + +par(op) + +#dev.off() diff --git a/inst/doc/lhreg.Rmd b/inst/doc/lhreg.Rmd index 6e88e52..4440d22 100644 --- a/inst/doc/lhreg.Rmd +++ b/inst/doc/lhreg.Rmd @@ -8,7 +8,7 @@ vignette: > output: knitr:::html_vignette --- -```{r eval=FALSE,results='hide'} +```{r update-stuff,eval=FALSE,results='hide',echo=FALSE} devtools::install_github("borealbirds/lhreg") devtools::build_vignettes("~/repos/lhreg") ``` @@ -23,7 +23,7 @@ Explain what this Vignette is about, as a Supporting Info. Explain sources and transformations, maybe show some exploratory analyses. -```{r} +```{r trait-data} library(lhreg) data(lhreg_data) str(lhreg_data) @@ -33,7 +33,7 @@ str(lhreg_data) Explain source, how it was derived: -```{r eval=FALSE} +```{r phylo-corr,eval=FALSE} library(ape) mph <- read.nexus("11960.tre") # 1000 trees with Ericson backbone cph <- consensus(mph) @@ -57,7 +57,7 @@ cor_matrix <- as.matrix(nearPD(vvv, corr=TRUE)$mat) Visualize the phylo matrix: -```{r} +```{r heatmap} data(cor_matrix) str(cor_matrix) heatmap(cor_matrix) @@ -67,7 +67,7 @@ heatmap(cor_matrix) ### Variable screening for SR -```{r} +```{r screening-sr} y <- lhreg_data$logphi m4 <- lm(y ~ logmass + Mig2 + Nesthm + xMaxFreqkHz + Hab4, lhreg_data) m3 <- lm(y ~ logmass + Mig2 + Nesthm + xMaxFreqkHz + Hab3, lhreg_data) @@ -90,7 +90,7 @@ summary(mpx2) ### Variable screening for DD -```{r} +```{r screening-dd} y <- lhreg_data$logtau m4 <- lm(y ~ logmass + Mig2 + Nesthm + xMaxFreqkHz + Hab4, lhreg_data) m3 <- lm(y ~ logmass + Mig2 + Nesthm + xMaxFreqkHz + Hab3, lhreg_data) @@ -120,7 +120,7 @@ summary(mtx2) 5. Mpx, saturated, phylo max (~., lam=1) 6. Mlx, saturated, phylo est (~., lam=NA) -```{r eval=FALSE} +```{r models,eval=FALSE} met <- "DE" # can use "SANN", "Nelder-Mead" is quickest x <- lhreg_data vc <- cor_matrix @@ -180,7 +180,7 @@ cbind(aicp, t(sapply(list(pM0, pMp0, pMl0, pMx, pMpx, pMlx), ### Profile likelihood for lambda -```{r eval=FALSE} +```{r profile-lik,eval=FALSE} library(parallel) lam <- seq(0, 2, by=0.01) @@ -214,7 +214,7 @@ stopCluster(cl) ### LOO analysis -```{r eval=FALSE} +```{r loo,eval=FALSE} ## makes sense to use lm for EDR LOO tM0$coef c(coef(mt0), log(summary(mt0)$sigma)) @@ -263,8 +263,194 @@ MSEt <- SSEt / n MSEp <- SSEp / n ``` +Use `save.image()` to save the results. + ## Results -```{r} -#load(system.file("extdata", "lhreg-results-DE.Rdata", package = "lhreg")) +Load results and set some values: + +```{r load-results} +load(system.file("extdata", "lhreg-results-DE.rda", package = "lhreg")) + +Crit <- -0.5*qchisq(0.95, 1) +ltmp <- seq(0, 1, by=0.0001) +## red-yl-blue +Col <- c("#2C7BB6", "#6BAACF", "#ABD9E9", "#D4ECD3", "#FFFFBF", "#FED690", + "#FDAE61", "#EA633E", "#D7191C") +Col1 <- Col[1] +Col2 <- rgb(171/255, 217/255, 233/255, 0.5) # Col[3] +Col3 <- Col[9] +Col4 <- rgb(253/255, 174/255, 97/255, 0.5) # Col[7] +prt <- exp(pr_tMx) +prp <- exp(pr_pMlx) ``` + +### Table with estimates and MSE + +```{r table-1} +ff <- function(z) { + zz <- z$summary + + pcut <- function(p) { + factor(c("***", "**", "*", "+", "ns")[as.integer(cut(p, + c(1, 0.1, 0.05, 0.01, 0.001, 0), include.lowest=TRUE, right=FALSE))], + levels=c("***", "**", "*", "+", "ns")) + } + structure(sapply(1:nrow(zz), function(i) + paste0(round(zz[i,1], 3), " (SE +/- ", round(zz[i,2], 3), pcut(zz[i,4]), ")")), + names=rownames(zz)) +} +zzz <- lapply(list(pM0, pMp0, pMl0, pMx, pMpx, pMlx), ff) +m <- matrix("", length(zzz[[6]]), 6) +rownames(m) <- names(zzz[[6]]) +colnames(m) <- c("M0", "Mp0", "Ml0", "Mx", "Mpx", "Mlx") +for (i in 1:6) { + j <- match(names(zzz[[i]]), rownames(m)) + m[j,i] <- zzz[[i]] +} +ii <- grep("(SE +/- NANA)", m,fixed=TRUE) +m[ii] <- gsub("(SE +/- NANA)", "(fixed)", m[ii], fixed=TRUE) +m[m==""] <- "n/a" +m1 <- m + +zzz <- lapply(list(tM0, tMp0, tMl0, tMx, tMpx, tMlx), ff) +m <- matrix("", length(zzz[[6]]), 6) +rownames(m) <- names(zzz[[6]]) +colnames(m) <- c("M0", "Mp0", "Ml0", "Mx", "Mpx", "Mlx") +for (i in 1:6) { + j <- match(names(zzz[[i]]), rownames(m)) + m[j,i] <- zzz[[i]] +} +ii <- grep("(SE +/- NANA)", m,fixed=TRUE) +m[ii] <- gsub("(SE +/- NANA)", "(fixed)", m[ii], fixed=TRUE) +m[m==""] <- "n/a" +m2 <- m + +m1 <- rbind(m1, df=aicp$df, dAIC=round(aicp$dAIC, 3), + XV_MSE=round(MSEp, 3)) +m2 <- rbind(m2, df=aict$df, dAIC=round(aict$dAIC, 3), + XV_MSE=round(MSEt, 3)) + +print.default(m1, quote=FALSE) # SR results +print.default(m2, quote=FALSE) # DD results +``` + +Note: estimates and p-values for lambda taken from profile likelihood results +due to issues with boundary condition when traits used for DD model. + +### Profile likelihood figures + +```{r fig-1,width=14,height=6} +#pdf("Fig1.pdf", width=14, height=6) +op <- par(mfrow=c(1,2)) + +Res1 <- pl_pMl0 +Res2 <- pl_pMlx +yv <- exp(Res1-max(Res1)) +plot(lam, yv, type="n", lwd=1, ylim=exp(c(2*Crit, 0)), + xlab=expression(lambda), ylab="Profile Likelihood Ratio") +tmp1 <- splinefun(lam, yv)(ltmp) +i1 <- tmp1 > exp(Crit) +yv <- exp(Res2-max(Res2)) +tmp2 <- splinefun(lam, yv)(ltmp) +i2 <- tmp2 > exp(Crit) +polygon(c(ltmp[i1], rev(ltmp[i1])), + c(tmp1[i1], rep(-1, sum(i1))), + col=Col2, border=NA) +polygon(c(ltmp[i2], rev(ltmp[i2])), + c(tmp2[i2], rep(-1, sum(i2))), + col=Col4, border=NA) +abline(h=exp(Crit), col="grey") +lines(ltmp, tmp1, col=Col1, lwd=1) +lines(ltmp[i1], tmp1[i1], col=Col1, lwd=3) +lines(rep(ltmp[which.max(tmp1)], 2), c(1, -1), col=Col1) +lines(ltmp, tmp2, col=Col3, lwd=1) +lines(ltmp[i2], tmp2[i2], col=Col3, lwd=3) +lines(rep(ltmp[which.max(tmp2)], 2), c(1, -1), col=Col3) +box() +legend(0.1, 1, bty="n", border=c(Col1, Col3), fill=c(Col2, Col4), pt.cex=2, pt.lwd=2, + legend=c(expression(M[lambda*0]-SR), expression(M[lambda*beta]-SR))) +round(c(Max_Ml0=ltmp[which.max(tmp1)], CI=range(ltmp[i1])), 3) +round(c(Max_Mlx=ltmp[which.max(tmp2)], CI=range(ltmp[i2])), 3) + +Res1 <- pl_tMl0 +Res2 <- pl_tMlx +yv <- exp(Res1-max(Res1)) +plot(lam, yv, type="n", lwd=1, ylim=exp(c(2*Crit, 0)), + xlab=expression(lambda), ylab="Profile Likelihood Ratio") +tmp1 <- splinefun(lam, yv)(ltmp) +i1 <- tmp1 > exp(Crit) +yv <- exp(Res2-max(Res2)) +tmp2 <- splinefun(lam, yv)(ltmp) +i2 <- tmp2 > exp(Crit) +polygon(c(ltmp[i1], rev(ltmp[i1])), + c(tmp1[i1], rep(-1, sum(i1))), + col=Col2, border=NA) +polygon(c(ltmp[i2], rev(ltmp[i2])), + c(tmp2[i2], rep(-1, sum(i2))), + col=Col4, border=NA) +abline(h=exp(Crit), col="grey") +lines(ltmp, tmp1, col=Col1, lwd=1) +lines(ltmp[i1], tmp1[i1], col=Col1, lwd=3) +lines(rep(ltmp[which.max(tmp1)], 2), c(1, -1), col=Col1) +lines(ltmp, tmp2, col=Col3, lwd=1) +lines(ltmp[i2], tmp2[i2], col=Col3, lwd=3) +lines(rep(ltmp[which.max(tmp2)], 2), c(1, -1), col=Col3) +box() +legend(0.1, 1, bty="n", border=c(Col1, Col3), fill=c(Col2, Col4), pt.cex=2, pt.lwd=2, + legend=c(expression(M[lambda*0]-DD), expression(M[lambda*beta]-DD))) +round(c(Max_Ml0=ltmp[which.max(tmp1)], CI=range(ltmp[i1])), 3) +round(c(Max_Mlx=ltmp[which.max(tmp2)], CI=range(ltmp[i2])), 3) + +par(op) + +#dev.off() +``` + +### LOO cross validation plots + +```{r fig-2,width=14,height=7} +#pdf("Fig2.pdf", width=14, height=7) + +op <- par(mfrow=c(1,2)) + +Max <- 0.7 +plot(prp, xaxs = "i", yaxs = "i", type="n", + ylim=c(0, Max), xlim=c(0, Max), + xlab="Time-removal SR Estimate", ylab="LOO SR Estimate") +abline(0,1,lty=1, col=1) +abline(0,2,lty=2, col="grey") +abline(0,1/2,lty=2, col="grey") +abline(0,1.5,lty=2, col="grey") +abline(0,1/1.5,lty=2, col="grey") +points(prp, xaxs = "i", yaxs = "i", + col=c(Col1,Col3)[as.integer(x$Mig2)], + cex=0.2+2*x$MaxFreqkHz/10) +box() +legend("topleft", pch=21, col=c(Col1,Col3,1,1), + pt.cex=c(1.5,1.5,2,1), bty="n", + legend=c("Migrant", "Resident", "Song Picth: High", "Song Pitch: Low")) +text(0.9*Max, 0.05*Max, expression(M[lambda*beta]-SR)) + +Max <- 2.1 +plot(prt, xaxs = "i", yaxs = "i", type="n", + ylim=c(0, Max), xlim=c(0, Max), + xlab="Distance-sampling DD Estimate", ylab="LOO DD Estimate") +abline(0,1,lty=1, col=1) +abline(0,2,lty=2, col="grey") +abline(0,1/2,lty=2, col="grey") +abline(0,1.5,lty=2, col="grey") +abline(0,1/1.5,lty=2, col="grey") +points(prt, xaxs = "i", yaxs = "i", + cex=0.2+2*x$logmass/5, + col=c(Col1,Col3)[as.integer(x$Hab2)]) +box() +legend("topleft", pch=21, col=c(Col1,Col3,1,1), pt.cex=c(1.5,1.5,2,1), bty="n", + legend=c("Habitat: Closed", "Habitat: Open", "Body Mass: Large", "Body Mass: Small")) +text(0.9*Max, 0.05*Max, expression(M[0*beta]-DD)) + +par(op) + +#dev.off() +``` + diff --git a/inst/doc/lhreg.html b/inst/doc/lhreg.html index 8bfa4ab..3e034ef 100644 --- a/inst/doc/lhreg.html +++ b/inst/doc/lhreg.html @@ -74,8 +74,6 @@

2017-05-11

-
devtools::install_github("borealbirds/lhreg")
-devtools::build_vignettes("~/repos/lhreg")

Introduction

Explain what this Vignette is about, as a Supporting Info.

@@ -550,11 +548,253 @@

LOO analysis

pMlx = sum((pr_pMlx - pMlx$Y)^2)) MSEt <- SSEt / n MSEp <- SSEp / n
+

Use save.image() to save the results.

Results

-
#load(system.file("extdata", "lhreg-results-DE.Rdata", package = "lhreg"))
+

Load results and set some values:

+
load(system.file("extdata", "lhreg-results-DE.rda", package = "lhreg"))
+
+Crit <- -0.5*qchisq(0.95, 1)
+ltmp <- seq(0, 1, by=0.0001)
+## red-yl-blue
+Col <- c("#2C7BB6", "#6BAACF", "#ABD9E9", "#D4ECD3", "#FFFFBF", "#FED690",
+    "#FDAE61", "#EA633E", "#D7191C")
+Col1 <- Col[1]
+Col2 <- rgb(171/255, 217/255, 233/255, 0.5) # Col[3]
+Col3 <- Col[9]
+Col4 <- rgb(253/255, 174/255, 97/255, 0.5) # Col[7]
+prt <- exp(pr_tMx)
+prp <- exp(pr_pMlx)
+
+

Table with estimates and MSE

+
ff <- function(z) {
+    zz <- z$summary
+
+    pcut <- function(p) {
+        factor(c("***", "**", "*", "+", "ns")[as.integer(cut(p,
+            c(1, 0.1, 0.05, 0.01, 0.001, 0), include.lowest=TRUE, right=FALSE))],
+            levels=c("***", "**", "*", "+", "ns"))
+    }
+    structure(sapply(1:nrow(zz), function(i)
+        paste0(round(zz[i,1], 3), " (SE +/- ", round(zz[i,2], 3), pcut(zz[i,4]), ")")),
+        names=rownames(zz))
+}
+zzz <- lapply(list(pM0, pMp0, pMl0, pMx, pMpx, pMlx), ff)
+m <- matrix("", length(zzz[[6]]), 6)
+rownames(m) <- names(zzz[[6]])
+colnames(m) <- c("M0", "Mp0", "Ml0", "Mx", "Mpx", "Mlx")
+for (i in 1:6) {
+    j <- match(names(zzz[[i]]), rownames(m))
+    m[j,i] <- zzz[[i]]
+}
+ii <- grep("(SE +/- NANA)", m,fixed=TRUE)
+m[ii] <- gsub("(SE +/- NANA)", "(fixed)", m[ii], fixed=TRUE)
+m[m==""] <- "n/a"
+m1 <- m
+
+zzz <- lapply(list(tM0, tMp0, tMl0, tMx, tMpx, tMlx), ff)
+m <- matrix("", length(zzz[[6]]), 6)
+rownames(m) <- names(zzz[[6]])
+colnames(m) <- c("M0", "Mp0", "Ml0", "Mx", "Mpx", "Mlx")
+for (i in 1:6) {
+    j <- match(names(zzz[[i]]), rownames(m))
+    m[j,i] <- zzz[[i]]
+}
+ii <- grep("(SE +/- NANA)", m,fixed=TRUE)
+m[ii] <- gsub("(SE +/- NANA)", "(fixed)", m[ii], fixed=TRUE)
+m[m==""] <- "n/a"
+m2 <- m
+
+m1 <- rbind(m1, df=aicp$df, dAIC=round(aicp$dAIC, 3),
+    XV_MSE=round(MSEp, 3))
+m2 <- rbind(m2, df=aict$df, dAIC=round(aict$dAIC, 3),
+    XV_MSE=round(MSEt, 3))
+
+print.default(m1, quote=FALSE) # SR results
+
##                  M0                       Mp0                     
+## beta_Intercept   -1.449 (SE +/- 0.033***) -1.755 (SE +/- 0.396***)
+## beta_Mig2W       n/a                      n/a                     
+## beta_xMaxFreqkHz n/a                      n/a                     
+## sigma            0.374 (SE +/- 0.024***)  0.877 (SE +/- 0.062***) 
+## lambda           0 (fixed)                1 (fixed)               
+## df               2                        2                       
+## dAIC             41.061                   38.528                  
+## XV_MSE           0.207                    0.169                   
+##                  Ml0                      Mx                      
+## beta_Intercept   -1.712 (SE +/- 0.229***) -1.627 (SE +/- 0.088***)
+## beta_Mig2W       n/a                      -0.368 (SE +/- 0.08***) 
+## beta_xMaxFreqkHz n/a                      0.368 (SE +/- 0.122**)  
+## sigma            0.536 (SE +/- 0.066***)  0.328 (SE +/- 0.022***) 
+## lambda           0.824 (SE +/- 0.075***)  0 (fixed)               
+## df               3                        4                       
+## dAIC             1.74                     13.017                  
+## XV_MSE           0.154                    0.178                   
+##                  Mpx                     Mlx                     
+## beta_Intercept   -1.77 (SE +/- 0.397***) -1.722 (SE +/- 0.217***)
+## beta_Mig2W       -0.167 (SE +/- 0.131ns) -0.199 (SE +/- 0.104+)  
+## beta_xMaxFreqkHz 0.174 (SE +/- 0.116ns)  0.19 (SE +/- 0.126ns)   
+## sigma            0.863 (SE +/- 0.061***) 0.493 (SE +/- 0.07***)  
+## lambda           1 (fixed)               0.771 (SE +/- 0.106***) 
+## df               4                       5                       
+## dAIC             38.739                  0                       
+## XV_MSE           0.169                   0.153
+
print.default(m2, quote=FALSE) # DD results
+
##                  M0                       Mp0                    
+## beta_Intercept   -0.196 (SE +/- 0.026***) -0.085 (SE +/- 0.345ns)
+## beta_logmass     n/a                      n/a                    
+## beta_Mig2W       n/a                      n/a                    
+## beta_xMaxFreqkHz n/a                      n/a                    
+## beta_Hab2Open    n/a                      n/a                    
+## sigma            0.305 (SE +/- 0.019***)  0.762 (SE +/- 0.048***)
+## lambda           0 (fixed)                1 (fixed)              
+## df               2                        2                      
+## dAIC             80.685                   82.206                 
+## XV_MSE           0.098                    0.081                  
+##                  Ml0                     Mx                      
+## beta_Intercept   -0.085 (SE +/- 0.167ns) -0.582 (SE +/- 0.107***)
+## beta_logmass     n/a                     0.158 (SE +/- 0.022***) 
+## beta_Mig2W       n/a                     -0.168 (SE +/- 0.056**) 
+## beta_xMaxFreqkHz n/a                     -0.229 (SE +/- 0.088**) 
+## beta_Hab2Open    n/a                     0.145 (SE +/- 0.043***) 
+## sigma            0.416 (SE +/- 0.05***)  0.221 (SE +/- 0.014***) 
+## lambda           0.747 (SE +/- 0.097***) 0 (fixed)               
+## df               3                       6                       
+## dAIC             54.144                  0                       
+## XV_MSE           0.077                   0.057                   
+##                  Mpx                     Mlx                     
+## beta_Intercept   -0.814 (SE +/- 0.379*)  -0.581 (SE +/- 0.106***)
+## beta_logmass     0.188 (SE +/- 0.049***) 0.158 (SE +/- 0.022***) 
+## beta_Mig2W       -0.17 (SE +/- 0.102+)   -0.17 (SE +/- 0.057**)  
+## beta_xMaxFreqkHz -0.183 (SE +/- 0.089*)  -0.23 (SE +/- 0.087**)  
+## beta_Hab2Open    0.136 (SE +/- 0.049**)  0.145 (SE +/- 0.044***) 
+## sigma            0.69 (SE +/- 0.044***)  0.221 (SE +/- 0.014***) 
+## lambda           1 (fixed)               0.344 (SE +/- 0.459ns)  
+## df               6                       7                       
+## dAIC             62.718                  2.003                   
+## XV_MSE           0.063                   0.054
+

Note: estimates and p-values for lambda taken from profile likelihood results due to issues with boundary condition when traits used for DD model.

+
+
+

Profile likelihood figures

+
#pdf("Fig1.pdf", width=14, height=6)
+op <- par(mfrow=c(1,2))
+
+Res1 <- pl_pMl0
+Res2 <- pl_pMlx
+yv <- exp(Res1-max(Res1))
+plot(lam, yv, type="n", lwd=1, ylim=exp(c(2*Crit, 0)),
+    xlab=expression(lambda), ylab="Profile Likelihood Ratio")
+tmp1 <- splinefun(lam, yv)(ltmp)
+i1 <- tmp1 > exp(Crit)
+yv <- exp(Res2-max(Res2))
+tmp2 <- splinefun(lam, yv)(ltmp)
+i2 <- tmp2 > exp(Crit)
+polygon(c(ltmp[i1], rev(ltmp[i1])),
+    c(tmp1[i1], rep(-1, sum(i1))),
+    col=Col2, border=NA)
+polygon(c(ltmp[i2], rev(ltmp[i2])),
+    c(tmp2[i2], rep(-1, sum(i2))),
+    col=Col4, border=NA)
+abline(h=exp(Crit), col="grey")
+lines(ltmp, tmp1, col=Col1, lwd=1)
+lines(ltmp[i1], tmp1[i1], col=Col1, lwd=3)
+lines(rep(ltmp[which.max(tmp1)], 2), c(1, -1), col=Col1)
+lines(ltmp, tmp2, col=Col3, lwd=1)
+lines(ltmp[i2], tmp2[i2], col=Col3, lwd=3)
+lines(rep(ltmp[which.max(tmp2)], 2), c(1, -1), col=Col3)
+box()
+legend(0.1, 1, bty="n", border=c(Col1, Col3), fill=c(Col2, Col4), pt.cex=2, pt.lwd=2,
+    legend=c(expression(M[lambda*0]-SR), expression(M[lambda*beta]-SR)))
+round(c(Max_Ml0=ltmp[which.max(tmp1)], CI=range(ltmp[i1])), 3)
+
## Max_Ml0     CI1     CI2 
+##   0.835   0.627   0.933
+
round(c(Max_Mlx=ltmp[which.max(tmp2)], CI=range(ltmp[i2])), 3)
+
## Max_Mlx     CI1     CI2 
+##   0.789   0.447   0.921
+
Res1 <- pl_tMl0
+Res2 <- pl_tMlx
+yv <- exp(Res1-max(Res1))
+plot(lam, yv, type="n", lwd=1, ylim=exp(c(2*Crit, 0)),
+    xlab=expression(lambda), ylab="Profile Likelihood Ratio")
+tmp1 <- splinefun(lam, yv)(ltmp)
+i1 <- tmp1 > exp(Crit)
+yv <- exp(Res2-max(Res2))
+tmp2 <- splinefun(lam, yv)(ltmp)
+i2 <- tmp2 > exp(Crit)
+polygon(c(ltmp[i1], rev(ltmp[i1])),
+    c(tmp1[i1], rep(-1, sum(i1))),
+    col=Col2, border=NA)
+polygon(c(ltmp[i2], rev(ltmp[i2])),
+    c(tmp2[i2], rep(-1, sum(i2))),
+    col=Col4, border=NA)
+abline(h=exp(Crit), col="grey")
+lines(ltmp, tmp1, col=Col1, lwd=1)
+lines(ltmp[i1], tmp1[i1], col=Col1, lwd=3)
+lines(rep(ltmp[which.max(tmp1)], 2), c(1, -1), col=Col1)
+lines(ltmp, tmp2, col=Col3, lwd=1)
+lines(ltmp[i2], tmp2[i2], col=Col3, lwd=3)
+lines(rep(ltmp[which.max(tmp2)], 2), c(1, -1), col=Col3)
+box()
+legend(0.1, 1, bty="n", border=c(Col1, Col3), fill=c(Col2, Col4), pt.cex=2, pt.lwd=2,
+    legend=c(expression(M[lambda*0]-DD), expression(M[lambda*beta]-DD)))
+

+
round(c(Max_Ml0=ltmp[which.max(tmp1)], CI=range(ltmp[i1])), 3)
+
## Max_Ml0     CI1     CI2 
+##   0.759   0.504   0.899
+
round(c(Max_Mlx=ltmp[which.max(tmp2)], CI=range(ltmp[i2])), 3)
+
## Max_Mlx     CI1     CI2 
+##   0.000   0.000   0.421
+
par(op)
+
+#dev.off()
+
+
+

LOO cross validation plots

+
#pdf("Fig2.pdf", width=14, height=7)
+
+op <- par(mfrow=c(1,2))
+
+Max <- 0.7
+plot(prp, xaxs = "i", yaxs = "i", type="n",
+    ylim=c(0, Max), xlim=c(0, Max),
+    xlab="Time-removal SR Estimate", ylab="LOO SR Estimate")
+abline(0,1,lty=1, col=1)
+abline(0,2,lty=2, col="grey")
+abline(0,1/2,lty=2, col="grey")
+abline(0,1.5,lty=2, col="grey")
+abline(0,1/1.5,lty=2, col="grey")
+points(prp, xaxs = "i", yaxs = "i",
+    col=c(Col1,Col3)[as.integer(x$Mig2)],
+    cex=0.2+2*x$MaxFreqkHz/10)
+box()
+legend("topleft", pch=21, col=c(Col1,Col3,1,1),
+    pt.cex=c(1.5,1.5,2,1), bty="n",
+    legend=c("Migrant", "Resident", "Song Picth: High", "Song Pitch: Low"))
+text(0.9*Max, 0.05*Max, expression(M[lambda*beta]-SR))
+
+Max <- 2.1
+plot(prt, xaxs = "i", yaxs = "i", type="n",
+    ylim=c(0, Max), xlim=c(0, Max),
+    xlab="Distance-sampling DD Estimate", ylab="LOO DD Estimate")
+abline(0,1,lty=1, col=1)
+abline(0,2,lty=2, col="grey")
+abline(0,1/2,lty=2, col="grey")
+abline(0,1.5,lty=2, col="grey")
+abline(0,1/1.5,lty=2, col="grey")
+points(prt, xaxs = "i", yaxs = "i",
+    cex=0.2+2*x$logmass/5,
+    col=c(Col1,Col3)[as.integer(x$Hab2)])
+box()
+legend("topleft", pch=21, col=c(Col1,Col3,1,1), pt.cex=c(1.5,1.5,2,1), bty="n",
+    legend=c("Habitat: Closed", "Habitat: Open", "Body Mass: Large", "Body Mass: Small"))
+text(0.9*Max, 0.05*Max, expression(M[0*beta]-DD))
+

+
par(op)
+
+#dev.off()
+