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

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 = √[ Σ(P _{i} – O_{i})^{2} / n ]**

where:

- Σ symbol indicates “sum”
- Pi is the predicted value for the i
^{th}observation in the dataset - Oi is the observed value for the i
^{th}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 »