Regression Models & Hypothesis Testing


Regression Models & Hypothesis Testing

Anna
Welcome back! Today we're diving into regression analysis – a cornerstone of data science. It's all about understanding relationships between variables, like how bill length relates to a penguin's body mass or what factors drive product sales. And the tools we build here will set us up for more advanced machine learning later.

Hope
I love that we can finally quantify these relationships. But before we jump into the math, we need a roadmap. The course introduces the PACE framework: Plan, Analyze, Construct, and Execute. It keeps us organized as we go from understanding the problem to sharing insights.

Anna
Exactly. In the Plan stage, we think about the business context and what data we have. Then Analyze is where we explore the data and check assumptions. In Construct we build the model, and finally Execute means interpreting results and telling the story. It's iterative too – you might loop back as you learn more.

Hope
Let's start with the simplest model: simple linear regression. It estimates the relationship between one independent variable X and a continuous dependent variable Y. The equation is Y = β₀ + β₁X, where β₀ is the intercept and β₁ is the slope. For every one-unit increase in X, Y changes by β₁.

Anna
But how do we find the best β₀ and β₁? That's where Ordinary Least Squares, or OLS, comes in. It minimizes the sum of squared residuals – the differences between actual values and the values our line predicts. The computer does the heavy lifting, but understanding that we're minimizing error is key.

Hope
And we can't just trust any line. We have to check four assumptions for simple linear regression: linearity (the relationship should look like a line), normality of residuals (errors should be bell-shaped), independent observations (no hidden connections between data points), and homoscedasticity (constant variance of residuals).

Anna
Right. For linearity, we can just scatter‑plot X vs. Y. Normality we check after building the model using a Q‑Q plot of residuals. Independence is more about the data collection – like, each penguin is a separate observation. And homoscedasticity we see from a plot of fitted values vs. residuals – it should look like a random cloud.

Hope
Once the assumptions are satisfied, we can evaluate the model. The most common metric is R‑squared, which tells us the proportion of variance in Y explained by X. In the penguin example, bill length explained about 77% of the variation in body mass – pretty good for one variable!

Anna
But we also need to report uncertainty. That's where confidence intervals and p‑values come in. The p‑value tests if the slope is significantly different from zero. A small p‑value means there's strong evidence that X and Y are related. And the confidence interval gives a range where the true slope likely lies.

Hope
Now, real‑world problems rarely have just one predictor. That's where multiple linear regression shines. It extends the equation to Y = β₀ + β₁X₁ + β₂X₂ + … + βₙXₙ. We can include many independent variables, like bill length, species, and gender to predict penguin body mass.

Anna
But with many variables come new challenges. First, we need to handle categorical variables – like species or gender. One‑hot encoding turns them into binary columns (e.g., is_male, is_female). And we can include interaction terms when we think two variables together affect Y differently than alone.

Hope
Also, multiple regression has a fifth assumption: no multicollinearity. That means your predictor variables shouldn't be too highly correlated with each other. If they are, it becomes hard to tell which one is truly driving Y. We can check with a Variance Inflation Factor (VIF) – values above 5 or 10 are problematic.

Anna
Another issue is overfitting – if we throw in too many variables, the model might fit the training data perfectly but fail on new data. That's why we use adjusted R‑squared, which penalizes unnecessary variables. And we can use holdout samples: train on one part of the data, test on another.

Hope
To choose which variables to include, we have techniques like forward selection (start with nothing, add one at a time) and backward elimination (start with all, remove the weakest). These rely on the extra‑sum‑of‑squares F‑test to see if adding or removing a variable significantly improves the model.

Anna
And when datasets are huge, we can use regularization – like lasso, ridge, or elastic‑net. Lasso can actually shrink some coefficients to zero, effectively removing variables. Ridge shrinks them but keeps all variables. This balances the bias‑variance tradeoff: a little bias can reduce variance and prevent overfitting.

Hope
Great. Now let's shift to hypothesis testing for categorical data. The chi‑squared goodness‑of‑fit test checks if an observed categorical variable matches an expected distribution – for example, are popcorn sales really 25% for each size? The test of independence checks if two categorical variables are associated, like weather and popcorn sales.

Anna
Then there's ANOVA – analysis of variance – which compares means across three or more groups on a continuous outcome. One‑way ANOVA looks at one categorical factor (e.g., butterfly species and lifespan). Two‑way ANOVA adds a second factor (species and size) and can test for interaction between them.

Hope
After a significant ANOVA, we don't know which groups differ. That's where post‑hoc tests like Tukey's HSD come in – they run pairwise comparisons while controlling for the increased chance of false positives. In Python, we can use `pairwise_tukeyhsd` from statsmodels.

Anna
ANOVA can be extended to ANCOVA (analysis of covariance) which adds continuous covariates to control for their effect. For example, comparing book sales across genres while controlling for publication year. Then MANOVA and MANCOVA handle multiple outcome variables at once – like both sales and profit.

Hope
Finally, logistic regression. When our Y is categorical (e.g., will a user click or not?), we use logistic regression to model the probability of the event. Instead of a line, we get an S‑shaped curve. The key is the logit link: log(p/(1‑p)) = β₀ + β₁X, which ensures predictions stay between 0 and 1.

Anna
Logistic regression assumptions include linearity in the logit (each X should be linearly related to the log‑odds), independence, little multicollinearity, and no extreme outliers. We estimate coefficients using Maximum Likelihood Estimation (MLE), which finds the betas that make the observed data most probable.

Hope
To evaluate a logistic model, we use a confusion matrix showing true/false positives and negatives. From that we derive precision (of those we said were lying down, how many actually were?), recall (of those actually lying down, how many did we catch?), and accuracy (overall correct classifications).

Anna
Interpreting coefficients is different too. In the lying‑down example, a one‑unit increase in vertical acceleration decreased the odds of lying down by 11% (because we exponentiated the negative coefficient). And we always remember: even with a strong model, correlation isn't causation – we need to be honest storytellers.

Hope
Choosing the right model depends on your question: continuous Y? Linear regression. Categorical Y? Logistic regression. Comparing groups? ANOVA. And always start with PACE – plan, analyze, construct, execute. This framework keeps us disciplined and helps us communicate effectively with stakeholders.

Anna
We've covered a lot – from simple lines to complex models with regularization and hypothesis tests. But the core is always understanding your data, checking assumptions, and telling a clear story. You now have a solid regression toolkit to tackle real problems. Great work, and see you next time!

Ask anything
We've read this whole study set, so questions about it get the best answers.