1 WEEK 9: DATA ANALYSIS (STAGE 1)
These notes accompany the Week 9 lecture, Data analysis: Stage 1, and follow the slides topic
by topic. Every worked example in this document, every table, and every Stata command uses
one real dataset: bangladesh_adolescent_social_media_mental_health.dta, a survey of 500
Bangladeshi adolescents on social media use and mental health. You will receive this dataset
and its codebook alongside these notes, so every number here is one you can reproduce
yourself by running the same commands on the same file.
How to use this document: The examples throughout this document use Stata, but the
goal is not to learn Stata itself. Instead, focus on understanding the logic of the analyses, the
structure of the data, and why each analytical step is being performed. These principles are
transferable to any statistical software.
You do not need access to Stata to benefit from this material. A separate course focused on
conducting analyses in R will be offered in the future.
For now, concentrate on developing a solid understanding of data analysis concepts and
decision making rather than software-specific commands.
1.1 The data analysis workflow
Follow these steps every time you start analysing a new dataset, before you touch a single
statistical test.
● Load the data and check its structure
● Identify variable types (continuous vs. categorical)
● Check for missing values across all key variables
● For continuous variables: check distribution, then summarise and visualise
● For categorical variables: produce frequency tables with percentages, check missing,
visualise with bar charts
● Compile Table 1 for the manuscript
Remember: this whole workflow is descriptive. It tells you what your data looks like.
Whether a difference you notice along the way is statistically meaningful is Stage 2 work,
covered later in the course.
1.2 Understanding variables in a dataset
A variable is any characteristic that can take different values across people, objects, or
situations. In a dataset, a variable is one column. Each row holds one respondent's value for it.
1.3 Focus on these four kinds of variables
The lecture asks you to focus on four variable types. Getting the type right determines
everything that follows: how you summarise it, how you visualise it, and which Stata command
is correct.
Variable type What it means Example from this dataset
Binary (2 categories) Takes exactly two values, usually mental_health_problem (0 = no, 1
0/1 = yes)
Categorical (more than 2 Takes more than two category school_type (1 = public, 2 = private,
categories) labels 3 = English medium, 4 = madrasa)
Continuous, normally Numeric, roughly symmetric self_esteem_score (skewness 0.02,
distributed distribution almost perfectly symmetric)
Continuous, not normally Numeric, skewed or long-tailed daily_social_media_hours (heavily
distributed distribution right-skewed)
Worked example: self_esteem_score and daily_social_media_hours are both continuous,
measured in the same dataset, yet one behaves and the other does not. Create a histogram
for self_esteem_score and daily_social_media_hours, and compare each.
1.4 Univariate analysis
1.4.1 What is univariate analysis?
Uni means one. Univariate analysis means examining one variable at a time. It is the first thing
you do when you open a dataset, before any comparisons or statistical tests. Think of it as
getting to know your data before asking it to answer questions.
1.4.2 What does univariate analysis answer?
● For continuous variables: what is the typical value, and how spread out are the values?
● For categorical variables: how many people fall into each category, and what
proportion does each category represent?
● For all variables: are there any missing values, outliers, or data entry errors that need to
be addressed before further analysis?
The answers shape every subsequent analytical decision.
|2|
1.4.3 Univariate analysis is both a data cleaning step and a data analysis step
The codebook for this week's dataset describes online_bullying as a yes/no (0/1) variable. The
first time you genuinely look at it, with a frequency table rather than just trusting the
documentation, you find something else.
use bangladesh_adolescent_social_media_mental_health.dta, clear
tabulate online_bullying, missing
online_bullying value Frequency Per cent
0 147 29.4%
1 110 22.0%
2 104 20.8%
3 74 14.8%
4 29 5.8%
5 13 2.6%
6 2 0.4%
7 1 0.2%
Missing 20 4.0%
Eight different values, 0 through 7, not two. This is univariate analysis acting as a cleaning step
and an analysis step at the same time: the simple act of tabulating the variable just exposed a
coding problem you would otherwise have carried into every later analysis. Before using this
variable for anything, you would go back to whoever provided the data and ask what these
values actually represent, most likely a bullying frequency count rather than a yes/no flag, and
document what you found.
Common mistake: trusting a codebook without checking the actual data is one of the most
common errors in student analyses. Always tabulate or summarize every variable yourself
before relying on what the documentation says it contains.
1.4.4 Simple rules for reporting each variable type
Once you know what type of variable you are looking at, the choice of summary measure
follows automatically.
|3|
Variable type Report as
Binary n (%)
Categorical (>2 groups) n (%)
Continuous + Normal Mean ± SD
Continuous + Non normal Median (IQR)
1.5 Univariate analysis is also called descriptive analysis
It produces the descriptive statistics table you see at the start of almost every published study,
usually labelled ‘Table 1: Characteristics of the study population.’ Table 1 tells the reader who
participated in the study and provides the foundation for all subsequent bivariate and
multivariable analyses.
1.6 Example of Table 1
The lecture illustrates Table 1 with the following example. These numbers are the lecture's own
illustration, not from this week's dataset; they show you the format, not a result to reproduce.
Characteristic n (%) Mean (SD) or Median (IQR)
Total participants 5,380
Age (years) 35.4 (12.8)
BMI (kg/m2) 22.8 (20.6, 25.3)
Systolic BP (mmHg) 118.4 (15.6)
HbA1c (%) 5.1 (4.8, 5.6)
Sex: male 2,187 (40.6%)
Sex: female 3,193 (59.4%)
Hypertension 1,531 (34.6%)
Diabetes 643 (12.1%)
Now build the same table for real: here is Table 1 for this week's dataset, built entirely from
bangladesh_adolescent_social_media_mental_health.dta.
|4|
Characteristic n (%) Mean (SD) or Median
(IQR)
Total participants 500
Age (years) 15.7 (2.0)*
Sex: male 226 (45.2%)
Sex: female 272 (54.4%)
Sex: unknown (code 9) 2 (0.4%)
Residence: urban 220 (44.0%)
Residence: rural 278 (55.6%)
Residence: undocumented code (0) 2 (0.4%)
School type: public 119 (23.8%)
School type: private 194 (38.8%)
School type: English medium 104 (20.8%)
School type: madrasa 83 (16.6%)
Self-esteem score 22.1 (4.7)
Daily social media use (hours) 4.2 (2.9, 5.6)†
Sleep duration (hours) 6.3 (1.2)†
PHQ-9 depression score 7.0 (4.0, 10.0)†
Mental health problem (yes) 262 (52.4%)
Suicidal ideation (yes) 41 / 490 (8.4%)
Cyberbullying victim (yes) 75 / 480 (15.6%)
* age_years still contains two impossible values (age 3) that the codebook claims were already
corrected. † these variables contain at least one flagged or impossible value, discussed fully in
Section 5. The median and IQR reported for daily social media use and PHQ-9 score barely
move even with those flagged values included, which is exactly why the median is the right
summary measure for skewed data.
|5|
Note: Table 1 describes the sample as collected. It is normal to report it before you have
finished investigating every unusual value. Section 4 and Section 5 walk through exactly how
you quantify and act on missing data and outliers, rather than silently cleaning the data
before anyone sees the raw picture.
summarize age_years self_esteem_score sleep_duration_hours
tabulate sex, missing
tabulate residence, missing
tabulate school_type
summarize daily_social_media_hours, detail
summarize phq9_score, detail
tabulate mental_health_problem
tabulate suicidal_ideation, missing
tabulate cyberbullying_victim, missing
|6|
2 What should we do with missing values?
2.1 Never automatically delete missing values
Always:
● Quantify them.
● Investigate their pattern.
● Explain how they were handled.
● Report the amount of missing data in your methods and results.
Run misstable summarize, or tabulate varname, missing one variable at a time, and you will
find missingness scattered unevenly across this dataset:
Variable Missing Per cent missing
sex (unknown code 9) 2 of 500 0.4%
suicidal_ideation 10 of 500 2.0%
help_seeking 10 of 500 2.0%
platform_top 10 of 500 2.0%
wealth_quintile 15 of 500 3.0%
online_bullying 20 of 500 4.0%
cyberbullying_victim 20 of 500 4.0%
mother_edu 25 of 500 5.0%
father_edu 25 of 500 5.0%
Two further variables, wealth_quintile and platform_top, also contain values outside the range
the codebook defines (wealth_quintile = 6 for two respondents, against a stated 1–5 range;
platform_top = 7 for two respondents, against a stated 1–5 range). These are not missing
values. They are undocumented codes, and they need investigating in their own right,
separately from the true missingness above.
2.2 How to handle missing values
Option 1: Exclude missing observations (Complete Case Analysis). Software automatically
excludes observations with missing values. This is the most common approach for beginner
analyses, and it is appropriate when missingness is low (under 5%).
|7|
Option 2: Create a missing category. Useful for categorical variables, particularly when
missingness itself may be informative or the missing proportion is substantial.
The lecture illustrates Option 2 with a Gender example: Male, Female, Missing. This dataset
gives you the same structure for real. sex is coded 1 = male, 2 = female, 9 = unknown/other,
and 2 of the 500 adolescents carry that 9 code.
tabulate sex, missing
* Option 1: treat the 9s as true missing, complete case analysis
recode sex (9 = .), generate(sex_cc)
tabulate sex_cc
* Option 2: keep the 9s as their own visible category
label define sexlbl 1 "Male" 2 "Female" 9 "Unknown"
label values sex sexlbl
tabulate sex
2.3 Practical rules
Missing percentage Suggested action
<5% Usually ignore or complete case analysis
5–10% Investigate carefully (create a missing category)
10–20% Consider imputation
>20% Major concern, discuss explicitly
Against this dataset: sex, suicidal_ideation, help_seeking, platform_top, and wealth_quintile all
sit under 5%, usually safe to rely on complete case analysis. mother_edu and father_edu sit
exactly at the 5% boundary.
Practical heuristic: a variable sitting exactly at a threshold, like mother_edu and father_edu
here at 5.0% missing, is precisely when judgement matters most. Do not just round down
because the number looks close to the safe end. Check whether the missingness clusters in
any particular group before deciding.
|8|
3 Dealing with outliers
3.1 Setting the scene
The lecture opens this topic with a short sporting analogy, ‘Who is this player?’, before getting
into the statistics. The idea behind it is simple: spotting that something does not fit the pattern
is a skill you already use all the time, well before you ever learn the word ‘outlier’. Statistics just
gives that instinct a formal definition and a repeatable rule, which is where the rest of this
section picks up.
3.2 What is an outlier?
An outlier is an observation that is unusually different from the rest of the data. The lecture
illustrates this with eight adolescents' daily social media use, in hours: 2, 3, 4, 5, 6, 7, 8, 20.
Seven values sit comfortably between 2 and 8 hours. The eighth, 20 hours, sits far outside that
cluster.
The same pattern, for real, in your dataset: daily_social_media_hours ranges from 0 to 30
hours a day across the 480 adolescents who answered. Two of them, IDs 240 and 260, are
recorded at exactly 30.0 hours. Nobody is awake 30 hours in a day, so before you even reach
for a formal rule, these two values demand a second look.
3.3 Why do outliers matter?
Outliers can:
● Distort the mean
● Inflate standard deviations
● Affect regression coefficients
● Influence statistical significance
● Create misleading conclusions
See it for yourself: including the two 30-hour entries, the mean daily social media use across
the 480 adolescents who answered is 4.31 hours. Drop just those two records and the mean
falls to 4.20 hours, a shift of about six minutes a day across the whole sample. That shift is
modest here because only 2 of 480 records are affected; the same mechanism can swing a
mean far more if errors are more frequent or more extreme.
3.4 Common sources of outliers
Data entry errors. sleep_duration_hours has one adolescent (ID 373) recorded at −1.0 hours.
Sleep duration cannot be negative; this is almost certainly a data entry slip, perhaps a sign that
should have been positive.
Measurement / coding errors. insomnia_score has one adolescent (ID 305) recorded at 99.
Excluding that single case, every other insomnia score in the dataset sits between 0 and 18.
|9|
The value 99 is a classic disguised missing-value code that has leaked into the numeric field
instead of being coded as a true missing value.
Genuine extreme values, and impossible ones, both turn up here too. phq9_score has one
adolescent (ID 357) recorded at 40, and gad7_score has one adolescent (ID 181) recorded at
25. The PHQ-9 has nine items scored 0 to 3 each, so its maximum possible score is 27; the
GAD-7 has seven items scored 0 to 3 each, so its maximum possible score is 21. Both recorded
values are mathematically impossible given how the instruments are scored, which places
them in the error category rather than genuine extremes.
A genuine extreme value, by contrast: two adolescents (IDs 20 and 476) report daily social
media use of 10.0 and 10.3 hours. That is a lot of screen time for a teenager, but it is not
impossible. Unlike the cases above, you would not automatically discard these; they are
flagged by the IQR rule below, but they likely belong in the analysis as real, if heavy, users.
3.5 The IQR Rule
Statistical definition. An observation is an outlier if:
● Value < Q1 − 1.5 × IQR, or
● Value > Q3 + 1.5 × IQR
Where IQR = Q3 − Q1.
Worked example, using daily_social_media_hours
summarize daily_social_media_hours, detail
Step Calculation Result
Q1 (25th percentile) from the detail output 2.90 hours
Q3 (75th percentile) from the detail output 5.60 hours
IQR Q3 minus Q1 2.70 hours
Lower bound Q1 minus 1.5 × IQR −1.15 hours
Upper bound Q3 plus 1.5 × IQR 9.65 hours
Flagged cases Below −1.15 or above 9.65 IDs 20 (10.0h), 240 (30.0h),
260 (30.0h), 476 (10.3h)
egen q1_sm = pctile(daily_social_media_hours), p(25)
egen q3_sm = pctile(daily_social_media_hours), p(75)
gen iqr_sm = q3_sm - q1_sm
gen lower_sm = q1_sm - 1.5*iqr_sm
gen upper_sm = q3_sm + 1.5*iqr_sm
gen sm_outlier = (daily_social_media_hours < lower_sm |
daily_social_media_hours > upper_sm)
| 10 |
tabulate sm_outlier
list id daily_social_media_hours if sm_outlier == 1
3.6 Decision tree
Data entry error? YES → correct or set to missing. NO ↓ Impossible value? YES → set to
missing. NO ↓ Genuine extreme value? YES → usually keep.
Flagged value Data entry Impossible value? Genuine Decision
error? extreme?
ID 373, sleep = −1.0h Likely — — Correct if traceable,
else set to missing
ID 305, insomnia = 99 Likely (disguised — — Set to missing
missing code)
ID 357, PHQ-9 = 40 No Yes (max possible — Set to missing
27)
ID 181, GAD-7 = 25 No Yes (max possible — Set to missing
21)
IDs 240 / 260, SM use No Yes (>24h/day) — Set to missing
= 30.0h
IDs 20 / 476, SM use No No Yes Usually keep;
= 10.0–10.3h consider a
sensitivity analysis
IDs 68 / 419, age = 3 No Yes (below study's — Set to missing
10–19 frame)
Always verify, never assume: the codebook for this dataset states that the two age = 3
entries (IDs 68 and 419) were already replaced with the sample mean of 15.8. Open the data
yourself and check: both records still show age_years = 3. Documentation can be wrong, out
of date, or describe a step that did not make it into the file you were actually given. The
single most useful habit in this lecture is checking the raw values yourself rather than
trusting what you are told about them.
| 11 |
4 Example dataset: social media use and adolescent
mental health
From here, and in your own independent practice, work with
bangladesh_adolescent_social_media_mental_health.dta, a dataset of 500 Bangladeshi
adolescents with 66 variables covering demographics, socioeconomic background, social
media use, mental health and psychosocial wellbeing, sleep, and physical health. You will
receive this file together with its codebook. Every number used as an example in this
document was produced by running Stata commands on exactly this file, so you can
reproduce, check, and extend every result yourself.
Domain Example variables
Demographic & age_years, sex, residence, school_type, mother_edu, father_edu,
socioeconomic wealth_quintile
Social media use daily_social_media_hours, platform_top, night_use_days_week,
online_bullying, cyberbullying_victim
Mental health & phq9_score, phq9_cat, gad7_score, gad7_cat, self_esteem_score,
psychosocial stress_score, mental_health_problem, suicidal_ideation
Sleep & physical health sleep_duration_hours, insomnia_score, sleep_cat,
physical_activity_days_week, history_chronic_illness
Before you start: run codebook, compact and describe as your very first commands on any
new dataset, including this one. Sections 3 to 5 of these notes already show you several
things hiding in this file that the codebook alone would not tell you.
| 12 |
5 Graphical presentation of data
5.1 Why use graphs?
Graphs help us:
● Summarize large amounts of data quickly
● Identify patterns and trends
● Detect outliers and unusual values
● Compare groups
● Communicate findings effectively
5.2 Choosing the right graph
Variable type Recommended graph
Binary variable Bar chart, pie chart
Categorical variable (>2 categories) Bar chart, pie chart
Continuous (normal) Histogram, box plot
Continuous (non normal) Histogram, box plot
Binary, in this dataset: mental_health_problem (52.4% yes, 262 of 500) — a bar chart or pie
chart.
Categorical (>2 groups), in this dataset: phq9_cat, the four-level depression severity
category — a bar chart.
Continuous, normally distributed, in this dataset: self_esteem_score (skewness 0.02) — a
histogram or box plot; the near-zero skewness means the histogram should look close to
symmetric.
Continuous, not normally distributed, in this dataset: daily_social_media_hours (skewness
4.26) — a histogram or box plot; expect a long right tail, and remember the two 30-hour
entries flagged in Section 5 before you interpret it.
| 13 |
graph bar (percent), over(mental_health_problem) title("Mental health
problem")
graph bar (percent), over(phq9_cat) title("PHQ-9 depression severity
category")
histogram self_esteem_score, bin(20) normal title("Self-esteem score")
graph box self_esteem_score, title("Self-esteem score")
histogram daily_social_media_hours, bin(30) normal title("Daily social
media use (hours)")
graph box daily_social_media_hours, title("Daily social media use
(hours)")
| 14 |
6 Common mistakes to avoid
6.1 Mistake 1: using the wrong summary measure
daily_social_media_hours has a skewness of 4.26, heavily right-skewed. Its mean, including the
two impossible 30-hour entries, is 4.31 hours; its median is 4.20 hours, barely moved by those
same entries. Reporting the mean here, rather than the median, overstates typical use and is
exactly the kind of error a quick skewness check would catch.
6.2 Mistake 2: ignoring missing data
mother_edu and father_edu are each missing in 5.0% of respondents, and sex hides 2
respondents under an undocumented code (9) that is easy to miss if you only look at summary
statistics rather than a full frequency table. Run tabulate varname, missing before calculating
any percentage.
6.3 Mistake 3: confusing bar charts and histograms
This dataset has both phq9_cat (categorical, 5 severity groups) and phq9_score (continuous, 0–
27). phq9_cat needs a bar chart; phq9_score needs a histogram. Two variables built from the
same questionnaire, two different graphs, and using the wrong one for either is a conceptual
error, not just a cosmetic one.
6.4 Mistake 4: over-interpreting small differences
Mental health problems are reported by 54.0% of male adolescents and 51.5% of female
adolescents in this sample. A 2.5 percentage point gap looks like something, but Stage 1
analysis is descriptive: it tells you what the data looks like, not whether that gap is real or just
sampling noise. That question belongs to Stage 2, bivariate analysis, covered later in the
course.
6.5 Mistake 5: not labelling graphs
A histogram of daily_social_media_hours without an x-axis label in hours per day, or a bar
chart of mental_health_problem without a y-axis label in percent, forces the reader to guess at
units. Every graph needs a title, axis labels with units, and ideally a note on the sample size.
| 15 |
7 Summary
Stage 1 of data analysis is descriptive. It is not a box to tick before the ‘real’ analysis starts; it is
where you learn what your sample actually looks like. You examine one variable at a time,
choose the right type, the right summary measure, and the right graph, and you treat missing
values and outliers as something to investigate and report, not silently fix.
Across this week's dataset, that meant: discovering that online_bullying does not behave the
way its codebook claims; building Table 1 around 500 adolescents, several with hidden coding
issues; quantifying missingness from 0.4% up to 5.0%; and walking seven flagged values, from
a negative sleep duration to an impossible PHQ-9 score, through a consistent decision tree
rather than a gut reaction.
Use the same workflow on the dataset you have been given. The commands are the same;
only the variable names change.
| 16 |
8 Key concepts: quick reference
Concept One-line definition
Variable A characteristic that can take different values across people, objects, or situations
Binary variable A categorical variable with exactly two categories, usually coded 1 and 0
Categorical A variable with more than two category labels and no numerical meaning
variable
Continuous A numerical variable that can take any value within a range
variable
Table 1 The descriptive statistics table that opens most published health studies
Complete case Excluding observations with missing values on the variable being summarised
analysis
Missing category An explicit ‘missing’ group added to a categorical variable's frequency table
Outlier An observation that is unusually different from the rest of the data
IQR rule An observation is a likely outlier if it falls below Q1 − 1.5×IQR or above Q3 +
1.5×IQR
Mean The arithmetic average. Sensitive to outliers. Use for normally distributed data
Median The middle value when data is sorted. Not affected by outliers. Use for skewed
data
Standard The average distance of each value from the mean. Report alongside the mean
deviation (SD)
Interquartile The range of the middle 50% of the data (Q3 minus Q1). Report alongside the
range (IQR) median
Histogram A graph showing the distribution of a continuous variable, with no gaps between
bars
Boxplot A graph showing the median, IQR, and potential outliers of a continuous variable
Bar chart A graph showing the frequency or percentage of each category, with gaps
between bars
Pie chart A graph showing each category as a slice of a circle, best for two or three
categories
| 17 |
| 18 |