Linear regression
You can use the lm()
function to perform simple linear regression. In this exercise, you will fit a model for age
as a function of, or predicted by shuckedWeight
. You will save the model results as an object and then display elements from this output object such as the coefficients
.
You will also run a summary()
of the model object and save that output in another object, which will contain elements related to the model fit such as r.squared
and adj.r.squared
. The abaloneKeep
dataset has been loaded for you.
This exercise is part of the course
R For SAS Users
Exercise instructions
- Use
lm()
to perform a simple linear regression of age by shucked weight. Save the output aslmshucked
. - Display the
coefficients
element fromlmshucked
to get the intercept and slope model coefficients. - Run a
summary()
forlmshucked
and save the output assmrylmshucked
. - From the summary output object
smrylmshucked
, display ther.squared
andadj.r.squared
elements.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Run lm() of age by shuckedWeight, save output as lmshucked
lmshucked <- lm(___ ~ ___, data = ___)
# Display coefficients element from lmshucked
___
# Save summary() output of lmshucked as smrylmshucked
___
# Show r.squared and adj.r.squared elements of smrylmshucked
___
___