loading the data & getting a preview with the head() function
## year boys girls
## 1 1940 1211684 1148715
## 2 1941 1289734 1223693
## 3 1942 1444365 1364631
## 4 1943 1508959 1427901
## 5 1944 1435301 1359499
## 6 1945 1404587 1330869
## year boys girls
## Min. :1940 Min. :1211684 Min. :1148715
## 1st Qu.:1956 1st Qu.:1799857 1st Qu.:1711404
## Median :1971 Median :1924868 Median :1831679
## Mean :1971 Mean :1885600 Mean :1793915
## 3rd Qu.:1986 3rd Qu.:2058524 3rd Qu.:1965538
## Max. :2002 Max. :2186274 Max. :2082052
From this output of summary() we can see that there are three columns, or variables to the data with the names: ‘year’, ‘boys’ & ’girls.
## [1] 63 3
From the output of dim() we can see that there are 63 rows with entries of the 3 different data variables. This is not a large dataset, so we can simply view the data in the ‘years’ column to see what years are included in the data set
## [1] 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954
## [16] 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969
## [31] 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
## [46] 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
## [61] 2000 2001 2002
Indeed, the output above gives us a good idea of what years are imcluded, but what if our dataset had 63,000 entries instead of just 63? Let’s use a few simple ways to explicitly learn about the ‘years’ data.
# we can use the summary() function to access informtion about the features of
#our dataset. the 'Min.' and 'Max.' values will give us an idea of the range or
#years that are covered by this data.
print( summary(present$year)['Min.'] )## Min.
## 1940
## Max.
## 2002
The summary() functions gives us some summary statistics on the feature ‘year’ for our data frame ‘present’. From this we get the range of years Min.:Max. = 1940-2002
#make a vector that holds the values for the years from the 'Min.' to 'Max.' of
#'year'
yearsInRange <- seq(summary(present$year)[['Min.']][1],summary(present$year)[['Max.']][1],by=1)
#check to see if the values in yearsInRange equals those in present$years
all( yearsInRange == present$year )## [1] TRUE
The above few lines of code gives a little sanity check that comfirms that ‘year’ includes each year from 1940 to 2002 inclusive of both ends of the range.
#load the arbthnot dataframe so that we can compare values
source("more/arbuthnot.r")
#use the summary() function to look at the summary statistics for the 'boys' and #'girls' counts data.
summary( arbuthnot )## year boys girls
## Min. :1629 Min. :2890 Min. :2722
## 1st Qu.:1649 1st Qu.:4759 1st Qu.:4457
## Median :1670 Median :6073 Median :5718
## Mean :1670 Mean :5907 Mean :5535
## 3rd Qu.:1690 3rd Qu.:7576 3rd Qu.:7150
## Max. :1710 Max. :8426 Max. :7779
## year boys girls
## Min. :1940 Min. :1211684 Min. :1148715
## 1st Qu.:1956 1st Qu.:1799857 1st Qu.:1711404
## Median :1971 Median :1924868 Median :1831679
## Mean :1971 Mean :1885600 Mean :1793915
## 3rd Qu.:1986 3rd Qu.:2058524 3rd Qu.:1965538
## Max. :2002 Max. :2186274 Max. :2082052
A glance at the Mean values for ‘boys’ and ‘girls’ features for both datasets suggests that the counts for the ‘present’ data are several orders of magnitude greater than that of the ‘arbuthnot’ dataset. We can further quantify this….
#take a subset of arbuthnot that has the same dimensions (number of years) as
#the present dataset. We are just doing this to get an idea of the differences
#in magnutide between the counts in the datasets, so we will just arbitrarily
#use the first 63 entries to match the number of entries in 'present'
subArbuthnot <- arbuthnot[ 1:dim( present )[1],]
#check that the dimensions are the same
dim( subArbuthnot ) == dim( present )## [1] TRUE TRUE
#calculate the mean proportion of 'boys' counts that are from the subset of the #'arbuthnot' dataset
meanRatioBoys <- mean( subArbuthnot$boys / present$boys)
#calculate the mean proportion of 'girls' counts that are from the subset of the #'arbuthnot' dataset
meanRatioGirls <- mean( subArbuthnot$girls / present$girls )
cat("Proportion of arbuthnot 'boys' counts: ", meanRatioBoys )## Proportion of arbuthnot 'boys' counts: 0.002874931
## Proportion of arbuthnot 'girls' counts: 0.002822238
From the proportions of counts for ‘boys’ and ‘girls’ above, we learn that the counts in the ‘present’ dataframe are approximately 3 orders of magnitude greater than those of the arbuthnot dataframe.
#generate data for the boy-to-girl ratio of the 'present' dataframe
b2gRatio_present <- present$boys / present$girls
#plot the ratio as a scatter plot
plot(x = present$year, y = b2gRatio_present,
ylim=c(0.9,1.1),
xlab='Year',
ylab='Ratio (boys:girls)',
main='Ratio of boys:girls counts')
abline(h=1, col="blue")From the figure above we can see that the datapoints for each year lie above 1.00 (indicated here with a blue line). This tells us that for each years counts had proportionately more boys counts than girl counts. If the counts were equal, the ratio’s value would equal 1 (blue line), and if there had been a year with more ‘girls’ counts than ‘boys’, that ratio value would lie below 1.0. Neither of these alternatives are observed in the figure. Therefore, arbuthnot’s observation about boys being born in greater proportion than girls does hold up in the U.S.
However, this representation is not very intuitive, so we can recalculate and plot the data as a percentage of ‘boys’ counts from the total counts.
#generate data for the boy-to-girl ratio of the 'present' dataframe
b2gPercent_present <- ( present$boys / ( present$girls + present$boys ) )*100
#plot the ratio as a scatter plot
p <- plot(x = present$year, y = b2gPercent_present,
ylim=c(48,52),
xlab='Year',
ylab='% boys',
main="Percentage of 'boys' counts")
abline(h=50, col="blue")The figure above replots the data as a percentage of ‘boys’ counts. This is more intuitive as most people are very familiar with considering percentages of a quantity rather than ratios of two quantities. From this representation we see the same finding: for each year of the data, the ‘boys’ counts make up a larger percentage of the total counts than ‘girls’ counts do, because each point of %boys counts data is greater than 50% (indicated with a blue line in the figure).
#make a new column in the present dataframe to hold values of the total number
#of births ('boys' + 'girls')
present$total <- ( present$boys + present$girls )
#use the head() function to check that the addition of the total column worked
#as expected
head( present )## year boys girls total
## 1 1940 1211684 1148715 2360399
## 2 1941 1289734 1223693 2513427
## 3 1942 1444365 1364631 2808996
## 4 1943 1508959 1427901 2936860
## 5 1944 1435301 1359499 2794800
## 6 1945 1404587 1330869 2735456
## year boys girls total
## 22 1961 2186274 2082052 4268326
From the above result, we see that the year with the highest count value was 1961. This is surprising and not what was expected from our previous analysis of the arbuthnot dataset. The arbuthnot data had an overall trend of an increase as a function of years. If the present dataset were to follow the same trend, the expected maximum value would be towards the later years in the 20th century or perhaps even on of the few datapoints in the 2000s.
Let’s plot ‘total’ as a function of years
#plot 'total'
plot(present$year, present$total,
xlab='Year',
ylab='count',
main="Total Number of Births")The plot of the total population counts above confirms that the year 1961 represents the peak of birth counts that occured during the mid 20th century in the U.S. and confirms that the term Baby Boomers is not an exaggeration at all!
Baby Boomer 1000 piece puzzle available at White Mountain Puzzles