[1] 1.5
Standardizing Data, Finding Probabilities, and Testing a Sample Mean
Farmingdale State College
\[z = \frac{x - \mu}{\sigma}\]
A z-score tells you how many standard deviations a value is from the mean
Once we have a z-score, we can find probabilities: what proportion of scores fall above, below, or between values
Today’s habit: we will write out every argument by name (mean =, sd =, lower.tail =). It is more typing, but you never have to guess what a number is doing.
Professor Brocker’s PSY 348 midterm had
40students. The exam scores were approximately normally distributed with a mean of76and a standard deviation of8.
Nadia scored an 88.
Before we calculate anything: is that a good score relative to the class? Great? Just okay?
\[z = \frac{x - \mu}{\sigma} = \frac{88 - 76}{8} = 1.5\]
Nadia’s score is 1.5 standard deviations above the mean
Using the 68/95/99.7 rule: roughly 93% of students scored below Nadia
We store the facts first, so every later line says what each number is.
pnorm() returns the probability of scoring at or below a value: \(P(X \le x) = P\!\left(Z \le \dfrac{x - \mu}{\sigma}\right)\), the area to the left of the z-score.
pnorm_two_ways.R
Same answer: 93.3% score at or below Nadia, so about 6.7% outscore her
Raw score + the real mean and sd = z-score + mean = 0, sd = 1
Mixing them (a raw score with mean = 0, sd = 1) is the most common mistake in this lab
Click Code to see how the plot is built.
library(ggplot2)
dat <- data.frame(x = seq(from = 50, to = 102, by = 0.1))
dat$y <- dnorm(x = dat$x, mean = 76, sd = 8)
ggplot(data = dat, mapping = aes(x = x, y = y)) +
geom_line(linewidth = 1.1) +
geom_area(
data = subset(dat, x >= 88),
fill = "orange", alpha = 0.6
) +
geom_vline(xintercept = 88, linetype = "dashed") +
theme_minimal(base_size = 18) +
labs(
title = "Probability of Scoring 88 or Higher",
x = "\nExam Score",
y = "Density\n"
) +
theme(panel.grid = element_blank())qnorm() does the reverse: give it a probability, get back a score.
qnorm_top10.R
86 to be in the top 10% of this classNow we simulate 40 students. scale() converts every score to a z-score at once.
scale_class.R
score z
1 76.6 0.14
2 83.9 1.09
3 82.3 0.88
4 59.2 -2.11
5 69.8 -0.74
6 82.6 0.92
7 70.8 -0.61
8 69.5 -0.78
z_scores? The standard deviation?mean() and sd() Compute\[\bar{x} = \frac{\sum x}{n} \qquad s = \sqrt{\frac{\sum (x - \bar{x})^2}{n - 1}}\]
The numerator of \(s\) adds up each score’s squared distance from the mean. Dividing by \(n - 1\) and taking the square root gives the typical distance of a score from the mean.
Our 40 simulated scores are a sample, so their mean and sd are close to, but not exactly, 76 and 8.
class_vs_population.R
Nadia is one student. What if we care about the average of a whole class?
A single score varies a lot from student to student
An average of many scores varies much less
So a sample mean needs a different yardstick than a single score
Draw many samples of size n, record each sample’s mean, then look at how those means are spread out. The sd() of these simulated means is the standard error, \(SE = \dfrac{\sigma}{\sqrt{n}}\) (next slides).
sampling_sim.R
[1] 2.571714
[1] 1.273569
[1] 0.6340208
library(dplyr)
sims <- bind_rows(
data.frame(mean = means_10, n = "n = 10"),
data.frame(mean = means_40, n = "n = 40"),
data.frame(mean = means_160, n = "n = 160")
) |>
mutate(n = factor(x = n, levels = c("n = 10", "n = 40", "n = 160")))
ggplot(data = sims, mapping = aes(x = mean)) +
geom_histogram(bins = 40, fill = "#5C715E", color = "white") +
facet_wrap(facets = ~ n) +
theme_minimal(base_size = 18) +
labs(
title = "5,000 Sample Means at Each Sample Size",
x = "\nSample Mean", y = "Count\n"
) +
theme(panel.grid = element_blank())The spread of the sample means has a name: the standard error.
\[SE = \frac{\sigma}{\sqrt{n}}\]
standard_error.R
n and the standard error is cut in halfSame population: \(\mu = 76\), \(\sigma = 8\).
| Question | Formula | Result |
|---|---|---|
| Nadia scored 88. How unusual is she? | \(z = \dfrac{88 - 76}{8}\) | \(z = 1.5\) |
| A class of 40 averaged 80. How unusual is that class? | \(z = \dfrac{80 - 76}{8/\sqrt{40}}\) | \(z \approx 3.16\) |
Question: a class of n = 40 averaged 80. If the population mean really is 76 (with \(\sigma = 8\)), how likely is a class mean this high?
\[z = \frac{\bar{x} - \mu}{\sigma / \sqrt{n}}\]
The numerator is how far the sample mean sits from the population mean. The denominator is the standard error, so z counts standard errors.
z_test.R
mu <- 76 # population mean
sigma <- 8 # population standard deviation
n <- 40 # sample size
m <- 80 # observed sample mean
se <- sigma / sqrt(x = n)
z <- (m - mu) / se
# One-tailed: P(sample mean >= 80)
p_one <- pnorm(q = z, mean = 0, sd = 1, lower.tail = FALSE)
# Two-tailed: the extreme could have been in either direction
p_two <- 2 * p_one
c(se = se, z = z, p_one = p_one, p_two = p_two) se z p_one p_two
1.2649110641 3.1622776602 0.0007827011 0.0015654023
lower.tail = FALSE: we want the area above zse <- 8 / sqrt(x = 40)
dat2 <- data.frame(x = seq(from = 76 - 4 * se, to = 76 + 4 * se, length.out = 400))
dat2$y <- dnorm(x = dat2$x, mean = 76, sd = se)
ggplot(data = dat2, mapping = aes(x = x, y = y)) +
geom_line(linewidth = 1.1) +
geom_area(data = subset(dat2, x >= 80), fill = "orange", alpha = 0.6) +
geom_vline(xintercept = 80, linetype = "dashed") +
theme_minimal(base_size = 18) +
labs(
title = "Sampling Distribution of the Mean (n = 40, if mu = 76)",
x = "\nSample Mean", y = "Density\n"
) +
theme(panel.grid = element_blank())Use the exam setting: \(\mu = 76\), \(\sigma = 8\). Write every argument by name.
Calculate the z-score for a student who scored a 70. Is that above or below average?
Using pnorm(), what proportion of the population scored below a 70?
Using qnorm(), what raw score marks the bottom 25%?
Same setting: \(\mu = 76\), \(\sigma = 8\).
What is the standard error when n = 25? When n = 100? By what factor did it shrink?
A different class of n = 25 averaged 79. Run a two-tailed z-test against \(\mu = 76\). Report the standard error, z, and p-value.
In your own words: why can a class mean of 79 be “unusual” when an individual score of 79 is not?
Your answers to the six questions above (show your R code and output)
One sentence: is a z-score of 0 a “good” or “bad” score? Explain.
PSY 348 - Statistics for Psychology