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 laterBy the end of this session, participants will be able to:
tabstat, for all women and for groups.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 / 100The 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_lblagegrp 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.The type of variable decides which table or graph is suitable.
| Type of variable | Example | Table | Graph |
|---|---|---|---|
| Categorical | Residence, education, division | tabulate | graph bar |
| Numeric | Age, years of education, BMI | tabstat | histogram |
| Two numeric variables | Age and BMI | twoway 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.
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, rowcolumn makes each column add to 100, row makes each row add to 100.column, 22.4 percent of urban women and 10.6 percent of rural women have higher education.row, 71.7 percent of women with no education live in rural areas.parity by residence with column percentages. Which group has the larger share of women with three or more children?agegrp by education with row percentages.tabulate parity residence, column
tabulate agegrp education, rowAmong 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.
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)statistics() lists the statistics wanted, such as mean, sd, median, min, max and count.by() gives one row for each group and one for the total.child_number for each education level, using tabstat.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.
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_yearsbmi.histogram bmiThe shape is close to symmetric, with a longer tail to the right. Most women have a BMI between 20 and 27.
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.graph bar (percent), over(division)Dhaka has the tallest bar, 15.1 percent. Barishal and Sylhet are the smallest, both about 10.7 percent.
A scatter plot shows two numeric variables together. Each point is one woman.
twoway scatter bmi agebmi against educ_years.twoway scatter bmi educ_yearsThe points form vertical lines because years of education take whole numbers. The upward trend is slight.
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 agetabulate with the option column or row to compare groups with percentages.tabstat for a table of chosen statistics, and by() to repeat it for each group.histogram for one numeric variable, graph bar for categories and twoway scatter for two numeric variables.| Command | Use |
|---|---|
tabulate, column, row | Two way tables with percentages |
tabstat | Table of chosen statistics, by group |
histogram | Distribution of a numeric variable |
graph bar | Bars of counts or percentages for categories |
twoway scatter | Scatter plot of two numeric variables |
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.
marital_status by residence with column percentages.tabulate marital_status residence, columnAbout 94.3 percent of urban women and 95.2 percent of rural women are currently married, so the two groups are very similar.
bmi for each education level, using tabstat.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.
bmi. Describe its shape.histogram bmiThe histogram is close to symmetric with a slightly longer right tail. The mean BMI is about 23.8.
graph bar (percent), over(marital_status)One bar is far taller than the others: 94.9 percent of the women are currently married.
The next session covers statistical inference: confidence intervals, comparing groups with the t test, tests of association and linear regression.