0.1 Objectives

  • Get familiar with R-studio and RMarkdown
  • Hands-on R
  • Learn data science essentials
    • gather data
    • clean data
    • summarize data
    • display data
    • conclusion
  • Packages
    • dplyr
    • ggplot

0.2 Instructions

  • All work submitted should be completed in the R Markdown format. You can find a cheat sheet for R Markdown here For those who have never used it before, we urge you to start this homework as soon as possible.

  • Submit the following files, one submission for each group: (1) Rmd file, (2) a compiled HTML or pdf version, and (3) all necessary data files if different from our source data. You may directly edit this .rmd file to add your answers. If you intend to work on the problems separately within your group, compile your answers into one Rmd file before submitting. We encourage that you at least attempt each problem by yourself before working with your teammates. Additionally, ensure that you can ‘knit’ or compile your Rmd file. It is also likely that you need to configure Rstudio to properly convert files to PDF. These instructions might be helpful.

  • In general, be as concise as possible while giving a fully complete answer to each question.make sure to document your code with comments (written on separate lines in a code chunk using a hashtag # before the comment) so the teaching fellows can follow along. R Markdown is particularly useful because it follows a ‘stream of consciousness’ approach: as you write code in a code chunk, make sure to explain what you are doing outside of the chunk.

0.3 Review materials

  • Study Basic R Tutorial
  • Study Advanced R Tutorial (to include dplyr and ggplot)
  • Study lecture 1: Data Acquisition and EDA

1 Case study 1: Audience Size

How successful is the Wharton Talk Show Business Radio Powered by the Wharton School

Background: Have you ever listened to SiriusXM? Do you know there is a Talk Show run by Wharton professors in Sirius Radio? Wharton launched a talk show called Business Radio Powered by the Wharton School through the Sirius Radio station in January of 2014. Within a short period of time the general reaction seemed to be overwhelmingly positive. To find out the audience size for the show, we designed a survey and collected a data set via MTURK in May of 2014. Our goal was to estimate the audience size. There were 51.6 million Sirius Radio listeners then. One approach is to estimate the proportion of the Wharton listeners to that of the Sirius listeners, \(p\), so that we will come up with an audience size estimate of approximately 51.6 million times \(p\).

To do so, we launched a survey via Amazon Mechanical Turk (MTurk) on May 24, 2014 at an offered price of $0.10 for each answered survey. We set it to be run for 6 days with a target maximum sample size of 2000 as our goal. Most of the observations came in within the first two days. The main questions of interest are “Have you ever listened to Sirius Radio” and “Have you ever listened to Sirius Business Radio by Wharton?”. A few demographic features used as control variables were also collected; these include Gender, Age and Household Income.

We requested that only people in United States answer the questions. Each person can only fill in the questionnaire once to avoid duplicates. Aside from these restrictions, we opened the survey to everyone in MTurk with a hope that the sample would be more randomly chosen.

1.1 Data preparation

  1. We need to clean and select only the variables of interest. Select only the variables Age, Gender, Education Level, Household Income in 2013, Sirius Listener?, Wharton Listener? and Time used to finish the survey.
#Load in data file using read.csv function.
data <- read.csv("data/Survey_results_final.csv", header=T, stringsAsFactors = FALSE)  
#Select variables of interest and put them into a new data frame called 'radio'.
radio <- select(data, Answer.Age, Answer.Gender, Answer.Education, Answer.HouseHoldIncome, Answer.Sirius.Radio, Answer.Wharton.Radio, WorkTimeInSeconds)

Change the variable names to be “age”, “gender”, “education”, “income”, “sirius”, “wharton”, “worktime”.

#Rename the columns.
radio <- radio %>% rename(age = Answer.Age, gender = Answer.Gender, education = Answer.Education, income= Answer.HouseHoldIncome, sirius = Answer.Sirius.Radio, wharton = Answer.Wharton.Radio, worktime = WorkTimeInSeconds)
#Look at the head of the data frame to confirm column names changed correctly.
head(radio)
  1. Handle missing/wrongly filled values of the selected variables

It is always important to clean data before analysis, and in this case it is especially important considering our data comes from a 3rd party source and we did not directly collect it or gather the survey participants. Data can be wrong or incomplete when participants are (1) able to type in a response instead of choosing from a predetermined list, (2) allowed to move through the survey without answering certain questions, (3) not completing the survey, (4) do not understand what a question is asking or what it is referring to, etc. Additionally, the formatting of the survey can be incorrect leading to unexpected responses or even out of order responses. In order to address these potential issues we have to take a look at our data and find values that stand-out from the rest or are missing. Specifically, we use the unique function to look at the unique responses for each category of interest, which allows us to see if responses are missing (” “) as well as if responses are unreasonable or formatted non-uniformly (i.e. respondents saying their age is 4, 223, ‘Eighteen (18)’, and ‘female’). For any respondents who did not answer a question (showing as” ” or “select one”), we remove them from our sample. For the age category we fixed some values where the age was clear but not formatted uniformly, but for responses that were unreasonable (age of 4 and 223) or unclear (‘female’), we remove those respondents from the sample. We choose to fix the poorly formatted age values because it was clear what the respondents meant, but we remove the other responses because there aren’t obvious corrections. For example, the person who put ‘4’ as an age might have meant 40, 44, 45, etc., there is no way for us to know. Another important consideration when cleaning data and especially in removing respondents who did not answer all questions is that you don’t lose too much of your sample and that the removal of some responses does not create bias in the sample left over. We only lose about 2.1% of our sample from our cleaning (37 responses), so we are confident that it did not create any bias in the data set left over, and the sample size left is still sufficiently large (1727 responses). At this point we also reformat the age variable to the integer type to help later on when we look at its statistics.

  1. Brief summary

After cleaning the data, we are left with a sample size of 1,727 Amazon mturk respondents. The age range of these respondents is from 18 to 76 years old with an average age of about 30 years old and 75% of respondents being at most 34 years old. The education level of respondents ranges from those who did not complete highschool to those with graduate or professional degrees. The majority of respondents have either some college / an Associate’s degree - or- have completed their Bachelor’s degree. Respondents report incomes ranging from below $15K to above $150K. The distribution of incomes is pretty uniform across $15K to $75K, but the most frequently reported income is between $30-50K. About 42% of respondents reported being Female and about 58% reported being male. Of all the respondents about 77% reported that they listen to Sirius XM Radio and only 4% reported they listen to the Wharton talk show. The time taken to complete the survey by respondents ranged from 8 to 108 seconds and on average was 22.5 seconds. Based on the given characteristics the majority of respondents seem to be young adults, potentially still in college and doing work at mturk for extra money or to pay for school, or those recently graduated in entry level positions that need a side gig. The gender difference is pretty evenly split indicating both genders have equal exposure to this work opportunity. The speed at which most responders filled out the survey might indicate why this work would be motivating, if a respondent can do about 3 surveys a minute that is 30 cents/minute and $18 per hour. This pay is likely motivating for the average respondent considering the majority of respondents make about $14 - $24 an hour based on full-time work on the reported incomes. Additionally, there may be other surveys or work activites through Amazon mturk that provide even better pay rates.

1.2 Sample properties

The population from which the sample is drawn determines where the results of our analysis can be applied or generalized. We include some basic demographic information for the purpose of identifying sample bias, if any exists. Combine our data and the general population distribution in age, gender and income to try to characterize our sample on hand.

  1. Does this sample appear to be a random sample from the general population of the USA? Why it is crucial to have randomness here?

  2. Does this sample appear to be a random sample from the MTURK population?

Note: You can not provide evidence by simply looking at our data here. For example, you need to find distribution of education in our age group in US to see if the two groups match in distribution. You may need to gather some background information about the MTURK population to have a slight sense if this particular sample seem to a random sample from there… Please do not spend too much time gathering evidence.

1.3 Final estimate

Give a final estimate of the Wharton audience size by May of 2014. Assume that the sample is a random sample of the MTURK population, and that the proportion of Wharton listeners vs. Sirius listeners in the general population is the same as that in the MTURK population. Write a brief executive summary to summarize your findings and how you came to that conclusion.

To be specific, you should include:

  1. Goal of the study
  2. Method used: data gathering, estimation methods
  3. Findings
  4. Limitations of the study.

1.4 New task

Now suppose you are asked to design a study to estimate the audience size of Wharton Business Radio Show as of today: You are given a budget of $1000. You need to present your findings in two months.

Write a proposal for this study which includes:

  1. Method proposed to estimate the audience size.
  2. What data should be collected and where it should be sourced from. (Can we use ChatGPT to get us a rough estimate?)

Please fill in the google form to list your platform where surveys will be launched and collected HERE

A good proposal will give an accurate estimation with the least amount of money used.

2 Case study 2: Women in Science

Are women underrepresented in science in general? How does gender relate to the type of educational degree pursued? Does the number of higher degrees increase over the years? In an attempt to answer these questions, we assembled a data set (WomenData_06_16.xlsx) from NSF about various degrees granted in the U.S. from 2006 to 2016. It contains the following variables: Field (Non-science-engineering (Non-S&E) and sciences (Computer sciences, Mathematics and statistics, etc.)), Degree (BS, MS, PhD), Sex (M, F), Number of degrees granted, and Year.

Our goal is to answer the above questions only through EDA (Exploratory Data Analyses) without formal testing. We have provided sample R-codes in the appendix to help you if needed.

2.1 Data preparation

  1. Understand and clean the data

Notice the data came in as an Excel file. We need to use the package readxl and the function read_excel() to read the data WomenData_06_16.xlsx into R. a). Read the data into R.

b). Clean the names of each variables. (Change variable names to Field,Degree, Sex, Year and Number )

c). Set the variable natures properly.

d). Any missing values?

  1. Write a summary describing the data set provided here.

a). How many fields are there in this data? There are 10 fields represented in this data, including agricultural sciences, computer sciences, mathematics/statistics, psychology, engineering, biological sciences, earth/atmosphere/ocean sciences, physical sciences, social sciences, and lastly a general category of non-S&E.

b). What are the degree types? The degree types are BS, MS, and PhD.

c). How many year’s statistics are being reported here? The data set reports statistics from 2006 to 2016 inclusive.

2.2 BS degrees in 2015

Is there evidence that more males are in science-related fields vs Non-S&E? Provide summary statistics and a plot which shows the number of people by gender and by field. Write a brief summary to describe your findings.

## `summarise()` has grouped output by 'SE'. You can override using the `.groups`
## argument.

## `summarise()` has grouped output by 'SE'. You can override using the `.groups`
## argument.

2.3 EDA bringing type of degree, field and gender in 2015

Describe the number of people by type of degree, field, and gender. Do you see any evidence of gender effects over different types of degrees? Again, provide graphs to summarize your findings.

## `summarise()` has grouped output by 'Field'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'Field'. You can override using the
## `.groups` argument.

2.4 EDA bring all variables

In this last portion of the EDA, we ask you to provide evidence numerically and graphically: Do the number of degrees change by gender, field, and time?

2.5 Women in Data Science

Finally, is there evidence showing that women are underrepresented in data science? Data science is an interdisciplinary field of computer science, math, and statistics. You may include year and/or degree.

2.6 Final brief report

Summarize your findings focusing on answering the questions regarding if we see consistent patterns that more males pursue science-related fields. Any concerns with the data set? How could we improve on the study?

2.7 Appendix

To help out, we have included some R-codes here as references. You should make your own chunks filled with texts going through each items listed above. Make sure to hide the unnecessary outputs/code etc.

  1. Clean data

  2. A number of sample analyses

3 Case study 3: Major League Baseball

We would like to explore how payroll affects performance among Major League Baseball teams. The data is prepared in two formats record payroll, winning numbers/percentage by team from 1998 to 2014.

Here are the datasets:

-MLPayData_Total.csv: wide format -baseball.csv: long format

Feel free to use either dataset to address the problems.

3.1 EDA: Relationship between payroll changes and performance

Payroll may relate to performance among ML Baseball teams. One possible argument is that what affects this year’s performance is not this year’s payroll, but the amount that payroll increased from last year. Let us look into this through EDA.

Create increment in payroll

a). To describe the increment of payroll in each year there are several possible approaches. Take 2013 as an example:

- option 1: diff: payroll_2013 - payroll_2012
- option 2: log diff: log(payroll_2013) - log(payroll_2012)

Explain why the log difference is more appropriate in this setup.

b). Create a new variable diff_log=log(payroll_2013) - log(payroll_2012). Hint: use dplyr::lag() function.

c). Create a long data table including: team, year, diff_log, win_pct

3.2 Exploratory questions

a). Which five teams had highest increase in their payroll between years 2010 and 2014, inclusive?

b). Between 2010 and 2014, inclusive, which team(s) “improved” the most? That is, had the biggest percentage gain in wins?

3.3 Do log increases in payroll imply better performance?

Is there evidence to support the hypothesis that higher increases in payroll on the log scale lead to increased performance?

Pick up a few statistics, accompanied with some data visualization, to support your answer.

3.4 Comparison

Which set of factors are better explaining performance? Yearly payroll or yearly increase in payroll? What criterion is being used?