Statistical Learning: 3.Py Interactions, Qualitative Predictors and Other Details I 2023

Stanford Online · Beginner ·📐 ML Fundamentals ·2y ago

Key Takeaways

The video discusses statistical learning, focusing on interactions, qualitative predictors, and other details in regression models using Python and the statsmodels library. It covers topics such as designing matrices, specifying interactions, polynomial effects, and categorical variables.

Full Transcript

okay so now we're going to go to some more complicated examples and examples where you'll see the power of of model speak thanks Trev okay so a uh you know we've seen how to have design matrices with just a list of column names you know a another common uh modeling tool is an interaction so the way we specify interactions with model spec is just uh instead of just having the column name we put this is a a tupple a twole of the two column names so this tells model spec make me an interaction of the variable lstat and H um so once it's knows how to create the variable lstat and the variable age it can make the interaction between L stat and H okay so let's just uh fit this model and here we have a coefficient it's just another effect in the regression model and in this case because both lat and age are quantitative variables it just multiplies those two together entrywise yes we'll see in a moment you know there are Reg ex problems where not all variables are quantitative in the Boston Housing data actually all of them are but um we'll see uh many are categorical and then uh the interaction would look different but yeah okay so um another example you know another modeling tool that people might use is to rather than fit just a linear effect for a variable might put a polinomial effect so um we've there's a a function poly that allows us to specify um you know polinomial effect in some feature here we'll put a quadratic effect for lstat and a linear effect for age again the model is specified the same way it's a list of terms it's just that this term here poly has actually two columns corresponding to it uh let's fit the model and summarize and we can see that uh the two columns each one gets a separate coefficient estimate and indicated by this is the First Column corresponding and the second column because of course the two degree polinomial has got a quadratic term and a linear term that's right yeah um and if we wanted to actually we could uh uh you could we could consider an interaction between the polinomial and age uh but we'll leave that for for another day um okay so now this example here we've gone from just a linear term for lstat to a quadratic we might be interested in deciding whether there was really um a significant Improvement in fit going from linear to quadratic we can actually see here that the P value is small but there's another method that's commonly used that is the Inova method to compare two different fitted models so this Anova LM function from the stats models what it will do is um Carry Out uh a you know a test an F test comparing two nested models uh and we can use that to decide whether um one model is significantly better than the other this F statistic here 177 that's huge that's huge and it's also should be about 13.3 squared right that's that t statistic should correspond to that um though maybe not exactly I shouldn't I always says it actually no it should in this case that should be yes you know okay um let's see so part of the section that we skipped looked at residuals uh of our early regression model and noted a trend in the residuals plotted as a function of fitted value in this plot actually we can see that uh that trend has been removed um but we didn't see the first plot so let's not dwell too much on it the last example we're going to of model spec we'll we'll use is a um is qualitative predictors or categorical variables so uh in the another data set in the islp package the car seats data tries to predict the sales of car seets as a function of different features um so different features like uh you know the uh competitor's price so forget this maybe a chain like Walmart and maybe we'll compare it competitor's price like Target uh maybe this is income of the uh typical income in the neighborhood of the of that particular store Etc one of these variables is shelf location this is sort of where the car Siege is placed in the in the sh I think low medium high are the levels and so that's you know we could encode low medium high as 0 1 and two but those are kind of arbitrary so this is really a categorical variable I see right um and uh if we look at the column shelf Lo in the uh in the data frame we'll see whoops that's not right uh we'll see that it's been recognized as a category by pendas and so model spec will then uh if we use shelf lo as a um as a predictor it will treat as categorical so here we have we fit a rather complicated model um we first found all the features besides the response sales and we'll add all those features to the model and we added a few interactions just for the fun of it um what we're going to see is for the variable shelf Lo sh location which is categorical it will have uh created more than one column so here oh I guess rather low medium high is good medium and poor I suppose I don't actually know the levels but so it's it's done what it's done is it's made dummy variables um for these three categories and you always need to leave one of them out because they uh aliased with the intercept that's right and and so it's done it all automatically for you so this is another instance of things you would normally have to do yourself to set up the model Matrix this m has Ms model spec has just done it all for you make made it much easier yes and there of course there are various ways you can you can add these two columns so experts you can if experts in specifying these can can do it their own way if you like but we're definitely not going to draw on that okay so that brings us to the the end of the lab on on regression and um next Lab is uh we'll see you next week for classification

Original Description

Statistical Learning, featuring Deep Learning, Survival Analysis and Multiple Testing Trevor Hastie, Professor of Statistics and Biomedical Data Sciences at Stanford University - https://statistics.stanford.edu/people/trevor-j-hastie Robert Tibshirani, Professor of Statistics and Biomedical Data Sciences at Stanford University - https://statistics.stanford.edu/people/robert-tibshirani Jonathan Taylor, Professor Statistics at Stanford University - https://statistics.stanford.edu/people/jonathan-taylor You are able to take Statistical Learning as an online course on EdX, and you are able to choose a verified path and get a certificate for its completion. You can choose to take the course in R (https://www.edx.org/course/statistica) or in Python (https://www.edx.org/learn/data-analysis-statistics/stanford-university-statistical-learning-with-python) For more information about courses on Statistics, you can browse our Stanford Online Catalog: https://stanford.io/3QHRi72
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Stanford Online · Stanford Online · 0 of 60

← Previous Next →
1 Statistical Learning: 13.2 Introduction to Multiple Testing and Family Wise Error Rate
Statistical Learning: 13.2 Introduction to Multiple Testing and Family Wise Error Rate
Stanford Online
2 Statistical Learning: 13.1 Introduction to Hypothesis Testing II
Statistical Learning: 13.1 Introduction to Hypothesis Testing II
Stanford Online
3 Statistical Learning: 12.R.3 Hierarchical Clustering
Statistical Learning: 12.R.3 Hierarchical Clustering
Stanford Online
4 Statistical Learning: 12.R.2 K means Clustering
Statistical Learning: 12.R.2 K means Clustering
Stanford Online
5 Statistical Learning: 12.R.1 Principal Components
Statistical Learning: 12.R.1 Principal Components
Stanford Online
6 Statistical Learning: 13.R.1 Bonferroni and Holm II
Statistical Learning: 13.R.1 Bonferroni and Holm II
Stanford Online
7 Statistical Learning: 12.6 Breast Cancer Example
Statistical Learning: 12.6 Breast Cancer Example
Stanford Online
8 Statistical Learning: 12.5 Matrix Completion
Statistical Learning: 12.5 Matrix Completion
Stanford Online
9 Statistical Learning: 12.4 Hierarchical Clustering
Statistical Learning: 12.4 Hierarchical Clustering
Stanford Online
10 Statistical Learning: 12.3 k means Clustering
Statistical Learning: 12.3 k means Clustering
Stanford Online
11 Statistical Learning: 13.1 Introduction to Hypothesis Testing
Statistical Learning: 13.1 Introduction to Hypothesis Testing
Stanford Online
12 Stanford Seminar - Introduction to Web3
Stanford Seminar - Introduction to Web3
Stanford Online
13 Stanford Seminar - Designing Equitable Online Experiences
Stanford Seminar - Designing Equitable Online Experiences
Stanford Online
14 Stanford CS330: Deep Multi-Task & Meta Learning I 2021 I Lecture 1
Stanford CS330: Deep Multi-Task & Meta Learning I 2021 I Lecture 1
Stanford Online
15 Stanford Seminar - Perceiving, Understanding, and Interacting through Touch
Stanford Seminar - Perceiving, Understanding, and Interacting through Touch
Stanford Online
16 Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 2
Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 2
Stanford Online
17 Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 3
Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 3
Stanford Online
18 Stanford CS330: Deep Multi-Task & Meta Learning I 2021 I Lecture 4
Stanford CS330: Deep Multi-Task & Meta Learning I 2021 I Lecture 4
Stanford Online
19 Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 5
Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 5
Stanford Online
20 Stanford Seminar - Evolution of a Web3 Company
Stanford Seminar - Evolution of a Web3 Company
Stanford Online
21 Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 6
Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 6
Stanford Online
22 Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 7
Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 7
Stanford Online
23 Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 8
Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 8
Stanford Online
24 Stanford Seminar - Designing Human-Centered AI Systems for Human-AI Collaboration
Stanford Seminar - Designing Human-Centered AI Systems for Human-AI Collaboration
Stanford Online
25 The Sh*tFixers: Bob Sutton Interviews David Kelley, Design Thinking Superstar
The Sh*tFixers: Bob Sutton Interviews David Kelley, Design Thinking Superstar
Stanford Online
26 Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 9
Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 9
Stanford Online
27 Women Rise: Sheri Sheppard
Women Rise: Sheri Sheppard
Stanford Online
28 Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 10
Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 10
Stanford Online
29 Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 11
Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 11
Stanford Online
30 Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 12
Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 12
Stanford Online
31 Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 13
Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 13
Stanford Online
32 Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 14
Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 14
Stanford Online
33 Stanford Webinar - Cloud Computing: What’s on the Horizon with Dr. Timothy Chou
Stanford Webinar - Cloud Computing: What’s on the Horizon with Dr. Timothy Chou
Stanford Online
34 Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 15
Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 15
Stanford Online
35 Stanford Seminar - Multi-Sensory Neural Objects: Modeling, Inference, and Applications in Robotics
Stanford Seminar - Multi-Sensory Neural Objects: Modeling, Inference, and Applications in Robotics
Stanford Online
36 Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 16
Stanford CS330: Deep Multi-task & Meta Learning I 2021 I Lecture 16
Stanford Online
37 Stanford Seminar - Toward Better Human-AI Group Decisions
Stanford Seminar - Toward Better Human-AI Group Decisions
Stanford Online
38 Stanford CS330: Deep Multi-Task & Meta Learning I 2021 I Lecture 17
Stanford CS330: Deep Multi-Task & Meta Learning I 2021 I Lecture 17
Stanford Online
39 Stanford CS330: Deep Multi-Task & Meta Learning I 2021 I Lecture 18
Stanford CS330: Deep Multi-Task & Meta Learning I 2021 I Lecture 18
Stanford Online
40 Stanford Webinar - Web3 Considered: Possible Futures for Decentralization and Digital Ownership
Stanford Webinar - Web3 Considered: Possible Futures for Decentralization and Digital Ownership
Stanford Online
41 Stanford Seminar - Ethics Governance-in-the-Making: Bridging Ethics Work & Governance Menlo Report
Stanford Seminar - Ethics Governance-in-the-Making: Bridging Ethics Work & Governance Menlo Report
Stanford Online
42 Stanford Seminar -  Towards Generalizable Autonomy: Duality of Discovery & Bias
Stanford Seminar - Towards Generalizable Autonomy: Duality of Discovery & Bias
Stanford Online
43 Stanford Seminar - ML Explainability Part 1 I Overview and Motivation for Explainability
Stanford Seminar - ML Explainability Part 1 I Overview and Motivation for Explainability
Stanford Online
44 Stanford Seminar - ML Explainability Part 2 I Inherently Interpretable Models
Stanford Seminar - ML Explainability Part 2 I Inherently Interpretable Models
Stanford Online
45 Stanford Seminar - ML Explainability Part 3 I Post hoc Explanation Methods
Stanford Seminar - ML Explainability Part 3 I Post hoc Explanation Methods
Stanford Online
46 Kratika Gupta talks about Stanford's Product Management Program
Kratika Gupta talks about Stanford's Product Management Program
Stanford Online
47 Stanford Seminar - Making Teamwork an Objective Discipline - Sid Sijbrandij CEO & Chairman of GitLab
Stanford Seminar - Making Teamwork an Objective Discipline - Sid Sijbrandij CEO & Chairman of GitLab
Stanford Online
48 Stanford Seminar - ML Explainability Part 4 I Evaluating Model Interpretations/Explanations
Stanford Seminar - ML Explainability Part 4 I Evaluating Model Interpretations/Explanations
Stanford Online
49 Stanford Seminar - Adaptable Robotic Manipulation Using Tactile Sensors
Stanford Seminar - Adaptable Robotic Manipulation Using Tactile Sensors
Stanford Online
50 Stanford Seminar - ML Explainability Part 5 I Future of Model Understanding
Stanford Seminar - ML Explainability Part 5 I Future of Model Understanding
Stanford Online
51 Meet Joe Lapin, Innovation and Entrepreneurship Program Completer
Meet Joe Lapin, Innovation and Entrepreneurship Program Completer
Stanford Online
52 Stanford Seminar: Social Media Scrutiny of Frontline Professionals & Implications for Accountability
Stanford Seminar: Social Media Scrutiny of Frontline Professionals & Implications for Accountability
Stanford Online
53 Stanford Seminar - Alphy and Alphy Reflect: creating a reflective mirror to advance women
Stanford Seminar - Alphy and Alphy Reflect: creating a reflective mirror to advance women
Stanford Online
54 Stanford Webinar - The Digital Future of Health
Stanford Webinar - The Digital Future of Health
Stanford Online
55 Stanford CS229M - Lecture 1: Overview, supervised learning, empirical risk minimization
Stanford CS229M - Lecture 1: Overview, supervised learning, empirical risk minimization
Stanford Online
56 Stanford CS229M - Lecture 2:  Asymptotic analysis, uniform convergence, Hoeffding inequality
Stanford CS229M - Lecture 2: Asymptotic analysis, uniform convergence, Hoeffding inequality
Stanford Online
57 Stanford CS229M - Lecture 3: Finite hypothesis class, discretizing infinite hypothesis space
Stanford CS229M - Lecture 3: Finite hypothesis class, discretizing infinite hypothesis space
Stanford Online
58 Stanford Seminar - Decentralized Finance (DeFi)
Stanford Seminar - Decentralized Finance (DeFi)
Stanford Online
59 Stanford CS229M - Lecture 4: Advanced concentration inequalities
Stanford CS229M - Lecture 4: Advanced concentration inequalities
Stanford Online
60 Stanford Seminar - Bridging AI & HCI: Incorporating Human Values into the Development of AI Tech
Stanford Seminar - Bridging AI & HCI: Incorporating Human Values into the Development of AI Tech
Stanford Online

This video teaches how to build and analyze regression models using Python and statsmodels, covering interactions, polynomial effects, and categorical variables. It provides hands-on examples and explanations of key concepts.

Key Takeaways
  1. Design a regression model using model spec
  2. Specify interactions between variables
  3. Add polynomial effects to the model
  4. Handle categorical variables using dummy variables
  5. Analyze results using ANOVA and F-test
💡 Using model spec to design regression models can simplify the process of specifying interactions, polynomial effects, and categorical variables, making it easier to build and analyze complex models.

Related Reads

📰
SMOTE from scratch: fixing imbalanced data without just copy-pasting rows
Learn to implement SMOTE from scratch to fix imbalanced data, a crucial technique for building robust fraud detectors and other classifiers
Dev.to · Devanshu Biswas
📰
Building a Production Audio Separation API with Meta’s Demucs
Learn to build a production-ready audio separation API using Meta's Demucs for vocal isolation and podcast cleanup
Medium · Machine Learning
📰
Co-Training — Two Models That Grade Each Other’s Homework
Learn co-training, a semi-supervised learning technique where two models grade each other's homework to improve performance
Medium · Python
📰
The Best Developers Don't Know Everything. They Know How to Learn.
Top developers don't know everything, but they excel at learning and adapting to new technologies
Dev.to · Stack Horizon
Up next
10-Phase Deep Learning Roadmap 2026 | AI & Neural Networks | #shorts
SCALER
Watch →