How to Calculate Root Mean Square Error (RMSE) in R

[This article was first published on Methods – finnstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Root Mean Square Error In R, The root mean square error (RMSE) allows us to measure how far predicted values are from observed values in a regression analysis.

In other words, how concentrated the data around the line of best fit.

RMSE = √[ Σ(Pi – Oi)2 / n ]

where:

  • Σ symbol indicates “sum”
  • Pi is the predicted value for the ith observation in the dataset
  • Oi is the observed value for the ith observation in the dataset
  • n is the sample size

Naive Bayes Classification in R » Prediction Model »

Root Mean Square Error in R.

Method 1: Function

Let’s create a data frame with predicted values and observed values.

data <- data.frame(actual=c(35, 36, 43, 47, 48, 49, 46, 43, 42, 37, 36, 40),
predicted=c(37, 37, 43, 46, 46, 50, 45, 44, 43, 41, 32, 42))
data
   actual predicted
1      35        37
2      36        37
3      43        43
4      47        46
5      48        46
6      49        50
7      46        45
8      43        44
9      42        43
10     37        41
11     36        32

We will create our own function for RMSE calculation

sqrt(mean((data$actual - data$predicted)^2))
2.041241

The root mean square error is 2.041241.

Market Basket Analysis in R » What Goes With What »

Method 2: Package

rmse() function available from the Metrics package, Let’s make use of the same.

rmse(actual, predicted)

library(Metrics)
rmse(data$actual, data$predicted)
2.041241

The root mean square error is 2.041241.

Conclusion

Mean square error is a useful way to determine the extent to which a regression model is capable of integrating a dataset.

The larger the difference indicates a larger gap between the predicted and observed values, which means poor regression model fit. In the same way, the smaller RMSE that indicates the better the model.

Based on RMSE we can compare the two different models with each other and be able to identify which model fits the data better.

Decision Trees in R » Classification & Regression »

The post How to Calculate Root Mean Square Error (RMSE) in R appeared first on finnstats.

To leave a comment for the author, please follow the link and comment on their blog: Methods – finnstats.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)