Learning Objectives

Following this assignment students should be able to:

  • Create a scatter plot of two variables
  • Write code to manipulate how the plot looks
  • Describe the relationship between two variables
  • Represent condition distributions and conditional means
  • Fit a line to data
  • Evaluate the relationship between two variables

Reading

Topics

  • Scatter plots
  • Beautifying plots
  • Conditional distributions
  • Line fitting

Readings

Lecture Notes

  1. Making and Beautifying Scatter Plots
  2. Conditional Distributions
  3. Line Fitting

Exercises

  1. Basic Scatter Plots (10 pts)

    Using the eth_allrounds_final data,

    1. Make a scatter plot of harvest value (harvest_value_USD) versus farm size (farm_size), with harvest value on the y-axis. Include informative labels and a title.

    2. Change the appearance of the markers in the graph to be tiny blue diamonds.


  2. Grouped Scatter Plots (20 pts)

    Using the eth_allrounds_final data,

    1. Make a scatter plot of harvest value versus farm size by irrigation status (irrigated) on a single scatter plot. Include informative labels and a title. Color irrigated plots navy and rainfed plots maroon

    2. Make a scatter plot of harvest value versus farm size by irrigation status use the by(irrigated) option on scatter to make separate panels for irrigated and rainfed plots.

    3. Which makes it easier to compare irrigated and rainfed plots? Which makes it easier to see the overall relationship between farm size and harvest value?


  3. Conditional Distributions (10 pts)

    Using the eth_allrounds_final data, explore how the distribution of an input varies across groups.

    Start by using sum, detail to summarize total_hired_labor_days separately for:

    • Plots with improved seed (improved == 1), and
    • Plots without improved seed (improved == 0).
    1. Is the average number of hired labor days greater when a plot is planted with improved seed or without improved seed?

    2. Draw two kernel density plots, one for each group, on the same graph using if conditions. Is this graph informative? Why or why not?


  4. Conditional Means (Discrete) (10 pts)

    Using the eth_allrounds_final data, look at mean outcomes by group for a discrete variable.

    1. Compute mean harvest quantity (harvest_kg) by plot ownership (plot_owned) using tabstat. Which group has the higher mean yield?

    2. Make a graph bar of the mean harvest value (harvest_value_USD) by crop tpye (main_crop). Which crop has the highest average value and what is that value?


  5. Conditional Means (Continuous) (10 pts)

    Using the eth_allrounds_final data, now consider how an outcome varies with a continuous household-level variable by binning it.

    1. Use xtile to create quartiles (asset_q) of household asset index (hh_asset_index). Label the variable as “Asset Quartiles”. For each asset quartile, use bys and sum to compute the mean harvest value (harvest_value_USD). Which quartile has the higest mean value of harvest?

    2. Make a bar graph using hbar showing the mean harvest value (harvest_value_USD) over both asset quartile (asset_q) and over if the household experienced a shock to their crops (crop_shock).

      1. Add an informative title and axis label.
      2. Tell Stata to treat the categories as if the first category are variables on y-axis (asyvar). This will allow us to change the color of each bar.
      3. Set the color and intensity of bar 1 as navy*3
      4. Color bar 2 forest_green, bar 3 sienna, and bar 4 maroon
      5. Place the legend on the bottom of the graph (pos) in four column (col)
      6. Label group 1 “lowest”, group 2 as “Lower Middle”, group 3 as “Upper Middle”, and group 4 as “Highest”

  6. Fitted Lines (10 pts)

    Using the eth_allrounds_final data, add a fitted line and confidence interval to a scatter plot, and adjust the appearance.

    1. Make a scatter plot with yield (yield_kg) on the y-axis and inorganic fertilizer (inorganic_fertilizer_value_USD) on the x-axis, restricting observations to only maize (crop == 3).
      • Add a fitted line with a confidence band using lfitci in a twoway graph.
      • Place the legend on the bottom (pos) in three columns (col).
      • Use very small, hollow circular markers for the points.
      • Change the color of the line to maroon and the pattern of the line to be solid (use lcolor and lpattern).
      • Make the color of confidence band gray and partially transparent (gray%50) so the points and line remain visible (use fcolor).
      • Label the x-axis “Inorganic Fertilizer (USD)” and the y-axis as “Maize Yield (kg)”
    2. What is the relationship between yield and fertilizer use? Positive? Negative? No relationship?

  7. Fitted Lines by Group (10 pts)

    Using the eth_allrounds_final data, connect the fitted line on a scatter plot to the underlying regression.

    1. Create a scatter plot with harvest value (harvest_value_USD) on the y-axis and farm size (farm_size) on the y-axis.

    • Add a fitted line with a confidence band using lfitci in a twoway graph.
    • Place the legend on the bottom (pos) in three columns (col).
    • Use very small, hollow circular markers for the points.
    • Change the color of the line to maroon and the pattern of the line to be solid.
    • Make the color of confidence band gray and partially transparent (gray%50) so the points and line remain visible.
    • Make the color of the confidence interval lines marron and partially transparent (maroon%25) so the points and line remain visible (use alcolor).
    • Label the x-axis “Inorganic Fertilizer (USD)” and the y-axis as “Maize Yield (kg)”

    2. Run the corresponding regression:

       regress        harvest_value_USD farm_size
    

    1. What does a one-unit increase in farm size represent?
    2. How much does harvest value change, on average, for a one-unit increase in farm size ? 3. Is the relationship between harvest value and farm size statistically significant?

    3. Now let the relationship differ by whether the household is using traditional seed (improved == 0) or improved seed (improved == 1). The easiest way to do this is copy the code that makes the graph in problem 1 and then edit it as follows:

    • Change the scatter plot to display only traditional seed and change the marker color to be maroon%50.
    • Add a new scatter plot that displays only improved seed and change the marker color to be navy%50.
    • Change the fitted line so that it only fits to observations of traditional seeds.
    • Add a new fitted line that fits to observations of improved seed.
    • Using lcolor, change the color of the fitted line so that it matches the color of the corresponding markers (so traditional is maroon and improved is navy).
    • Using alcolor, change the color and transparency of the confidence interval lines so that the correspond to the markers and are transparent at %25.
    • Change the legend so it is in two columns. Use order to restrict the legend to only printing the third and fifth item in the legend and label them as 3 "Traditional Seeds" and 5 "Improved Seeds"

    4. Run a regression that allows interacts farm size with improved see:

       regress        harvest_value_USD c.farm_size#i.improved
    

    1. How much does harvest value change, on average, for a one-unit increase in farm size when traditional seed is used? 2. How much does harvest value change, on average, for a one-unit increase in farm size when improved seed is used? 3. Are these relationships between harvest value and farm size-seed type statistically significant?


  8. Check That Your Code Runs (20 pts)

    Sometimes you think you’re code runs, but it only actually works because of something else you did previously. To make sure it actually runs you should save your work and then run it in a clean environment.

    Follow these steps in to make sure your code really runs:

    1. Restart Stata by closing the instance of Stata that you have open.

    2. When you reopen Stata, type in the command line clear all. Or, better yet, put clear all at the top of your .do file, after the preamble but before any commands.

    3. Rerun your entire homework assignment by clicking the Execute button to make sure it runs from start to finish and produces the expected results.

    4. Make sure that you saved your code with the name of the assignment number (i.e., assignment_01). You should see the file in your git repo on your machine.

    5. Make sure that your code will run on other computers (relevant for all assignments after the first assignment)

      • No cd. Use a project.do file instead to set directories
      • Use only relative paths in files other than the project.do file
      • Use / not \ for paths

  9. Challenge 5 (10 pts)

    For this challenge we are going to switch up the data and use a data set of women’s labor force participation and earnings from 1975. The data comes from the paper The Sensitivity of an Empirical Model of Married Women’s Hours of Work to Economic and Statistical Assumptions by Thomas A. Mroz.

    Download the Mroz.dta file from the list of data sets on the course website. Load the data using use.

    We are going to analysis women’s income conditional a number of different variables. Since we want to understand how women’s earnings varies based on these other variables, start by using keep if to drop from the data set any woman who does not participate in the labor force.

    In the data set, women’s earnings have been logged (lwg). We want the unlogged version so create a new variable called earn that is the unlogged lwg. Recall to undo a log we exponentiate the value (exp()) using e. Also, use drop if to drop any values of income (inc) that are less than 0.

    1. Draw a scatter plot with earn on the x-axis and inc on the y-axis. Because of the long tail in data like inc we want to “normalize” the data a bit by graphing both variables on a log scale. Use the graphing option yscale(log) and xscale(log).

    2. Use tabstat to get mean (stat(mean)) earnings conditional on (by) college attendance (wc). Are average earnings higher if the wife attended college or did not attend college?

    3. Now, use egen with cut to create 10 groups (deciles) based on family income inc. Use tabstat to get mean (stat(mean)) earnings conditional on (by) being in one of the 10 deciles (inc_cut). Do women in households that are in the highest decile (group 10) by family income earn the most money?

    4. Create a new variable called loginc that is the logged value (log()) of family income (inc).

    • Make a scatter plot with log family income (log_inc) on the y-axis and log women’s earnings (lwg) on the x-axis.
    • Change the marker color to gray and make it 50% transparent (gray%50).
    • To this scatter plot at a fitted line using lfit.
    • Change the color of this line to navy, change the pattern to solid and change the width of the line to thick.
    • Also add a fitted line using lowess. This fits a locally weighted non-linear regression line to the data.
    • Change the color of this line to maroon, change the pattern to solid and change the width of the line to thick.

    5. Run two regressions (regress). For the first one, regress the log of women’s earnings on the log of family income. For the second one add college attendance (wc) as a control. Is the unconditional relationship between family income and women’s earnings stronger then that relationship when we condition on college attendance (which regression has the larger co-efficient)?


Assignment submission checklist

Solutions

  1. Basic Scatter Plots 1, 2
  2. Grouped Scatter Plots 1, 2, 3,
  3. Conditional Distributions
  4. Conditional Means (Discrete)
  5. Conditional Means (Continuous) 1, 2
  6. Fitted Lines 1, 2
  7. Fitted Lines by Group 1, 2, 3
  8. Check That Your Code Runs
  9. Challenge 5 1, 2, 3,