0. Concept Review

A. Sampling

Sampling is the selection of observations from a “population of interest” to gain insights and draw inferences about that population.

To avoid biases, sampling should be done very carefully. A sample should be random and have the same characteristics of the population it is representing (e.g. blood type, height, sex, etc).

Statisticians have many ways to achieve this, however, we’ll focus on random sampling using the sample() and replicate() functions in R for this project/assignment.

B. Point Estimation

Point Estimation is a single number calculated from sample data and used to estimate a population parameter. For example:
  • Point Estimate of the Population Mean μ is the Sample Mean or x̄.
  • Point Estimate for the Population Variance σ² is the Sample Variance s².

C. Central Limit Theorem

Some key takeaways from the Central Limit Theorem (CLT):
  • The distribution of the sample means has an approximate normal distribution as long as the sample size is large enough (> 30 observations) and all samples have the same size
  • This holds true for other sample statistics, such as the sample proportion
  • Law of Large Numbers - if you take samples of larger and larger size from any population, then the sample mean (x̄) will approach the population mean (μ)
  • Since the standard deviation is σ/√n , as n increases, the standard deviation (variance) gets smaller.


1. Library

Load the necessary libraries into this R studio session.

Note: some packages may need to be installed. Use the install.package() function or search for the package at R Studio Menu Bar > Tools > Install Packages.

Many libraries used below are not necessary to complete this assignment.

library(tidyr)
library(readxl)
library(dplyr)
library(stringr)
library(writexl)
library(ggplot2)
library(httpuv)
library(googledrive)
library(googlesheets4)
library(knitr)
library(xtable)
library(glue)
library(rmdformats)
library(bookdown)



2. Loading Files

For this assignment, our file and directory are hosted by Google drive. As such, we need to authorize access to the drive and read the necessary file(s) into this R studio session.

The code below give us access to our Google drive. Check the console output for additional instructions.

googledrive::drive_auth()
sample_data2 = read_sheet(gs4_find('sample_data2'))


Next, we need to read and assign the Google spreadsheet to a variable.

sample_data2 = read_sheet(gs4_find('sample_data2'))


Let’s take a look at the first five rows of the data frame to ensure the import was successful and correct.

view_sample = sample_data2[1:5,]
kable(view_sample, align = "llllcccc", caption = "Sample Data - 2020 Population by County and State")
Sample Data - 2020 Population by County and State
Census_Year State_County County State Population State_Code Unique_State_County County_Code
2020 Coahoma County, Mississippi Coahoma County Mississippi 22124 28 28-027 027
2020 Jasper County, Mississippi Jasper County Mississippi 16383 28 28-061 061
2020 Jones County, Mississippi Jones County Mississippi 68098 28 28-067 067
2020 Choctaw County, Mississippi Choctaw County Mississippi 8210 28 28-019 019
2020 Hancock County, Mississippi Hancock County Mississippi 47632 28 28-045 045



3. Summary Statistics

We can run the summary function to get a full panel of descriptive statistics (see code below).

For this assignment, the population mean is the only parameter necessary. Since a single population observation equates to one person, we’re also going to round the population mean to the nearest number.

dstats = summary(sample_data2)

pop_mean = round(mean(sample_data2$Population))
glue("Population Mean (Mu): {pop_mean}")
## Population Mean (Mu): 102930



4. Point Estimate (μ): n sizes

This assignment assumes that we DO NOT have all the population data which is why we’re using the point estimate method to derive the best estimation of our population mean.

Important Note: each time we run the code below, we’re sampling from the total population. Every occurrence uses a different combination of observations to calculate our point estimate. As a result, we will always produce a different point estimate of the sample mean.

Point Estimation (20 observations)

Using the sample function to select 20 random samples followed by the mean function to derive the average.

sample_20 = mean(sample(sample_data2$Population, size = 20))
glue("Point Estimate (20 Observations): {sample_20}")
## Point Estimate (20 Observations): 116997

The variance between our population mean of 102930 and this point estimation is due to randomness. As we increase the number of observations used, our point estimations will moving closer and closer to the observed population mean.

Point Estimation (200 observations)

Using the sample function to select 200 random samples (10x the previous sample) followed by the mean function to derive the average.

sample_200 = mean(sample(sample_data2$Population, size = 200))
glue("Point Estimate (200 Observations): {sample_200}")
## Point Estimate (200 Observations): 113301.26


Point Estimation (2000 observations)

Using the sample function to select 200 random samples (10x the previous sample and slight 1000 less observations than the population data set) followed by the mean function to derive the average.

sample_2000 = mean(sample(sample_data2$Population, size = 2000))
glue("Point Estimate (2000 Observations): {sample_2000}")
## Point Estimate (2000 Observations): 108691.28



5. Point Estimate (μ): Replicate

In this section, we calculate the sample mean using a fixed sample of 100 observations. Then we use the replicate function to repeat this process n number of times and examine how closely we can approximate the true population mean.

In the example below, we replicate the sampling of 100 observations 40 times provide us with different 400 means. Then we compute a mean of means:

rsample_40 = mean(replicate(40,mean(sample(sample_data2$Population, size=100))))
print(rsample_40)
## [1] 100955.9


Increasing the number of replications to 400 would result in a mean of mean even closer to the population mean:

rsample_400 = mean(replicate(400,mean(sample(sample_data2$Population, size=100))))
print(rsample_400)
## [1] 103293


Let’s set the replicate function to 4000 and compare to the population mean:

rsample_4000 = mean(replicate(4000,mean(sample(sample_data2$Population, size=100))))
print(rsample_4000)
## [1] 102369.1


The tables below aggregates the results from using sampling and sompling with replication. Two key takeways:

  1. Increasing the sample size will move us closer to the true population mean.

  2. Increasing the sample size via replication is a faster and more efficient method of getting as close as possible to our population mean.

1. Point Estimation of Mean with Sampling
n x \(\mu\) \(\Delta\)(%)
20 116997.0 102930 -0.1202339
200 113301.3 102930 -0.0915370
2000 108691.3 102930 -0.0530059
2. Point Estimation of Mean (Fixed Sample with Replication)
n Replicate Factor # of Means \(\chi\) \(\mu\) \(\Delta\)(%)
100 40 400 100955.9 102930 0.0195539
100 400 4000 103293.0 102930 -0.0035142
100 4000 40000 102369.1 102930 0.0054796



6. Histogram of Means

We can analyze the histograms of the previous “mean of means” to illustrate how an increase in the frequency of computed sample means impacts the distribution of means.

hist(replicate(40,mean(sample(sample_data2$Population, size=100),simplify=TRUE)), xlab = 'Sample Means', ylab = 'Frequency', main = 'Distribution of Sample Means = 40 Replications', col = 'blue', border = 'white')


hist(replicate(400,mean(sample(sample_data2$Population, size=100),simplify=TRUE)), xlab = 'Sample Means', ylab = 'Frequency', main = 'Distribution of Sample Means - 400 Replications', col = 'purple', border = 'white')


hist(replicate(4000,mean(sample(sample_data2$Population, size=100),simplify=TRUE)), xlab = 'Sample Means', ylab = 'Frequency', main = 'Distribution of Sample Means - 4000 Replications', col = 'grey', border = 'white', breaks = 30)


7. Overall Conclusions

  1. As we increase the number of sample means, more and more sample means fall within the same bin of our population mean (or nearby bins depending on bin sizes).

  2. Consistent with the Central Limit Theorem, when we repeatedly draw samples of a specific size (100), calculate the means and display those means on a histogram, the histogram has a normal bell shape distribution

  3. Outliers increase the variability in the sample but as we increase n, variance decreases, as noted by the bell shaped distribution.

  4. Consistent with the law of large numbers, as we increase n, x̄ moves closer and closer to μ. This is visiable both in the histograms and the delta columns in the charts of section 5.