Correlation and R-squared
The linear correlation of two variables, \(x\) and \(y\), measures the strength of the linear relationship between them. When \(x\) and \(y\) are respectively:
- the outcomes of a regression model that minimizes squared-error (like linear regression) and
- the true outcomes of the training data,
then the square of the correlation is the same as \(R^2\). You will verify that in this exercise.
unemployment
and unemployment_model
are available for you to use.
This exercise is part of the course
Supervised Learning in R: Regression
Exercise instructions
- Use
cor()
(docs) to get the correlation between the predictions and female unemployment. Assign it to the variablerho
and print it. Make sure you use Pearson correlation (the default). - Square
rho
and assign it torho2
. Print it. - Compare
rho2
to \(R^2\) from the model (usingglance()
). Is it the same?
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# unemployment is available
summary(unemployment)
# unemployment_model is available
summary(unemployment_model)
# Get the correlation between the prediction and true outcome: rho and print it
(rho <- ___)
# Square rho: rho2 and print it
(rho2 <- ___)
# Get R-squared from glance and print it
(rsq_glance <- ___(___)$___)