DEMOGRAPHICS AND EMPLOYMENT IN THE UNITED STATES
In the wake of the Great Recession of 2009, there has been a good deal of focus on employment statistics, one of the most important metrics policymakers use to gauge the overall strength of the economy. In the United States, the government measures unemployment using the Current Population Survey (CPS), which collects demographic and employment information from a wide range of Americans each month. In this exercise, we will employ the topics reviewed in the lectures as well as a few new techniques using the September 2013 version of this rich, nationally representative dataset (available online).
The observations in the dataset represent people surveyed in the September 2013 CPS who actually completed a survey. While the full dataset has 385 variables, in this exercise we will use a more compact version of the dataset, CPSData.csv, which has the following variables:
PeopleInHousehold: The number of people in the interviewee’s household.
Region: The census region where the interviewee lives.
State: The state where the interviewee lives.
MetroAreaCode: A code that identifies the metropolitan area in which the interviewee lives (missing if the interviewee does not live in a metropolitan area). The mapping from codes to names of metropolitan areas is provided in the file MetroAreaCodes.csv.
Age: The age, in years, of the interviewee. 80 represents people aged 80-84, and 85 represents people aged 85 and higher.
Married: The marriage status of the interviewee.
Sex: The sex of the interviewee.
Education: The maximum level of education obtained by the interviewee.
Race: The race of the interviewee.
Hispanic: Whether the interviewee is of Hispanic ethnicity.
CountryOfBirthCode: A code identifying the country of birth of the interviewee. The mapping from codes to names of countries is provided in the file CountryCodes.csv.
Citizenship: The United States citizenship status of the interviewee.
EmploymentStatus: The status of employment of the interviewee.
Industry: The industry of employment of the interviewee (only available if they are employed).
Load the dataset from CPSData.csv into a data frame called CPS, and view the dataset with the summary() and str() commands.
How many interviewees are in the dataset?
> CPS <- read.csv(“CPSData.csv”)
Among the interviewees with a value reported for the Industry variable, what is the most common industry of employment? Please enter the name exactly how you see it.
Recall from the homework assignment “The Analytical Detective” that you can call the sort() function on the output of the table() function to obtain a sorted breakdown of a variable. For instance, sort(table(CPS$Region)) sorts the regions by the number of interviewees from that region.
Which state has the fewest interviewees?
Which state has the largest number of interviewees?
What proportion of interviewees are citizens of the United States?
The CPS differentiates between race (with possible values American Indian, Asian, Black, Pacific Islander, White, or Multiracial) and ethnicity. A number of interviewees are of Hispanic ethnicity, as captured by the Hispanic variable. For which races are there at least 250 interviewees in the CPS dataset of Hispanic ethnicity?
> hispanic <- subset(CPS, CPS$Hispanic==”1″)
Which variables have at least one interviewee with a missing (NA) value?
Often when evaluating a new dataset, we try to identify if there is a pattern in the missing values in the dataset. We will try to determine if there is a pattern in the missing values of the Married variable. The function is.na(CPS$Married) returns a vector of TRUE/FALSE values for whether the Married variable is missing. We can see the breakdown of whether Married is missing based on the reported value of the Region variable with the function table(CPS$Region, is.na(CPS$Married)). Which is the most accurate:
> table(CPS$Region, is.na(CPS$Married))
> table(CPS$Sex, is.na(CPS$Married))
> table(CPS$Age, is.na(CPS$Married))
> table(CPS$Citizenship, is.na(CPS$Married))
As mentioned in the variable descriptions, MetroAreaCode is missing if an interviewee does not live in a metropolitan area. Using the same technique as in the previous question, answer the following questions about people who live in non-metropolitan areas.
How many states had all interviewees living in a non-metropolitan area (aka they have a missing MetroAreaCode value)? For this question, treat the District of Columbia as a state (even though it is not technically a state).
> nonmetro <- subset(CPS, is.na(CPS$MetroAreaCode))
> summary(CPS$State)-summary(nonmetro$State) note the entire state should be nonmetro
How many states had all interviewees living in a metropolitan area? Again, treat the District of Columbia as a state.
> summary(nonmetro$State) note the entire state should be a metro
Which region of the United States has the largest proportion of interviewees living in a non-metropolitan area?
> sort(summary(nonmetro$Region)/summary(CPS$Region)) Note I am not comfortable in using table function when this is much more simpler way of doing things.
While we were able to use the table() command to compute the proportion of interviewees from each region not living in a metropolitan area, it was somewhat tedious (it involved manually computing the proportion for each region) and isn’t something you would want to do if there were a larger number of options. It turns out there is a less tedious way to compute the proportion of values that are TRUE. The mean() function, which takes the average of the values passed to it, will treat TRUE as 1 and FALSE as 0, meaning it returns the proportion of values that are true. For instance, mean(c(TRUE, FALSE, TRUE, TRUE)) returns 0.75. Knowing this, use tapply() with the mean function to answer the following questions:
Which state has a proportion of interviewees living in a non-metropolitan area closest to 30%?
Which state has the largest proportion of non-metropolitan interviewees, ignoring states where all interviewees were non-metropolitan?
> sort(summary(nonmetro$State)/summary(CPS$State)) Again I dont know why I dont conform to what is told by the instructors but I find my way a bit simpler. Beside it is just plain wrong to undermine the power of R as it is lot more versatile then what is given in the instruction. Anyway that is my excuse.
Codes like MetroAreaCode and CountryOfBirthCode are a compact way to encode factor variables with text as their possible values, and they are therefore quite common in survey datasets. In fact, all but one of the variables in this dataset were actually stored by a numeric code in the original CPS datafile.
When analyzing a variable stored by a numeric code, we will often want to convert it into the values the codes represent. To do this, we will use a dictionary, which maps the the code to the actual value of the variable. We have provided dictionaries MetroAreaCodes.csv and CountryCodes.csv, which respectively map MetroAreaCode and CountryOfBirthCode into their true values. Read these two dictionaries into data frames MetroAreaMap and CountryMap.
How many metropolitan areas are stored in MetroAreaMap?
> MetroAreaMap <- read.csv(“MetroAreaCodes.csv”)
> CountryMap <- read.csv(“CountryCodes.csv”)
How many countries are stored in CountryMap?
To merge in the metropolitan areas, we want to connect the field MetroAreaCode from the CPS data frame with the field Code in MetroAreaMap. The following command merges the two data frames on these columns, overwriting the CPS data frame with the result:
CPS = merge(CPS, MetroAreaMap, by.x=”MetroAreaCode”, by.y=”Code”, all.x=TRUE)
The first two arguments determine the data frames to be merged (they are called “x” and “y”, respectively, in the subsequent parameters to the merge function). by.x=”MetroAreaCode” means we’re matching on the MetroAreaCode variable from the “x” data frame (CPS), while by.y=”Code” means we’re matching on the Code variable from the “y” data frame (MetroAreaMap). Finally, all.x=TRUE means we want to keep all rows from the “x” data frame (CPS), even if some of the rows’ MetroAreaCode doesn’t match any codes in MetroAreaMap (for those familiar with database terminology, this parameter makes the operation a left outer join instead of an inner join).
Review the new version of the CPS data frame with the summary() and str() functions. What is the name of the variable that was added to the data frame by the merge() operation?
How many interviewees have a missing value for the new metropolitan area variable? Note that all of these interviewees would have been removed from the merged data frame if we did not include the all.x=TRUE parameter.
> CPS = merge(CPS, MetroAreaMap, by.x=”MetroAreaCode”, by.y=”Code”, all.x=TRUE)
> names(CPS) my preference as it takes out all the unwanted things.
Which of the following metropolitan areas has the largest number of interviewees?
Which metropolitan area has the highest proportion of interviewees of Hispanic ethnicity? Hint: Use tapply() with mean, as in the previous subproblem. Calling sort() on the output of tapply() could also be helpful here.
Remembering that CPS$Race == “Asian” returns a TRUE/FALSE vector of whether an interviewee is Asian, determine the number of metropolitan areas in the United States from which at least 20% of interviewees are Asian.
Normally, we would look at the sorted proportion of interviewees from each metropolitan area who have not received a high school diploma with the command:
sort(tapply(CPS$Education == “No high school diploma”, CPS$MetroArea, mean))
However, none of the interviewees aged 14 and younger have an education value reported, so the mean value is reported as NA for each metropolitan area. To get mean (and related functions, like sum) to ignore missing values, you can pass the parameter na.rm=TRUE. Passing na.rm=TRUE to the tapply function, determine which metropolitan area has the smallest proportion of interviewees who have received no high school diploma.
> sort(tapply(CPS$Education == “No high school diploma”, CPS$MetroArea, mean,na.rm=TRUE))
Just as we did with the metropolitan area information, merge in the country of birth information from the CountryMap data frame, replacing the CPS data frame with the result. If you accidentally overwrite CPS with the wrong values, remember that you can restore it by re-loading the data frame from CPSData.csv and then merging in the metropolitan area information using the command provided in the previous subproblem.
What is the name of the variable added to the CPS data frame by this merge operation?
> CPS = merge(CPS, CountryMap, by.x=”CountryOfBirthCode”, by.y=”Code”, all.x=TRUE)
How many interviewees have a missing value for the new country of birth variable?
Among all interviewees born outside of North America, which country was the most common place of birth?
> summary(CPS$Country) note it is outside of North America and not outside of USA
What proportion of the interviewees from the “New York-Northern New Jersey-Long Island, NY-NJ-PA” metropolitan area have a country of birth that is not the United States? For this computation, don’t include people from this metropolitan area who have a missing country of birth.
> NY <- subset(CPS, CPS$MetroArea==”New York-Northern New Jersey-Long Island, NY-NJ-PA”,na.rm=TRUE)
> NY <- subset(NY, NY$Country!=”NA’s”) in order to clean the data from NA’s
> NYNonUS <- subset(NY,NY$Country!=”United States”)
Which metropolitan area has the largest number (note — not proportion) of interviewees with a country of birth in India? Hint — remember to include na.rm=TRUE if you are using tapply() to answer this question.
> India <- subset(CPS, CPS$Country==”India”)
> Brazil <- subset(CPS, CPS$Country==”Brazil”)
> Somalia <- subset(CPS, CPS$Country==”Somalia”)
Phew! that was a long assignment for first week