0
0
R Programmingprogramming~10 mins

Linear regression (lm) in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Linear regression (lm)
Start with data
Choose dependent and independent variables
Fit linear model lm(y ~ x)
Calculate coefficients (intercept, slope)
Use model to predict y from x
Evaluate model fit (summary)
End
Linear regression fits a straight line to data by finding coefficients that best predict y from x.
Execution Sample
R Programming
x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 5, 4, 5)
model <- lm(y ~ x)
summary(model)
Fit a linear model predicting y from x and show the summary of the fit.
Execution Table
StepActionEvaluationResult
1Create vector xx = c(1,2,3,4,5)x = 1,2,3,4,5
2Create vector yy = c(2,4,5,4,5)y = 2,4,5,4,5
3Fit model lm(y ~ x)Calculate coefficientsmodel with intercept and slope
4Extract coefficientscoef(model)Intercept ≈ 2.2, Slope ≈ 0.6
5Predict y for x=3predict(model, newdata=data.frame(x=3))Predicted y ≈ 3.8
6Show summary(model)summary(model)R-squared ≈ 0.6, p-values, residuals
7EndModel fit completeReady for predictions or diagnostics
💡 All steps complete, model fitted and summary available
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
xNULL[1,2,3,4,5][1,2,3,4,5][1,2,3,4,5][1,2,3,4,5][1,2,3,4,5]
yNULLNULL[2,4,5,4,5][2,4,5,4,5][2,4,5,4,5][2,4,5,4,5]
modelNULLNULLNULLlm objectlm objectlm object
coefficientsNULLNULLNULLNULLIntercept=2.2, Slope=0.6Intercept=2.2, Slope=0.6
Key Moments - 3 Insights
Why does the model have an intercept and slope?
The intercept and slope come from fitting the line y = intercept + slope * x that best matches the data (see execution_table step 4).
What does the summary(model) tell us?
It shows how well the line fits the data, including R-squared and p-values (see execution_table step 6).
Why do we use predict() after fitting the model?
predict() uses the fitted line to estimate y for new x values (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the approximate slope coefficient after fitting the model?
A4.0
B2.2
C0.6
D5.0
💡 Hint
Check the 'Extract coefficients' row in the execution_table where coefficients are shown.
At which step do we get the model object created?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the step where lm(y ~ x) is called and the model is fitted.
If we add a new x value 6 and predict y, which step would this action be similar to?
AStep 5
BStep 1
CStep 3
DStep 6
💡 Hint
Prediction of y for a new x value is done in the step where predict() is used.
Concept Snapshot
Linear regression fits a line y = intercept + slope * x
Use lm(y ~ x) in R to fit the model
Coefficients show intercept and slope
summary(model) shows fit quality
predict(model, newdata) estimates y for new x
Full Transcript
This visual execution traces fitting a linear regression model in R using lm(). We start by creating numeric vectors x and y. Then we fit the model lm(y ~ x), which calculates the best intercept and slope to predict y from x. The coefficients are extracted and show the intercept around 2.2 and slope around 0.6. Using predict(), we estimate y for a new x value. Finally, summary(model) gives statistics about the fit quality like R-squared. Variables x, y, model, and coefficients change as we progress through these steps.