Hello, readers! In this article, we would be walking through an important concept in Machine Learning - R squared (R2) in R programming.
So, let us begin!!
Let us first understand the importance of error metrics in the domain of Data Science and Machine Learning!!
Error metrics enable us to evaluate the performance of a machine learning model on a particular dataset.
There are various error metric models depending upon the class of algorithm.
We have the Confusion Matrix to deal with and evaluate Classification algorithms. While R square is an important error metric to evaluate the predictions made by a regression algorithm.
R squared (R2)
is a regression error metric that justifies the performance of the model. It represents the value of how much the independent variables are able to describe the value for the response/target variable.
Thus, an R-squared model describes how well the target variable is explained by the combination of the independent variables as a single unit.
The R squared value ranges between 0 to 1 and is represented by the below formula:
R2= 1- SSres / SStot
Here,
Always remember, Higher the R square value, better is the predicted model!
In this example, we have implemented the concept of R square error metric on the Linear Regression model.
createDataPartition()
method.lm()
function and then we have called the user-defined R square function to evaluate the performance of the modelExample:
Output:
As seen below, the R square value is 0.82 i.e. the model has worked well for our data.
We can even make use of the summary() function
in R to extract the R square value after modelling.
In the below example, we have applied the linear regression model on our data frame and then used summary()$r.squared
to get the r square value.
Example:
Output:
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.
Till then, Happy Learning!! :)
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.