2024-02-01

Types of Sampling

There are different types of sampling that can be used for data manipulation, some common ones are

  • Simple Random
  • Systematic
  • Stratified
  • Cluster

Plotly simple random sampling

This plot show’s the entirety of the data, comparing the Height and Width against the Girth. The black spots are the random sample, showing how the sampled data may not cover the extremities, but gives a good sample overall

Code for previous slide

random_sample = sample_n(trees, 10)

plot_ly(x = trees$Volume, y = trees$Height, z = trees$Girth,
        type = "scatter3d", 
        name = "All Trees", 
        mode = "markers", 
        color = trees$Girth) %>%
  add_trace(x = random_sample$Volume, y = random_sample$Height, 
            z = random_sample$Girth,
            type = "scatter3d",
            name = "Sample", 
            mode = "markers", 
            color = random_sample$Girth,
            marker = list(color = rgb(0,0,0)))

Formula for Systematic Sampling

\(n_h = {N_h \over N} n\)

To create variable \(n_h\) we divide the total stratum (\(N_h\)) over the total population (\(N\)), then multiplied by the sample size (\(n\))

This formula will be written into R code for the next slide

Systematic Sampling

R code

  syst = function(N,n){
  k = ceiling(N/n)
  r = sample(1:k, 1)
  seq(r, r + k*(n-1), k)
}
  all = trees %>%
  select(Girth, Height)

syst_sample = trees[syst(nrow(trees), 10),] %>%
  select(Girth, Height)
fig_sys = ggplot(syst_sample, aes(x=Girth, y=Height)) + 
  geom_point(aes(color = "Systematic Sample"), size = 5) +
  geom_point(data = all, aes(color = "All Data"))

ggplot 2 Stratified

This graph shows us how it can be important to separate your sample first before pulling numbers, as these different species have very different width and lengths of their petals

Sample Size Formula

For infinite population \(Z^2 * P * {1 - P \over M^2}\)

For an adjusted sample size \({S \over 1 + {S - 1 \over Population}}\)

  • Z = The Z or standard score
  • P = Population (as a proportion)
  • M = Margin of error