Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 1 problems

This notebook contains the problems from Chapter 1 Data in the No Bullshit Guide to Statistics.

Notebooks setup

datasets/ directory already exists.

P1.1

Create a bar plot that compares the mean scores for the debate and lecture groups in the students dataset (datasets/students.csv). The height of each bar should show the mean score for each curriculum type, with error bars indicating the standard deviation for each group.

Hint: Use sns.barplot and set the required value for the errorbar option.

P1.2

For each of the following sampling scenarios, determine whether the results obtained will apply to the population as a whole.
a) A researcher interested in the mobile usage among 18 year olds used the national citizens registry to obtain a list of all 18 year olds in the country, and randomly selected a sample of size n=100n=100 from this list to contact.
b) A librarian is interested in the lending patterns of digital vs print books for the users of her library. The librarian used the library management system to extract checkout statistics for print books and digital loans for all users of the library for the past five years.
c) The library website administrator ran a survey to collect feedback on the new library discovery tool from the student body. The survey appeared on the website during the month of July and August.
d) A university wants to estimate the proportion of students who have a part-time job, so they ask a random sample of n=60n=60 students from all currently registered students.
e) A researcher wants to estimate the average screen time of teenagers in the country by running a survey on a social media site and analyzing the responses of people who choose to respond.

P1.3

A statistician working for the ministry of health is studying the sleep quality and duration of family doctors in the country. In particular, they are interested in the effectiveness of a new sleep tracking app. For each of the studies described below, determine if it is experimental or observational in nature, and whether it is performed on a representative sample. Will the study results generalize to the population of all doctors in the country? Does the study support a causal conclusion?

a) The researcher uses the national doctors’ registry to select 100 doctors at random, and makes them fill out a survey about the number of hours they sleep per night.

b) The researcher places a banner ad on the central web portal that doctors use every day to communicate with their patients. The ad asks doctors to volunteer for a sleep study, and manages to recruit 200 volunteers. The researcher then randomly assigns half of them to use a sleep tracking app and half to a control group that receives some basic educational messaging about the importance of sleep.

c) The researcher selects 200 doctors at random from the national doctors’ registry, and randomly assigns them either to use the sleep tracking app or to the control group (basic educational messaging).

d) The researcher sends a paper survey by mail to all family doctors that asks them about the number of hours they sleep per night, and analyzed the results of the doctors who replied.

P1.4

Classify the following variables according to their level of measurement subtype: nominal, ordinal, interval, or ratio.
a) Body temperature measured in °C
b) Sleep duration during one night measured in minutes
c) Request priority level (low, medium, high, urgent)
d) File size measured in megabytes
e) Payment method used (cash, credit, debit, e-transfer)
f) T-shirt size (XS, S, M, L, XL, XXL, 3XL)
g) Screen time measured in minutes

P1.5

The data file datasets/markswide.csv contains students’ grades on three tests, and it is organized in “wide” format with columns student_ID, name, test1, test2, test3. Convert this dataset into long format (tidy data) with columns: student_ID, name, test, and grade.

Hint: Use .melt() and set the options id_vars, var_name, and value_name.

Loading...

P1.6

The doctors dataset datasets/doctors.csv contains data from a sleep study. We want to explore the variable score which represents the overall sleep quality score.
a) Generate a strip plot.
b) Generate a box plot.
c) Generate a histogram.
d) Generate a kernel density estimate (KDE) plot.
e) Compute these sample statistics: mean, standard deviation, median, and the quartiles.

P1.7

The pandas convenience method .describe() allows us to compute several key descriptive statistics in a single line of code. For example, the code below computes the descriptive statistics for the variable effort from the students dataset.

count 15.00 mean 8.90 std 1.95 min 5.21 25% 7.76 50% 8.69 75% 10.35 max 12.00 Name: effort, dtype: float64

Can you write your own version of the .describe() method from scratch?

P1.8

Consider the sample x=(10,x2,x3)\mathbf{x} = (10,x_2,x_3). Find the values x2x_2 and x3x_3 such that x‾=10\overline{\mathbf{x}}=10 and sx=5s_{\mathbf{x}}=5.

P1.9

The dataset [x,y]=[(2,2),(3,3),(4,3),(5,5),(6,4),(5,4),(7,6),(8,5)][\mathbf{x}, \mathbf{y}] = [(2,2), (3,3), (4,3), (5,5), (6,4), (5,4), (7,6), (8,5)] consists of eight observations of the variables xx and yy.
a) Draw a scatter plot of the (x,y)(x,y) pairs.
b) Compute x‾\overline{\mathbf{x}}, sxs_{\mathbf{x}}, y‾\overline{\mathbf{y}}, and sys_{\mathbf{y}}.
c) Compute the covariance cov(x,y)\mathbf{cov}(\mathbf{x}, \mathbf{y}).
d) Compute the correlation coefficient corr(x,y)\mathbf{corr}(\mathbf{x}, \mathbf{y}).

Hint: You can use pd.DataFrame([(2,2),(3,3),...],columns=["x","y"]) to create a data frame object from the list of observations.

P1.10

Consider the following Spear--Tukey box plot of the variable z\mathbf{z}.

Determine the values of the following descriptive statistics: med(z)\mathbf{med}(\mathbf{z}), min(z)\mathbf{min}(\mathbf{z}), max(z)\mathbf{max}(\mathbf{z}), Q1(z)\mathbf{Q}_{1}(\mathbf{z}), Q2(z)\mathbf{Q}_{2}(\mathbf{z}), Q3(z)\mathbf{Q}_{3}(\mathbf{z}), and IQR(z)\mathbf{IQR}(\mathbf{z}), range(z)\mathbf{range}(\mathbf{z}).

P1.11

We want to summarize the dataset xz=(1,2,3,4,5,z)\mathbf{x}_z = (1, 2, 3, 4, 5, z) by reporting a the mean mean(x)\mathbf{mean}(\mathbf{x}) and the median med(x)\mathbf{med}(\mathbf{x}).

P1.12

We often encounter non-standard CSV data files. For example, the data file datasets/formats/students_meta.csv contains metadata rows.

# title: Student's dataset with metadata rows
# description: A copy of students.csv with extra metadata at the top
# author: Ivan Savov
# date: 2026-03-25
student_ID,background,curriculum,effort,score
1,arts,debate,10.96,75
2,science,lecture,8.69,75
3,arts,debate,8.6,67
... 12 more lines ...

You’ll get an error if you try loading this file using pd.read_csv with no options. Consult the help docs for the function pd.read_csv to find an option that skips the metadata rows, and load the data below them.

Hint: Use help(pd.read_csv), pd.read_csv?, or the online docs at https://pandas.pydata.org/docs/.

Hint: There are at least two options you can use to skip the metadata rows.

P1.13

The data file datasets/bpwide.csv is in wide format with columns patient, sex, agegrp, bp_before, and bp_after. The last two columns contain blood pressure measurements taken before and after an intervention. This file is not tidy data since there are two observations per row. Convert the data to tidy data (long format), with the columns patient, sex, agegrp, when, and bp, where the variable when encodes when the measurement was taken (Before or After), and bp contains the corresponding measurement.

Loading...

P1.14

The data file datasets/kombuchapop.csv contains the volume measurements from all 1000 bottles in the kombucha batches 55 and 56. This is a census of the entire population. We’ll generate a random sample from the batch 55 population to investigate whether random samples really are representative of the population, as we hope they are.
a) Compute the population mean and population standard deviation.
b) Draw a random sample k=ksample\mathbf{k} = \texttt{ksample} of size n=30n=30 from the population and compute the sample mean k‾\overline{\mathbf{k}} and the sample standard deviation sks_{\mathbf{k}}.
c) Draw 5000 samples of size n=30n=30 from the population compute the sample mean k‾\overline{\mathbf{k}} from each of these samples, and plot a histogram of the 5000 sample means [k‾1,k‾2,…,k‾5000][\overline{\mathbf{k}}_1, \overline{\mathbf{k}}_2, \ldots, \overline{\mathbf{k}}_{5000}].
d) Based on the histogram, what can you say about the random sampling approach? Does random sampling produce representative samples?

P1.15

Your friend Ben has written the following function for generating a random sample of size n from a given population.

Use this function to generate a few samples of size n=30n=30 from Batch 55 of the kombucha population dataset datasets/kombuchapop.csv. Compare the sample means to the population mean. What can you say about the the samples produced by Ben’s sampling function?

P1.16

The dataset datasets/howell30.csv contains sex, age, weight, and height information for 298 individuals.

number of individuals: 298
Loading...

Suppose you’re preparing to run a statistical experiment, and you want to randomly assign half the individuals to Group A (the intervention group), and the remaining individuals to Group B (the control group).

a) Write the Python code that performs the random assignment, then compare the average age in the two groups. Did the random assignment produce two groups of equal size? Did the random assignment produce balanced groups with similar average age?

b) Repeat the random assignment procedure 3000 times and calculate the 3000 differences between average age in the two groups. Plot a histogram of the mean age differences. Does random assignment produce balanced groups?

c) Estimate the proportion of the 3000 assignments that led to groups where the average age differs by more than two years. Does the random assignment procedure guarantee the two groups will be balanced?

P1.17

Load the doctors dataset (datasets/doctors.csv) and compute the descriptive statistics for the categorical variables loc and work.
a) The conditional relative frequency relfreqrur∣cli(loc,work)\mathbf{relfreq}_{\texttt{rur}|\texttt{cli}}(\texttt{loc}, \texttt{work}).
b) The conditional relative frequency relfreqcli∣rur(loc,work)\mathbf{relfreq}_{\texttt{cli}|\texttt{rur}}(\texttt{loc}, \texttt{work}).

Hint: Use the pd.crosstab function with the normalize option.

P1.18

The dataset (datasets/faithful.csv) contains data about the eruptions of the Old Faithful geyser in Yellowstone National Park. We’ll focus on the variable duration, which represents how long each eruption lasted.
a) Compute the mean and the standard deviation of the variable duration.
b) Generate a box plot of the variable duration.
c) Using the information from the previous two answers, what do you think the duration data looks like?
d) Generate a histogram of the variable duration. What does the histogram show that the descriptive statistics didn’t show?

Loading...

P1.19 NEW