Lecture 2: Stats Intro

2026-fall

Introduction to Statistical Inference

What’s in this section?

    1. What is a population?
    2. What is a sample?
    3. How does the sample relate to the population?

1. What is a population?

In statistics, a population is the entire group of individuals, objects, measurements or outcomes that you want to learn about.

If you had the data for the entire population, you would be able ask and answer very direct questions.

Below we read in a dataset of 1000 numbers, draw a histogram of their values, then overlay a density plot from a probability distribution that fits the data.

numdat = read.csv("numdata1k.csv")
dim(numdat)
## [1] 1000    1
summary(numdat)
##        x         
##  Min.   : 59.21  
##  1st Qu.: 91.85  
##  Median :100.44  
##  Mean   :100.62  
##  3rd Qu.:109.18  
##  Max.   :152.43
hist(numdat[,1], probability=T, breaks=100)
curve(dnorm(x, mean = 100, sd = 13), 
      col = "red", lwd = 2, add = TRUE)

If we can figure out a way to represent the data as a parametric mathematical function, then we can ask questions using that mathematical function instead of having to deal with the full set of numbers.

In the plot above, the red line comes from a normal(Gaussian) probability distribution with mean=100 and standard dev=13. It’s a pretty good fit for the numbers we just read in.

A normal(Gaussian) distribution can be fully described by two parameters:

If you know the 2 parameters \(\mu\) and \(\sigma\), then you know everything there is know about the distribution.

That red line captures the ‘essence’ of 1000 numbers with just 2 parameters.

.

1. What is a population?

Calculus Quiz 1

.

1. What is a population?

2. What is a sample?

.

3. How does the sample relate to the population?

    • We use the sample point estimates to figure out the population parameters.

      • if the sample mean is 75, we would hope the population mean is also close to 75.
    • When we take a sample from a population and calculate a point estimate (like a sample mean or proportion), the result will vary from sample to sample because of the natural randomness in the sampling process.

      • if you repeated this procedure many times, you wouldn’t always get the same point estimate.
    • the standard error (SE) is a measurement of the spread in the sample point estimate.

    • Remember this distinction:

      • standard deviation (SD) is a general function that measures the spread of a set of numbers. It is usually used to describe a population parameter.
      • standard error (SE) is only relevant when you’re talking about sample point estimates and their relation to a population parameter. It’s the amount of spread you would expect to see from the point estimate if you repeated the sampling procedure many times. The larger the sample size, the less spread you’ll see and the closer to the true population parameter you’ll be (for unbiased estimators).

3. How does the sample relate to the population?

    • Statistical Inference
      • If you know the population follows a certain distribution
        • Then you can figure out the distribution of a sample point estimate.
      • If the population observations are independent and identically distributed and follow a normal distribution with parameters \(\mu\) and \(\sigma\),
        • Let \(x_1, x_2, \ldots, x_n \overset{\text{i.i.d.}}{\sim} \mathcal{N}(\mu, \sigma^2)\)
      • then the sample mean also follows a normal distribution.
        • \(\bar{x} \sim \mathcal{N}\left(\mu, \frac{\sigma^2}{n} \right)\)
      • If you know the distribution of the point estimator
        • then you can ask mathematical questions about the population parameters.
    • What are the usual questions?
      • What’s the probability that an interval within a certain range of the sample estimate includes the true population parameter?
        • confidence intervals
      • If you assume population parameters that represent the absence of signal, what’s the probability of seeing a sample estimate with greater signal than what’s been observed under those assumptions?
        • hypothesis tests and p-values
      • How big does my sample size need to be in order to be able to accurately say things about the population?
        • power calculations
    • logic statement (\(A \Rightarrow B\))
      • if A is true, then B is true
        • what if A is False?
    • if the statistical assumptions of our tests are true then we can draw conclusions about the population parameters based on the sample point estimator.
      • what if the assumptions are not met?
    • If \(x_1, x_2, \ldots, x_n \overset{\text{i.i.d.}}{\sim} \mathcal{N}(\mu, \sigma^2))\) then \(\bar{x} \sim \mathcal{N}\left(\mu, \frac{\sigma^2}{n} \right)\) .
      • What if the population isn’t normally distributed?
    • Our statistical conclusions are only valid if the assumptions of the test are met

.

How do you check that your assumptions on the population are correct?

Without having the complete observations of the entire population at hand it will be very hard to prove the underlying assumptions you make about the population parameters are true. The closest we can do is to show they are reasonable.

There are standard procedures for testing the assumptions of every common statistical test (e.g., Shapiro Wilk, Bartlett’s),

But it’s always a good idea to start with a visualization that shows how the distribution of your sample fairs against the distribution you’re assuming it to take.

QQ plot.

Stands for the quantile-quantile plot. Here we show a scatterplot of the quantiles of our sample with the expected quantiles of the distribution we’re expecting it to follow. If the plots coincide with each other, then we can proceed with our test. If not, we should figure out a different test that doesn’t violate its assumptions.

# Step 1: Simulate some data
x <- rnorm(100, mean = 5, sd = 2)  # Normally distributed sample

# Step 2: Sort the sample data
x_sorted <- sort(x)

# Step 3: Compute theoretical quantiles from the standard normal
n <- length(x_sorted)
# Use (i - 0.5)/n quantile levels
p <- (1:n - 0.5) / n
theoretical_q <- qnorm(p, mean = mean(x), sd = sd(x))  # match sample mean/sd

# Step 4: Plot
plot(theoretical_q, x_sorted,
     main = "QQ Plot from Scratch",
     xlab = "Theoretical Quantiles",
     ylab = "Sample Quantiles",
     pch = 19, col = "blue")

# Step 5: Add reference line (through first and third quartiles)
q_theo <- quantile(theoretical_q, c(0.25, 0.75))
q_sample <- quantile(x_sorted, c(0.25, 0.75))
slope <- diff(q_sample) / diff(q_theo)
intercept <- q_sample[1] - slope * q_theo[1]

abline(intercept, slope, col = "red", lwd = 2)

But in practice, you would just use the built-in functions. To check if your data follows a normal distribution, you can use the qqnorm, and shapiro.test functions.

#visual check
qqnorm(x)

#procedural check
#if the p-value is small, then your assumptions are violated
shapiro.test(x)
## 
##  Shapiro-Wilk normality test
## 
## data:  x
## W = 0.98348, p-value = 0.2456

How do you work with probability distributions in R?

In the past we had to use lookup tables to find our probabilities.

Now we use computers.

In R, the functions for working with probability distributions all have a similar format.

distributions

Distribution Name in R Notes
Normal norm Continuous
Binomial binom Discrete
Poisson pois Discrete
Exponential exp Continuous
Chi-squared chisq Continuous
Student’s t t Continuous
F-distribution f Continuous
Uniform unif Continuous
Geometric geom Discrete
Negative Binomial nbinom Discrete
Hypergeometric hyper Discrete
Gamma gamma Continuous
Beta beta Continuous
Weibull weibull Continuous
Logistic logis Continuous

examples

What’s the area under the curve to the the left of 0, in a standard normal distribution?

pnorm(0)
## [1] 0.5

What value of x would give you an area under the curve of 0.5?

qnorm(0.5)
## [1] 0

Plot the values from -3 to 3 of the standard normal distribution

#first get the range of x values you to to plot
xvals = seq(-3, 3, 0.01)

#then get the probabilites of each of those values
yvals = dnorm(xvals)

#now plot them in a scatter plot
plot(xvals, yvals)

Find the area to the right of 1.96 in a standard normal

#this is the area to the left
pnorm(1.96)
## [1] 0.9750021
#to get the area to the right, you can subtract it from 1
1 - (pnorm(1.96))
## [1] 0.0249979

What’s the area between -1.96 and 1.96 in a standard normal?

pnorm(1.96) - pnorm(-1.96)
## [1] 0.9500042

What’s the area under the curve of all the values higher than 1.96, or lower than -1.96?

#area to the left of the lower point
a1 = pnorm(-1.96)
a1
## [1] 0.0249979
#area to the right of the higher point
a2 = 1 - pnorm(1.96)
a2
## [1] 0.0249979
a1+a2
## [1] 0.04999579

generate 30 random numbers from a standard normal distribution

x = rnorm(30)
x
##  [1] -0.95432215 -0.28515122 -1.57502819 -0.33562222 -2.21865419  0.80735407
##  [7] -0.91664212  0.77078600 -0.01685817 -1.88395141  0.56094278 -0.27729275
## [13]  1.93240750  0.38904165 -1.42447942  1.44688360  0.24233406 -0.26041695
## [19]  0.05200804 -1.61673287  2.00346078 -1.07664766 -0.48237020 -1.38009043
## [25]  0.91965947 -0.07994883  1.04690813 -2.47708298 -2.13654282 -0.03938584

.

What are the next the lectures about?