In this data dive, I will be looking at sampling in my data set to further analyze the data.
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
cdata <- read.csv("/Users/cadgo/Downloads/data_aging_congress.csv")
cdf <- data.frame(cdata)
Now that we have our population data set, we will now create 5 different samples of the data containing about 50% of the original dataset.
n <- 29120/2
sdf1 <- cdf[sample(nrow(cdf), n), ]
sdf2 <- cdf[sample(nrow(cdf), n), ]
sdf3 <- cdf[sample(nrow(cdf), n), ]
sdf4 <- cdf[sample(nrow(cdf), n), ]
sdf5 <- cdf[sample(nrow(cdf), n), ]
Now we investigate these sample datasets to see if they deviate off of the population data set. The first row is the average age in the population dataset, with the following rows being the average in the sample datasets.
mean(cdf$age_years)
## [1] 53.73248
mean(sdf1$age_years)
## [1] 53.75348
mean(sdf2$age_years)
## [1] 53.71857
mean(sdf3$age_years)
## [1] 53.73307
mean(sdf4$age_years)
## [1] 53.69582
mean(sdf5$age_years)
## [1] 53.80852
As you can see above, the average does not fluctuate much between the samples. Now how about we look at the youngest age in each data set?
min(cdf$age_years)
## [1] 23.66598
min(sdf1$age_years)
## [1] 23.66598
min(sdf2$age_years)
## [1] 23.66598
min(sdf3$age_years)
## [1] 23.66598
min(sdf4$age_years)
## [1] 23.66598
min(sdf5$age_years)
## [1] 23.66598
They youngest member in each sample is only different in two of the datasets, where one is two years older than the actual youngest member in congress. Now how about we look at the oldest age in each data set?
max(cdf$age_years)
## [1] 98.08077
max(sdf1$age_years)
## [1] 96.0794
max(sdf2$age_years)
## [1] 98.08077
max(sdf3$age_years)
## [1] 96.0794
max(sdf4$age_years)
## [1] 96.0794
max(sdf5$age_years)
## [1] 98.08077
The oldest member in each sample only varies in the first two samples, where the oldest member in the first sample is 6 years younger than the actual oldest.
Now lets look at the quantile distribution of each sample
quantile(cdf$age_years, probs=c(0.25, 0.5, 0.75))
## 25% 50% 75%
## 45.80972 53.45106 61.21629
quantile(sdf1$age_years, probs=c(0.25, 0.5, 0.75))
## 25% 50% 75%
## 45.83162 53.46064 61.14511
quantile(sdf2$age_years, probs=c(0.25, 0.5, 0.75))
## 25% 50% 75%
## 45.82546 53.35661 61.25257
quantile(sdf3$age_years, probs=c(0.25, 0.5, 0.75))
## 25% 50% 75%
## 45.83984 53.40999 61.15880
quantile(sdf4$age_years, probs=c(0.25, 0.5, 0.75))
## 25% 50% 75%
## 45.74127 53.46475 61.15400
quantile(sdf5$age_years, probs=c(0.25, 0.5, 0.75))
## 25% 50% 75%
## 45.86927 53.55373 61.24641
The Quantile distributions do not vary much between the sample data sets.
Now lets look at the amount of unique members in each data set.
length(unique(cdf$bioname))
## [1] 5251
length(unique(sdf1$bioname))
## [1] 4456
length(unique(sdf2$bioname))
## [1] 4443
length(unique(sdf3$bioname))
## [1] 4439
length(unique(sdf4$bioname))
## [1] 4461
length(unique(sdf5$bioname))
## [1] 4440
Because the sample data sets are only half of the size of the population dataset, it is expected that the amount of unique members is lower. What is interesting is that they are only around 800 members lower with half of the data that contains 29120 rows. Even in the amount of unique members, there’s not too much variation between the sample sets.
Now let’s look at the amount of members in each chamber of congress.
table(cdf$chamber)
##
## House Senate
## 23622 5498
table(sdf1$chamber)
##
## House Senate
## 11813 2747
table(sdf2$chamber)
##
## House Senate
## 11763 2797
table(sdf3$chamber)
##
## House Senate
## 11806 2754
table(sdf4$chamber)
##
## House Senate
## 11836 2724
table(sdf5$chamber)
##
## House Senate
## 11839 2721
Comparing the sample data sets, the most extreme sets have a difference of about 100 members in both chambers. If you were to do a % analysis of the amount of members in each chamber, the samples would pull noticably different results.
Now let’s look at some basic statistics for the average terms served in congress
mean(cdf$cmltv_cong)
## [1] 5.413736
mean(sdf1$cmltv_cong)
## [1] 5.433516
mean(sdf2$cmltv_cong)
## [1] 5.39272
mean(sdf3$cmltv_cong)
## [1] 5.374588
mean(sdf4$cmltv_cong)
## [1] 5.410577
mean(sdf5$cmltv_cong)
## [1] 5.440385
The same kind of results show up for the average terms served in congress. There is little varaince between the data sets.
With this exercise in sampling data out of a population data set, you can see how the summary statistics for each column can vary. Since it is near impossible to collect every instance/record/person for many datasets, we have to look at these statistics with certain confidence levels. The smaller the sample, the less confidence you can put in the statistics of that data set. Although the variance in mine was very low, my sample data sets were still quite large. This allowed for more normalization of the data to get the sample sets statistics closer to the population.
In the future when I collect data for a project, it will be important to collect as much data as possible to get the statistics of the set as close to the true value as possible. Outliers in smaller datasets have a much more significant impact and they can be mitigated by making the data set larger.