Learning Objectives

Following this assignment students should be able to:

  • Describe the distribution of a variable
  • Summarize the distribution of categorical/discrete variables
  • Summarize the distribution of continuous variables
  • Call and use Stata’s stored results
  • Construct graphs of distributions of single variables
  • Construct theoretical distributions
  • Conduct a hypothesis test adn evaluate the results

Reading

Topics

  • The distribution of a variable
  • Summarizing the distribution
  • Plotting the distribution
  • Creating theoretical distributions
  • Testing hypotheses

Readings

Lecture Notes

  1. Understanding and Summarizing Distributions of Variables
  2. Graphing Distributions
  3. Creating Theoretical Distributions

Exercises

  1. Tabulate Data (10 pts)

    Load the Stata system dataset nlsw88.dta:

    * load national longitudinal survey of young women
        sysuse              nlsw88, clear
    

    1. Use tab to produce a frequency table for the variable race.

    1. What are the number of observations in each race category?
    2. Which race category is the mode (most common)?
    3. What is the percentage of women in each category?

    2. Use tab again, this time on the variable union, but include missing values in the table using the missing option:

    1. How many women are in a union, how many are not, and how many are missing?
    2. What percent of the sample has missing values for union?

  2. Summarize Data (10 pts)

    Using the nlsw88 data,

    1. Use sum to summarize the variable wage.
      1. What is the mean wage?
      2. What is the standard deviation?
      3. What are the minimum and maximum values?
    2. Use the detail option with sum.
      1. What is the median (50th percentile) wage?
      2. What are wages at the 25th and 75th percentile?
    3. Restrict the summary to women who are college graduates using if.
      1. Is the mean wage for college graduates higher or lower than the overall mean?
      2. Is the standard deviation for college graduates larger or smaller than in the full sample?

  3. Use Stored Values (10 pts)

    Using the nlsw88 data,

    1. Using Stata’s stored results, what is the range of hours?

    2. Using the stored percentiles, what is the interquartile range (IQR) of hours?


  4. Histograms (10 pts)

    Using the nlsw88 data,

    1. Draw a histogram of hours worked per week, with the percent of workers on the y-axis.

    2. Draw a histogram of hours with 10 bins.

    3. Draw a histogram of hours controlling the bin width directly starting the first bin at 0 and having bins of width 5.

    The variable grade records completed years of education and takes on integer values, meaning it is a discrete variable. In this case, Stata will make better looking historgrams if you tell Stata that the variable is discrete.

    Start by graphing grade using just the hist command.

    4. Draw a histogram of grade using the discrete option along with frequency.


  5. Density Functions (10 pts)

    Using the nlsw88 data,

    1. Draw a kernel density of total work experience.

    2. Draw a version with less smoothing.

    3. Draw a version with more smoothing.

    Stata can overlay a normal density curve with the same mean and variance as your data on top of the kernel density estimate. This lets you visually compare your distribution to a smooth, bell-shaped curve (we are not doing formal hypothesis tests here).

    4. Draw a kernel density of ttl_exp using the normal option.

    5. Finally, we will put it all together by drawing a histogram and a kernel density for ttl_exp on the same plot and make the graph presentation-quality.

    • First, choose reasonable binning (20) for ttl_exp and draw a histogram with percent on the y-axis:
    • Next create a combined graph with both the histogram and the kernel density. Use twoway to overlay them:
    • Modify the graph by adding the color(%60) option, which makes the bars lighter (60% of full opacity) so the density line stands out.
    • Change the title() of the graph to be “Distribution of total work experience”
    • Change the x-axis title to “Total work experience (years)” and the y-axis title to “Percent of workers”
    • Finally, create a legend and place it below the graph (pos) and in two columns (col). Label the shaded area using 1 "Histogram" and the line using 2 "Kernel density".

  6. Random Numbers (10 pts)

    Using the nlsw88 data,

    1. Simulate and then graph a theoretical log-normal distribution.

    • Start by setting the seed to the same seed that we set when we simulated a normal distribution for ttl_exp.
    • Next, create a variable called wage_norm that has a normal distribution with mean 1.831 and standard deviation of 0.6615.
    • To make a log-normal distribution, we need to exponentiate wage_norm using Stata’s built in math function exp(). Call this new variable wage_logn.
    • Finally, draw the distribution of this new variable using kdensity.

    2. Graph wage and the log-normal distribution on same graph.

    • Give the graph a descriptive title as well as labelling the x-axis and y-axis.
    • Create a legend and place it below the graph (pos) and in two columns (col).
    • Label the two lines as “Simulated log-normal” and “Observed wage”.

  7. Percentiles (10 pts)

    Using the nlsw88 data,

    1. Use sum, detail to get detailed summary statistics for wage. Store the value of the 5th and 95th percentile as locals called p5 and p95. Finally, tell stata to display two lines of text, followed by the actual value of the 5th and 95th using the values stored in p5 and p95:

    1. 5th percentile of wage:
    2. 95th percentile of wage:

    When we use kdensity we are drawing the probability density function (PDF). Sometimes it’s convenient to work with the cumulative distribution function (CDF) rather than the density. The CDF shows, for each wage value, the fraction of workers with wage less than or equal to that value.

    2. Use the cumul command to create the empirical CDF of wage: cumul wage, gen(F_wage). Then draw the CDF using the command twoway line. Note that this is our first time graphing more than one variable. You will need to include F_wage and then wage. You will also need to use the option sort to get the smooth CDF that we are looking for.

    • Title the graph “Cumulative distribution of wage”
    • Title the x-axis “Hourly wage (1988 dollars)”
    • Title the y-axis “Cumulative probability”

    3. Now we will add a line to the graph at 5% (0.05 on the y-axis) to mark the 5th percentile using yline. Recall in the lecture we added vertical lines using xline.

    • Give the line a dash pattern
    • Color the line maroon
    • Title this new graph “CDF of wage with 5% cutoff”

  8. Hypothesis Testing (20 pts)

    Using the nlsw88 data, this exercise asks you to apply the five-step hypothesis testing procedure from the Creating Theoretical Distributions lecture:

    Test whether women work 40 hours per week on average. The variable hours records usual hours worked per week.

    • Use sum to find the sample mean, standard deviation, and sample size.
    • As a comment in your do-file, write down the hypotheses for testing whether women work 40 hours per week on average:
      • $H_0: \mu = 40$
      • $H_1: \mu \neq 40$

    1. Use ttest to perform a one-sample t-test of this hypothesis.

    1. What is the sample mean of hours?
    2. What is the t-statistic?
    3. How many degrees of freedom are there?
    4. What is the two-sided p-value?
    5. At the 5% significance level, do you reject or fail to reject the null hypothesis that women work 40 hours per week on average?

    Now we will test a hypothesis about the mean hourly wage for college graduates only.

    • Use tab with and without the nolab option to determine which numerical value college grade takes.
    • Use sum with if to summarize the wages of just college graduates.
    • Suppose someone claims that college graduates in this sample earn $10 per hour on average. Set up the hypotheses:
      • $H_0: \mu = 10$
      • $H_1: \mu \neq 10$

    2. Use a one-sample t-test restricted to college graduates to test this claim.

    1. What is the sample mean of hours?
    2. What is the t-statistic?
    3. How many degrees of freedom are there?
    4. What is the two-sided p-value?
    5. At the 5% significance level, what is your decision about $H_0$?

  9. Check That Your Code Runs (10 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

  10. Challenge 4 (Challenge - 20 pts)

    In this challenge exercise you will recreate the “PDF of total work experience with tails” graph from lecture.

    You will:

    • Compute the kernel density of ttl_exp,
    • Shade the bottom 5% and top 5% of the distribution,
    • Mark the 5th percentile, mean, and 95th percentile with vertical lines,
    • Label the tails with “5%” and “95%”, and
    • Print the mean value below the x-axis.

    Work in a do-file, and include comments so you can remember what you did when you come back later.


    1. Load the data and get key summary stats

    1. Load the data:

       * load data
           sysuse          nlsw88, clear
      
    2. Get detailed summary statistics for ttl_exp:

       * get summary stats from ttl_exp
           quietly sum     ttl_exp, detail
      
    3. Store the mean, 5th percentile, and 95th percentile in locals:

       * store values as locals
           local           mu  = r(mean)
           local           p5  = r(p5)
           local           p95 = r(p95)
      

    Check in the Results window that these values look reasonable for total work experience.


    2. Compute the kernel density and define tail regions

    1. Ask Stata to compute the kernel density of ttl_exp and save the grid and density in new variables:

       * compute kernel density and save the grid + density
           kdensity        ttl_exp, gen(x_ttl d_ttl) nograph
      
      • x_ttl contains the x-values for the density grid.
      • d_ttl contains the estimated density at each x-value.
    2. Create variables for the left and right tails:

       * create left and right tail densities for shading
           gen             d_left  = d_ttl if x_ttl <= `p5'
           gen             d_right = d_ttl if x_ttl >= `p95'
      
      • d_left will be nonmissing only in the bottom 5% region.
      • d_right will be nonmissing only in the top 5% region.

    3. Find the cutoffs for shading and add a zero line

    1. Find the maximum x-value in the left tail and the minimum x-value in the right tail. These will be used for vertical lines:

       * create locals for right and left cutoff
           quietly sum     x_ttl if !missing(d_left), meanonly
           local           lcut = r(max)
      
           quietly sum     x_ttl if !missing(d_right), meanonly
           local           rcut = r(min)
      
    2. Create a variable of zeros for use with rarea (which shades between two curves):

       * a zero line for rarea (needed for shading)
           gen             zero = 0
      
    3. Create nicely formatted labels for the 5th percentile, mean, and 95th percentile if you want to use them later:

       * create nicely formatted labels for tick marks
           local           p5lbl  : display %5.2f `p5'
           local           mulbl  : display %5.2f `mu'
           local           p95lbl : display %5.2f `p95'
      

    4. Choose positions for the “5%” and “95%” labels

    You want the text “5%” and “95%” to appear inside the shaded tail regions.

    1. Compute the x-position for the center of the left tail and a y-position that is 60% of the maximum density in the left tail:

       * left tail center & height
           quietly sum     x_ttl if !missing(d_left), meanonly
           local           lx = (r(min) + r(max))/2
      
           quietly sum     d_ttl if !missing(d_left), meanonly
           local           ly = 0.6*r(max)
      
    2. Do the same for the right tail:

       * right tail center & height
           quietly sum     x_ttl if !missing(d_right), meanonly
           local           rx = (r(min) + r(max))/2
      
           quietly sum     d_ttl if !missing(d_right), meanonly
           local           ry = 0.6*r(max)
      

    These locals (lx, ly, rx, ry) will give Stata the coordinates for the text labels.


    5. Draw the final graph

    Now put everything together in a single twoway command. Type (or paste) the following into your do-file:

    * final graph
    twoway      (kdensity ttl_exp) ///
                    (rarea d_left  zero x_ttl, sort c(navy%60)) ///
                    (rarea d_right zero x_ttl, sort c(navy%60)), ///
                    xline(`lcut' `mu' `rcut', lp(dash) lc(maroon)) ///
                    xlabel(`mu' "`mulbl'", add) ///
                    text(`ly' `lx' "5%",  place(c)) ///
                    text(`ry' `rx' "95%", place(c)) ///
                    title("PDF of total work experience") ///
                    xtitle("Total work experience (years)") ///
                    ytitle("Density") ///
                    legend(off)
    

    This should produce a graph with:

    • The kernel density of ttl_exp in black,
    • The bottom 5% and top 5% shaded in navy,
    • Vertical dashed lines at the 5th percentile, mean, and 95th percentile,
    • The labels “5%” and “95%” inside the shaded regions, and
    • The numeric value of the mean printed below the x-axis at its location.

Assignment submission checklist

Solutions

  1. Tabulate Data
  2. Summarize Data
  3. Use Stored Values
  4. Histograms 1, 2, 3, 4
  5. Density Functions 1, 2, 3, 4, 5
  6. Random Numbers 1, 2
  7. Percentiles 1, 2, 3
  8. Hypothesis Testing
  9. Check That Your Code Runs
  10. Challenge 4