All sessions
1 / 1
Session 3 of 5

Descriptive Statistics and Graphs

Summarising categorical and numeric variables with tables, and drawing histograms, bar graphs and scatter plots, using the BDHS 2022 women's file.

120 minutesLab based exerciseStata 15 or later

Learning objectives

By the end of this session, participants will be able to:

  • Choose a suitable table or graph for a categorical or a numeric variable.
  • Produce two way tables with column and row percentages.
  • Build a table of chosen statistics with tabstat, for all women and for groups.
  • Draw histograms, bar graphs and scatter plots.
  • Read a table or a graph and describe what it shows.
Preparation

Starting the session

Begin with the same lines as before, then repeat the preparation from Session 2. Change the folder in cd to your own.

clear all
set maxvar 10000
cd "C:\StataCourse"
use "BDIR81FL.DTA", clear

keep v012 v024 v025 v106 v133 v201 v445 v501

rename v012 age
rename v024 division
rename v025 residence
rename v106 education
rename v133 educ_years
rename v201 child_number
rename v445 bmi100
rename v501 marital_status

generate bmi = bmi100 / 100
Preparation

Group variables from Session 2

The age group and the parity group are created again with recode and labelled. Run this part once.

recode age (15/19 = 1) (20/24 = 2) (25/29 = 3) (30/34 = 4) (35/39 = 5) (40/44 = 6) (45/49 = 7), gen(agegrp)
recode child_number (0 = 0) (1/2 = 1) (3/max = 2), gen(parity)

label variable agegrp "Age group of respondent"
label variable parity "Parity group"
label define parity_lbl 0 "No child" 1 "One or two" 2 "Three or more"
label values parity parity_lbl
  • agegrp has seven groups of five years, from 15 to 19 up to 45 to 49.
  • parity separates women with no child, one or two children, and three or more.
Choosing a summary

Which summary for which variable

The type of variable decides which table or graph is suitable.

Type of variableExampleTableGraph
CategoricalResidence, education, divisiontabulategraph bar
NumericAge, years of education, BMItabstathistogram
Two numeric variablesAge and BMItwoway scatter

The mean and standard deviation of a code such as education 0 to 3 have no useful meaning. Use a frequency table for categories.

Categorical variables

Two way tables

A two way table cross classifies two categorical variables. Percentages are requested with an option.

tabulate education residence
tabulate education residence, column
tabulate education residence, row
  • column makes each column add to 100, row makes each row add to 100.
  • With column, 22.4 percent of urban women and 10.6 percent of rural women have higher education.
  • With row, 71.7 percent of women with no education live in rural areas.

Practice

  1. Tabulate parity by residence with column percentages. Which group has the larger share of women with three or more children?
  2. Tabulate agegrp by education with row percentages.
Show solution
tabulate parity residence, column
tabulate agegrp education, row

Among rural women, 37.4 percent have three or more children, against 29.6 percent among urban women. In the second table, the share with no education rises from 1.6 percent at ages 15 to 19 to 40.2 percent at ages 45 to 49.

Tables of statistics

The tabstat command

tabstat builds a compact table of chosen statistics for several variables, optionally by group.

tabstat age child_number educ_years, statistics(mean sd min max)
tabstat age child_number educ_years, by(residence) statistics(mean min max)
  • The option statistics() lists the statistics wanted, such as mean, sd, median, min, max and count.
  • The option by() gives one row for each group and one for the total.
  • For all women, mean age is about 32.0 years, mean years of education 6.8 and mean children ever born 2.15.
  • Rural women have fewer years of education, 6.3 against 7.8, and more children, 2.23 against 2.00.

Practice

Show the mean, minimum and maximum of child_number for each education level, using tabstat.
Show solution
tabstat child_number, by(education) statistics(mean min max)

The mean falls from 3.17 children among women with no education to 1.34 among women with higher education.

Graphs

Histograms

A histogram shows the distribution of one numeric variable. The height of each bar shows how common the values in an interval are.

histogram age
histogram educ_years
  • The age histogram is spread across 15 to 49 years, with fewer women in the oldest groups.
  • The first bar of years of education is high, because 4,284 women, 14 percent, have no schooling.
  • Look at the centre, the spread and the shape of each histogram.

Practice

Draw a histogram of bmi.
Show solution
histogram bmi

The shape is close to symmetric, with a longer tail to the right. Most women have a BMI between 20 and 27.

Graphs

Bar graphs of a categorical variable

graph bar draws one bar for each category. The statistic is written in brackets, and over() names the categorical variable.

* Number of women in each category
graph bar (count), over(residence)
graph bar (count), over(education)

* Percentage of women in each category
graph bar (percent), over(education)
  • (count) shows the number of women and (percent) shows the share of all women.
  • Secondary education is the tallest bar, with 13,613 women, 45.3 percent.

Practice

Draw a bar graph of the percentage of women in each division.
Show solution
graph bar (percent), over(division)

Dhaka has the tallest bar, 15.1 percent. Barishal and Sylhet are the smallest, both about 10.7 percent.

Graphs

Scatter plots

A scatter plot shows two numeric variables together. Each point is one woman.

twoway scatter bmi age
  • The first variable goes on the vertical axis and the second on the horizontal axis.
  • BMI rises slightly with age, but the points are widely spread, so the relationship is weak.
  • Many points overlap, so dense areas look solid.

Practice

Draw a scatter plot of bmi against educ_years.
Show solution
twoway scatter bmi educ_years

The points form vertical lines because years of education take whole numbers. The upward trend is slight.

Practice

A descriptive statistics do file

The commands of the session combine into one do file that can be run again at any time.

* Session 3: descriptive statistics and graphs
* (start and prepare the variables as on the earlier slides)

* Tables
tabulate education residence, column
tabstat age child_number educ_years, by(residence) statistics(mean min max)

* Graphs
histogram age
graph bar (count), over(education)
graph bar (percent), over(education)
twoway scatter bmi age
Summary

Key points: descriptive statistics and graphs

  • Choose the summary by the type of variable: frequencies for categories, means and spread for numbers.
  • Use tabulate with the option column or row to compare groups with percentages.
  • Use tabstat for a table of chosen statistics, and by() to repeat it for each group.
  • Use histogram for one numeric variable, graph bar for categories and twoway scatter for two numeric variables.
Summary

Commands: descriptive statistics and graphs

CommandUse
tabulate, column, rowTwo way tables with percentages
tabstatTable of chosen statistics, by group
histogramDistribution of a numeric variable
graph barBars of counts or percentages for categories
twoway scatterScatter plot of two numeric variables
Exercise

Practical exercises

This part has 4 tasks that bring the commands of the session together. Each topic also has a practice block in the Slides part. Write the commands in a do file, run them, and open the answer after each attempt to compare.

Exercise 1 of 4

Marital status by residence

Tabulate marital_status by residence with column percentages.
Show answer
tabulate marital_status residence, column

About 94.3 percent of urban women and 95.2 percent of rural women are currently married, so the two groups are very similar.

Exercise 2 of 4

BMI by education

Show the mean of bmi for each education level, using tabstat.
Show answer
tabstat bmi, by(education) statistics(mean)

The mean BMI rises with education, from about 23.0 among women with no education to 24.7 among women with higher education.

Exercise 3 of 4

The distribution of BMI

Draw a histogram of bmi. Describe its shape.
Show answer
histogram bmi

The histogram is close to symmetric with a slightly longer right tail. The mean BMI is about 23.8.

Exercise 4 of 4

Marital status bar graph

Draw a bar graph of the percentage of women in each marital status category.
Show answer
graph bar (percent), over(marital_status)

One bar is far taller than the others: 94.9 percent of the women are currently married.

Session 3 complete

Next: Session 4

The next session covers statistical inference: confidence intervals, comparing groups with the t test, tests of association and linear regression.

Continue to Session 4 or return to all sessions.

Use the left and right arrow keys. Press F for full screen.