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.
In the example below, we read in a dataset of 1000 numbers, draw a histogram of their values, then overlay a probability density function. The probability function is completely defined by just 2 parameters and closely matches the distribution of the dataset.
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 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.
.
calculate this probability in R
1 - pnorm (120, mean=100, sd=13)
## [1] 0.0619679
We can also use R to visualize the problem.
# Parameters
mu <- 100
sigma <- 13
# Create a sequence of x values
x_vals <- seq(mu - 4*sigma, mu + 4*sigma, length = 500)
# Normal density values
y_vals <- dnorm(x_vals, mean = mu, sd = sigma)
# Base plot
plot(x_vals, y_vals, type = "l", lwd = 2, col = "black",
ylab = "Density", xlab = "x", main = "P(X > 120) under Normal Curve")
# Shade area where x > 120
x_shade <- seq(120, max(x_vals), length = 100)
y_shade <- dnorm(x_shade, mean = mu, sd = sigma)
polygon(c(120, x_shade, max(x_shade)),
c(0, y_shade, 0),
col = "lightblue", border = NA)
# Add vertical line at x = 120
abline(v = 120, col = "red", lty = 2)
# Add legend with computed probability
p_gt_120 <- 1 - pnorm(120, mean = mu, sd = sigma)
legend("topright", legend = paste0("P(X > 120) = ", round(p_gt_120, 4)),
bty = "n")
.
It’s a subset of population that we use to draw conclusions about the population.
In studying lung cancer in the Bronx, it’s a lot easier to collect info on 30 patients than every single patient that’s ever been treated here at Montefiore and the surrounding hospitals.
.
We use the sample point estimates to figure out the population parameters.
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.
the standard error (SE) is a measurement of the spread in the sample point estimate.
Remember this distinction:
.
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.
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.98778, p-value = 0.4921
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.
| 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 |
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] -1.14918688 0.44046906 -0.20203362 0.45611542 -1.92099665 -2.70246439
## [7] -0.37669252 0.38939779 -0.07782196 -1.00353317 -0.25170764 0.37935000
## [13] -1.08887781 1.07647682 -0.31622814 1.40120373 0.33669312 1.36952001
## [19] 0.57901692 0.06778218 -0.70083398 -0.27488586 -0.90526724 -0.10986025
## [25] 0.44286151 -0.79185606 0.43655710 0.30467749 1.26903804 0.29551744
.