Linear regression (lm) in R Programming - Time & Space Complexity
We want to understand how the time to run linear regression changes as the data grows.
How does the work needed grow when we add more data points?
Analyze the time complexity of the following code snippet.
# Fit a linear model with one predictor
model <- lm(y ~ x, data = dataset)
This code fits a straight line to predict y from x using all rows in the dataset.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calculating sums and products over all data points to find best fit line.
- How many times: Once over all n data points.
As the number of data points grows, the work grows roughly the same amount.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 sums and multiplications |
| 100 | About 100 sums and multiplications |
| 1000 | About 1000 sums and multiplications |
Pattern observation: Doubling data roughly doubles the work.
Time Complexity: O(n)
This means the time to fit the model grows linearly with the number of data points.
[X] Wrong: "Fitting a linear model takes the same time no matter how much data there is."
[OK] Correct: The model must look at each data point to calculate sums, so more data means more work.
Knowing how time grows with data size helps you explain your code choices clearly and confidently.
"What if we add more predictors (columns) to the model? How would the time complexity change?"
Practice
lm() function in R do?Solution
Step 1: Understand the purpose of
Thelm()and compare optionslm()function is used to fit linear models, which means it finds a line that best predicts one variable from others. Options A, B, and D describe other functions or analyses, not linear regression modeling.Final Answer:
Fits a linear regression model to predict a variable -> Option AQuick Check:
lm()fits linear regression [OK]
- Confusing lm() with plotting functions
- Thinking lm() calculates summary statistics only
- Mixing lm() with hypothesis testing functions
y from x in R?Solution
Step 1: Recall the formula syntax for lm() and check options
The correct syntax useslm(response ~ predictor, data = dataset). Here,yis the response andxthe predictor.model <- lm(y ~ x, data = mydata)matches.model <- lm(x ~ y, data = mydata)reverses response and predictor.model <- lm(y, x, data = mydata)uses wrong argument order.model <- lm(data = mydata, y = x)uses = instead of ~.Final Answer:
model <- lm(y ~ x, data = mydata) -> Option AQuick Check:
lm(response ~ predictor, data) [OK]
- Swapping response and predictor variables
- Passing variables without formula syntax
- Misordering arguments in lm()
data <- data.frame(x = 1:5, y = c(2, 4, 6, 8, 10)) model <- lm(y ~ x, data = data) coef(model)
Solution
Step 1: Analyze data and model fit
The data has x = 1 to 5 and y = 2,4,6,8,10 which is exactly y = 2*x. Since y = 2*x perfectly, intercept is 0 and slope is 2.Final Answer:
Intercept = 0, Slope = 2 -> Option DQuick Check:
y = 2*x means intercept 0, slope 2 [OK]
- Assuming intercept is 1 by default
- Mixing up slope and intercept values
- Not checking data pattern before fitting
data <- data.frame(x = 1:4, y = c(3, 6, 9, 12)) model <- lm(y = x, data = data) summary(model)
Solution
Step 1: Check lm() call and verify other code
The lm() function requires a formula likeresponse ~ predictor. Here,y = xis incorrect syntax. Data frame creation is correct. summary() works on lm objects. lm() is base R, no library needed.Final Answer:
Incorrect formula syntax in lm() -> Option BQuick Check:
lm() needs formula, not y = x [OK]
- Using = instead of ~ in lm() formula
- Assuming summary() is not for lm objects
- Thinking lm() requires extra libraries
height and weight. You want to predict weight from height using linear regression. After fitting model <- lm(weight ~ height, data = df), the summary shows a very low R-squared value. What is the best next step?Solution
Step 1: Interpret low R-squared and next steps
A low R-squared means the model explains little of the variation in weight using height alone. Adding more predictors can help explain more variation. Removing intercept or ignoring R-squared is not good practice. A t-test is unrelated.Final Answer:
Try adding more predictor variables to improve the model -> Option CQuick Check:
Low R-squared? Add predictors [OK]
- Removing intercept to fix low R-squared
- Ignoring model fit quality
- Confusing regression with hypothesis testing
Ready to prove it?
Pass the test and this pattern moves unknown
