0
0
R Programmingprogramming~10 mins

Linear regression (lm) in R Programming - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to fit a linear model predicting mpg from hp.

R Programming
model <- lm(mpg ~ [1], data = mtcars)
Drag options to blanks, or click blank then click option'
Ahp
Bmpg
Ccyl
Dwt
Attempts:
3 left
💡 Hint
Common Mistakes
Using the response variable 'mpg' as predictor.
Using unrelated variables like 'cyl' or 'wt' without instruction.
2fill in blank
medium

Complete the code to view the summary of the linear model.

R Programming
summary([1])
Drag options to blanks, or click blank then click option'
Amodel
Bmtcars
Clm
Dsummary
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the dataset 'mtcars' instead of the model.
Passing the function name 'lm' or 'summary' itself.
3fill in blank
hard

Fix the error in the code to extract coefficients from the model.

R Programming
coef <- [1](model)
Drag options to blanks, or click blank then click option'
Acoefficients
Bsummary
Ccoef
Dcoeff
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'summary' which returns a summary object, not coefficients directly.
Using 'coefficients' or 'coeff' which are not functions.
4fill in blank
hard

Fill both blanks to create a new data frame with predicted mpg values.

R Programming
new_data <- data.frame(hp = c(100, 150, 200))
predictions <- predict([1], newdata = [2])
Drag options to blanks, or click blank then click option'
Amodel
Bnew_data
Cmtcars
Dlm
Attempts:
3 left
💡 Hint
Common Mistakes
Using the dataset 'mtcars' instead of new data for predictions.
Using 'lm' instead of the model object.
5fill in blank
hard

Fill all three blanks to create a scatter plot with regression line.

R Programming
plot(mtcars$hp, mtcars$mpg, xlab = [1], ylab = [2], main = [3])
abline(model, col = "red")
Drag options to blanks, or click blank then click option'
A"Horsepower"
B"Miles per Gallon"
C"MPG vs Horsepower"
D"Weight"
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up x and y labels.
Using unrelated labels like 'Weight'.