sampler R package

R Package for Sample Design, Drawing, & Data Analysis Using Data Frames

The sampler R package is designed to enable data scientists to design, draw, and analyze simple or complex samples using data frames. It enables you to load machine-readable files (e.g. .csv, .tsv, etc.) in R containing a sampling frame or collected data, store them as objects, and perform sampling techniques and analysis using clear and concise methods.

Specifically, a data scientist can use the sampler R package to:

The sampler R package builds a bridge for survey administrators between the free and open-source R environment and no-to-low cost Open Data Kit (ODK)-based toolkits such as Ona and NEMO. The sampler package is available via CRAN or GitHub for use in R and R Studio.

To install in R from CRAN:

install.packages("sampler")
library(sampler)

To install in R from GitHub:

install.packages("devtools"); library(devtools)
# devtools::install_github("mbaldassaro/sampler")
install.packages("sampler", repos = c('https://mbaldassaro.r-universe.dev', 'https://cloud.r-project.org')); library(sampler)

The sampler R package includes the following datasets:

Full documentation of datasets and functions can be found on RDocumentation

Determine random sample size

rsampcalc(N, e, ci=95,p=0.5, over=0)

Where:

Returns appropriate sample size (rounded up to nearest integer)

Example:

rsampcalc(N=5361, e=3, ci=95, p=0.5, over=0.1)

Source: Sampling Design & Analysis, S. Lohr, 1999, equation 2.17

Draw a simple random sample

rsamp(df, n, over=0, rep=FALSE)

Where:

Returns a simple random sample of size n

Example:

rsamp(albania, n=360, over=0.1, rep=FALSE)

or

size <- rsampcalc(nrow(albania), 3, 95, 0.5)
rsamp(albania, size)

Determine sample size by strata using proportional allocation

ssampcalc(df, n, strata, over=0)

Where:

Returns proportional sample size per strata (rounded up to nearest integer)

Example:

ssampcalc(df=albania, n=544, strata=qarku, over=0.05)

or

size <- rsampcalc(nrow(albania), 3, 95, 0.5)
ssampcalc(albania, size, qarku)

Source: Sampling Design & Analysis, S. Lohr, 1999, 4.4

Draw stratified sample (proportional allocation)

ssamp(df, n, strata, over=1)

Where:

Returns stratified sample using proportional allocation without replacement

Example:

ssamp(df=albania, n=360, strata=qarku, over=0.1)

or

size <- rsampcalc(nrow(albania), 3, 95, 0.5)
ssamp(albania, size, qarku)

Determine sample size by strata using sub-units

psampcalc(df, n, strata, unit, over=0)

Where:

Returns sample size per strata based on sub-units (rounded up to nearest integer)

Example

psampcalc(df=albania, n=544, strata=qarku, unit=zgjedhes, over=0.1)

Source: Sampling Design & Analysis, S. Lohr, 1999, 4.4

Calculate proportion and margin of error (simple random sample)

rpro(df, col_name, ci=95, na="", N=0)

Where:

Returns table of responses (n), proportions (midpoint), margins of error, lower and upper bounds by factor for a given variable

Example:

rpro(df=opening, col_name=openTime, ci=95, na="n/a", N=5361)

Source: Sampling Design & Analysis, S. Lohr, 1999, Equation 2.15

Calculate proportion and margin of error (stratified sample)

spro(fulldf, sampdf, strata, col_name, ci=95, na="")

Where:

Returns table of responses (n), proportions (midpoint), margins of error, lower and upper bounds by factor for a given variable in a stratified sample

Example:

spro(fulldf=albania, sampdf=opening, strata=qarku, col_name=openTime, ci=95, na="n/a")

Source: Sampling Design & Analysis, S. Lohr, 1999, 4.6 & 4.7

Calculate proportion and margin of error (unequal-sized cluster sample)

cpro(df, numerator, denominator, ci=95, na="", N=0)

Where:

Returns table of responses (n), proportions (midpoint), margins of error, lower and upper bounds by factor for a given variable in an unequal-sized cluster sample

Example:

alresults <- ssamp(albania, 890, qarku)
cpro(df=alresults, numerator=totalVoters, denominator=zgjedhes, ci=95)
cpro(df=alresults, numerator=pd, denominator=validVotes, ci=95, N=5361)

Source 1: Survey Sampling, L. Kish, 1965, Equation 6.3.4

Source 2: Sampling Techniques, W.G. Cochran, 1977, Equation 3.34

Identify missing points between sample and collected data

rmissing(sampdf, colldf, col_name)

Where:

Returns table of sample points missing from collected data

Example:

alsample <- rsamp(df=albania, 544)
alreceived <- rsamp(df=alsample, 390)
rmissing(sampdf=alsample, colldf=alreceived, col_name=qvKod)

Identify number of missing points by strata between sample and collected data

smissing(sampdf, colldf, strata, col_name)

Where:

Returns table of number of sample points by strata missing from collected data

Example:

alsample <- rsamp(df=albania, 544)
alreceived <- rsamp(df=alsample, 390)
smissing(sampdf=alsample, colldf=alreceived, strata=qarku, col_name=qvKod)

Identify duplicate values within collected data

dupe(df, col_name)

Where:

Returns table of duplicate values within collected data

Example:

aldupe <- rsamp(df=albania, n=390, rep=TRUE)
dupe(df=aldupe, col_name=qvKod)

Remove observations based on duplicate values within collected data

dedupe(df, col_name)

Where:

Returns table of observations based on unique values within collected data

Example:

aldupe <- rsamp(df=albania, n=390, rep=TRUE)
dedupe(df=aldupe, col_name=qvKod)