Bird
Raised Fist0
R Programmingprogramming~20 mins

Linear regression (lm) in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linear Regression Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of simple linear regression coefficients
What is the output of the following R code that fits a linear model and extracts coefficients?
R Programming
data <- data.frame(x = 1:5, y = c(2, 4, 6, 8, 10))
model <- lm(y ~ x, data = data)
coef(model)
AIntercept = 1, x = 2
BIntercept = 2, x = 0
CIntercept = 0, x = 2
DIntercept = 0, x = 1
Attempts:
2 left
💡 Hint
Think about the relationship between y and x in the data.
❓ data_output
intermediate
1:30remaining
Number of residuals after fitting lm
After fitting a linear model with 10 observations, how many residuals does the model produce?
R Programming
df <- data.frame(x = 1:10, y = rnorm(10))
model <- lm(y ~ x, data = df)
length(residuals(model))
A10
B9
C11
D1
Attempts:
2 left
💡 Hint
Residuals correspond to each observation.
🔧 Debug
advanced
2:00remaining
Identify the error in lm formula usage
What error does this R code produce when fitting a linear model?
R Programming
data <- data.frame(x = 1:5, y = c(2, 4, 6, 8, 10))
lm(y = x, data = data)
ANo error, model fits successfully
BError: object 'y' not found
CError: argument is missing, with no default
DError: invalid formula
Attempts:
2 left
💡 Hint
Check the formula argument syntax in lm.
🚀 Application
advanced
1:30remaining
Interpreting adjusted R-squared
You fit a linear model and get an adjusted R-squared of 0.85. What does this tell you about the model?
A85% of the variance in the response is explained by the predictors, adjusted for number of predictors
BThe model predictions are 85% accurate
CThe model has 85% chance to predict new data correctly
DThe residuals sum to 0.85
Attempts:
2 left
💡 Hint
Adjusted R-squared measures explained variance accounting for predictors.
🧠 Conceptual
expert
2:30remaining
Effect of multicollinearity on lm coefficients
What is the main effect of strong multicollinearity among predictors in a linear regression model?
AModel residuals become zero
BCoefficients become unstable and standard errors increase
CAdjusted R-squared becomes negative
DThe model cannot be fit at all
Attempts:
2 left
💡 Hint
Think about how correlated predictors affect coefficient estimates.

Practice

(1/5)
1. What does the lm() function in R do?
easy
A. Fits a linear regression model to predict a variable
B. Creates a plot of data points
C. Calculates the mean of a numeric vector
D. Performs a t-test between two groups

Solution

  1. Step 1: Understand the purpose of lm() and compare options

    The lm() 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.
  2. Final Answer:

    Fits a linear regression model to predict a variable -> Option A
  3. Quick Check:

    lm() fits linear regression [OK]
Hint: Remember: lm() stands for linear model fitting [OK]
Common Mistakes:
  • Confusing lm() with plotting functions
  • Thinking lm() calculates summary statistics only
  • Mixing lm() with hypothesis testing functions
2. Which of the following is the correct syntax to fit a linear model predicting y from x in R?
easy
A. model <- lm(y ~ x, data = mydata)
B. model <- lm(x ~ y, data = mydata)
C. model <- lm(y, x, data = mydata)
D. model <- lm(data = mydata, y = x)

Solution

  1. Step 1: Recall the formula syntax for lm() and check options

    The correct syntax uses lm(response ~ predictor, data = dataset). Here, y is the response and x the 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 ~.
  2. Final Answer:

    model <- lm(y ~ x, data = mydata) -> Option A
  3. Quick Check:

    lm(response ~ predictor, data) [OK]
Hint: Formula is always response ~ predictor in lm() [OK]
Common Mistakes:
  • Swapping response and predictor variables
  • Passing variables without formula syntax
  • Misordering arguments in lm()
3. What will be the output of this R code?
data <- data.frame(x = 1:5, y = c(2, 4, 6, 8, 10))
model <- lm(y ~ x, data = data)
coef(model)
medium
A. Intercept = 0, Slope = 1
B. Intercept = 2, Slope = 1
C. Intercept = 1, Slope = 2
D. Intercept = 0, Slope = 2

Solution

  1. 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.
  2. Final Answer:

    Intercept = 0, Slope = 2 -> Option D
  3. Quick Check:

    y = 2*x means intercept 0, slope 2 [OK]
Hint: Check if y is exactly a multiple of x for slope [OK]
Common Mistakes:
  • Assuming intercept is 1 by default
  • Mixing up slope and intercept values
  • Not checking data pattern before fitting
4. Identify the error in this R code for linear regression:
data <- data.frame(x = 1:4, y = c(3, 6, 9, 12))
model <- lm(y = x, data = data)
summary(model)
medium
A. Missing library call for lm()
B. Incorrect formula syntax in lm()
C. summary() cannot be used on lm objects
D. Data frame creation is wrong

Solution

  1. Step 1: Check lm() call and verify other code

    The lm() function requires a formula like response ~ predictor. Here, y = x is incorrect syntax. Data frame creation is correct. summary() works on lm objects. lm() is base R, no library needed.
  2. Final Answer:

    Incorrect formula syntax in lm() -> Option B
  3. Quick Check:

    lm() needs formula, not y = x [OK]
Hint: lm() needs formula like y ~ x, not y = x [OK]
Common Mistakes:
  • Using = instead of ~ in lm() formula
  • Assuming summary() is not for lm objects
  • Thinking lm() requires extra libraries
5. You have a dataset with variables 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?
hard
A. Remove the intercept term to force a better fit
B. Use the model as is because R-squared is not important
C. Try adding more predictor variables to improve the model
D. Switch to a t-test instead of regression

Solution

  1. 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.
  2. Final Answer:

    Try adding more predictor variables to improve the model -> Option C
  3. Quick Check:

    Low R-squared? Add predictors [OK]
Hint: Low R-squared? Add more predictors to improve [OK]
Common Mistakes:
  • Removing intercept to fix low R-squared
  • Ignoring model fit quality
  • Confusing regression with hypothesis testing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard