R Tutorial: Basis functions and smoothing
Key Takeaways
This video tutorial covers the basics of Generalized Additive Models (GAMs) in R, focusing on basis functions and smoothing, using the mgcv package and its gam function.
Full Transcript
now that you have a sense of how gams can fit nonlinear data let's learn a bit more about how they work gams are powerful because of their ability to take on many shapes but this is also it makes them challenging their flexibility makes it easy to over fit your data here we'll learn how smoothing helps us deal with this issue in general we want to balance two things when fitting a nonlinear model we want a model that captures the relationship by being close to the data but we also want to avoid fitting our model to noise or overfitting how well the GAM captures patterns and the data is measured by a term called likelihood its complexity or how much the curve changes shape is measured by wiggliness the key to a good fit is the trade-off between the two this trade-off is expressed by this simple equation with a smoothing parameter or a lambda value controlling the balance this smoothing parameter is optimized when R fits a gam to data here are plots of three gams with different smoothing or lambda values as you can see the one on the Left smoothes too much creating a straight line through curve data the one in the middle smoothes too little fitting noise ah rather than the trend the one on the right is just right its lambda value balances over and under fitting normally when we fit a model with mg Seavey's gam function we let the package do the work of selecting a smoothing parameter however we can fix the smoothing parameter to a value of our choosing via the SP argument the SP argument can be set for the whole model vien argument to the GAM function as in the first line of code we can also set the SP argument for a specific term in the GAM formula as shown in the second line of code instead if we allow our to do this work for us the mg CV package offers different methods for selecting smoothing parameters I and most gam experts strongly recommend that you fit models with their Remmel or restricted maximum likelihood method while different methods have their advantages Remmel is most likely to give you stable reliable results in addition to the smoothing parameter the other factor that affects how Wiggly a gam function can be is the number of basis functions that make up a smooth function here I've plotted gams with three seven and twelve basis functions all fit to the same data as you can see a smooth with a small number of basis functions is limited in its wiggliness while one with many basis functions is capable of capturing finer patterns to set the number of basis functions in a smooth we use the K argument in the smooth function in a gam formula setting this value to low will prevent the model from being sufficiently Wiggly if it's high though the automatic smoothing parameter selection will prevent it from being too Wiggly we just don't want to set it very high which can result in a model with more parameters than data or one that is slow to fit later in the model testing portion of the course we will learn how to test if the number of basis functions are adequate to fit our data now let's explore gams with different smoothing parameters and different numbers of basis functions
Original Description
Want to learn more? Take the full course at https://learn.datacamp.com/courses/nonlinear-modeling-in-r-with-gams at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
Now that you have a sense of how GAMs can fit nonlinear data, let's learn a bit more about how they work. GAMs are powerful because of their ability to take on many shapes, but this is also what makes them challenging. Their flexibility makes it easy to over-fit your data. Here we'll learn how smoothing helps us deal with this issue.
In general, we want to balance two things when fitting a nonlinear model. We want a model that captures the relationship by being close to the data, but we also want to avoid fitting our model to noise, or over-fitting.
How well the GAM captures patterns in the data is measured by a term called likelihood. Its complexity, or how much the curve changes shape, is measured by wiggliness. The key to a good fit is the trade-off between the two. This trade-off is expressed by this simple equation, with a smoothing parameter, or lambda value, controlling the balance. This smoothing parameter is optimized when R fits a GAM to data.
Here are plots of three GAMs with different smoothing, or lambda values. As you can see, the one on the left smooths too much, creating a straight line through curved data. The one in the middle smooths too little, fitting noise rather than the trend. The one on the right is just right. It's lambda value balances over-and-under-fitting.
Normally when we fit a model with mgcv's gam() function, we let the package do the work of selecting a smoothing parameter. However, we can fix the smoothing parameter to a value of our choosing via the sp argument. The sp argument can be set for the whole model via an argument to the gam() function, as in the first line of code. We can also set the sp argument for a specific term in the GAM formula, as shown in the second line of code.
Instead if
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from DataCamp · DataCamp · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
SQL Server Tutorial: Date manipulation
DataCamp
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
R Tutorial: Adding aesthetics to represent a variable
DataCamp
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
Python Tutorial: Preparation for modeling
DataCamp
Python Tutorial: Machine Learning modeling steps
DataCamp
R Tutorial: The prior model
DataCamp
R Tutorial: Data & the likelihood
DataCamp
R Tutorial: The posterior model
DataCamp
R Tutorial: An Introduction to plotly
DataCamp
R Tutorial: Plotting a single variable
DataCamp
R Tutorial: Bivariate graphics
DataCamp
Python Tutorial: Customer Segmentation in Python
DataCamp
Python Tutorial: Time cohorts
DataCamp
Python Tutorial: Calculate cohort metrics
DataCamp
Python Tutorial: Cohort analysis visualization
DataCamp
R Tutorial: Building Dashboards with flexdashboard
DataCamp
R Tutorial: Anatomy of a flexdashboard
DataCamp
R Tutorial: Layout basics
DataCamp
R Tutorial: Advanced layouts
DataCamp
Python Tutorial: Time Series Analysis in Python
DataCamp
Python Tutorial: Correlation of Two Time Series
DataCamp
Python Tutorial: Simple Linear Regressions
DataCamp
Python Tutorial: Autocorrelation
DataCamp
R Tutorial: The gapminder dataset
DataCamp
R Tutorial: The filter verb
DataCamp
R Tutorial: The arrange verb
DataCamp
R Tutorial: The mutate verb
DataCamp
R Tutorial: What is cluster analysis?
DataCamp
R Tutorial: Distance between two observations
DataCamp
R Tutorial: The importance of scale
DataCamp
R Tutorial: Measuring distance for categorical data
DataCamp
Python Tutorial: Plotting multiple graphs
DataCamp
Python Tutorial: Customizing axes
DataCamp
Python Tutorial: Legends, annotations, & styles
DataCamp
Python Tutorial: Introduction to iterators
DataCamp
Python Tutorial: Playing with iterators
DataCamp
Python Tutorial: Using iterators to load large files into memory
DataCamp
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
SQL Tutorial: Tables: At the core of every database
DataCamp
SQL Tutorial: Update your database as the structure changes
DataCamp
Python Tutorial: Classification-Tree Learning
DataCamp
Python Tutorial: Decision-Tree for Classification
DataCamp
Python Tutorial: Decision-Tree for Regression
DataCamp
Python Tutorial: Census Subject Tables
DataCamp
Python Tutorial: Census Geography
DataCamp
Python Tutorial: Using the Census API
DataCamp
R Tutorial: A/B Testing in R
DataCamp
R Tutorial: Baseline Conversion Rates
DataCamp
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
R Tutorial: Introduction to qualitative data
DataCamp
R Tutorial: Understanding your qualitative variables
DataCamp
R Tutorial: Making Better Plots
DataCamp
SQL Tutorial: OLTP and OLAP
DataCamp
SQL Tutorial: Storing data
DataCamp
SQL Tutorial: Database design
DataCamp
Python Tutorial: Introduction to spaCy
DataCamp
Python Tutorial: Statistical Models
DataCamp
Python Tutorial: Rule-based Matching
DataCamp
Related Reads
📰
📰
📰
📰
How to learn the basics of c++?
Reddit r/learnprogramming
Composer Isn’t Just a Dependency Manager — It’s the Backbone of Modern PHP Development
Medium · Programming
How a frustrating school project led me to build my first CLI tool.
Medium · JavaScript
Stop Storing Money in float64
Dev.to · Simon Klinkert
🎓
Tutor Explanation
DeepCamp AI