knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%", messages = FALSE, warnings = FALSE )
The goal of drpop is to provide users doubly robust and efficient estimates of population size and the confidence intervals for a capture-recapture problem.
You can install the released version of drpop from CRAN with:
install.packages("drpop")
And the development version from GitHub with:
# install.packages("devtools") devtools::install_github("mqnjqrid/drpop")
This is a basic example which shows you how to solve a common problem:
library(drpop) n = 1000 x = matrix(rnorm(n*3, 2, 1), nrow = n) expit = function(xi) { exp(sum(xi))/(1 + exp(sum(xi))) } y1 = unlist(apply(x, 1, function(xi) {sample(c(0, 1), 1, replace = TRUE, prob = c(1 - expit(-0.6 + 0.4*xi), expit(-0.6 + 0.4*xi)))})) y2 = unlist(apply(x, 1, function(xi) {sample(c(0, 1), 1, replace = TRUE, prob = c(1 - expit(-0.6 + 0.3*xi), expit(-0.6 + 0.3*xi)))})) datacrc = cbind(y1, y2, exp(x/2))[y1+y2 > 0, ] estim <- popsize(data = datacrc, func = c("gam"), nfolds = 2, K = 2) # The population size estimates are 'n' and the standard deviations are 'sigman' print(estim) ## basic example code
The following shows the confidence interval of estimates for a toy data. Real population size is 3000.
library(drpop) n = 3000 x = matrix(rnorm(n*3, 2,1), nrow = n) expit = function(xi) { exp(sum(xi))/(1 + exp(sum(xi))) } y1 = unlist(apply(x, 1, function(xi) {sample(c(0, 1), 1, replace = TRUE, prob = c(1 - expit(-0.6 + 0.4*xi), expit(-0.6 + 0.4*xi)))})) y2 = unlist(apply(x, 1, function(xi) {sample(c(0, 1), 1, replace = TRUE, prob = c(1 - expit(-0.6 + 0.3*xi), expit(-0.6 + 0.3*xi)))})) datacrc = cbind(y1, y2, exp(x/2))[y1+y2>0,] estim <- popsize(data = datacrc, func = c("gam", "rangerlogit"), nfolds = 2, margin = 0.01) print(estim) plotci(estim) #result = melt(as.data.frame(estim), variable.name = "estimator", value.name = "population_size") #ggplot(result, aes(x = population_size - n, fill = estimator, color = estimator)) + # geom_density(alpha = 0.4) + # xlab("Bias on n")
The following shows confidence interval of estimates for toy data with a categorical covariate. Real population size is 10000.
library(drpop) n = 10000 x = matrix(rnorm(n*3, 2, 1), nrow = n) expit = function(xi) { exp(sum(xi))/(1 + exp(sum(xi))) } catcov = sample(c('m','f'), n, replace = TRUE, prob = c(0.45, 0.55)) y1 = unlist(apply(x, 1, function(xi) {sample(c(0, 1), 1, replace = TRUE, prob = c(1 - expit(-0.6 + 0.4*xi), expit(-0.6 + 0.4*xi)))})) y2 = sapply(1:n, function(i) {sample(c(0, 1), 1, replace = TRUE, prob = c(1 - expit(-0.6 + 0.3*(catcov[i] == 'm') + 0.3*x[i,]), expit(-0.6 + 0.3*(catcov[i] == 'm') + 0.3*x[i,])))}) datacrc = cbind.data.frame(y1, y2, exp(x/2), catcov)[y1+y2>0,] result = popsize_cond(data = datacrc, condvar = 'catcov') fig = plotci(result) fig + geom_hline(yintercept = table(catcov), color = "brown", linetype = "dashed")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.