## S3 method for class 'numeric'
mse(actual, predicted, ...)
## S3 method for class 'numeric'
weighted.mse(actual, predicted, w, ...)
mse(...)
weighted.mse(...)
mean squared error
mse.numeric | R Documentation |
Description
The mse()
-function computes the mean squared error between the observed and predicted <numeric>
vectors. The weighted.mse()
function computes the weighted mean squared error.
Usage
Arguments
actual
|
A |
predicted
|
A |
…
|
Arguments passed into other methods. |
w
|
A |
Value
A <numeric>
vector of length 1.
Calculation
The metric is calculated as,
\[ \frac{1}{n} \sum_i^n (y_i - \upsilon_i)^2 \]
Where \(y_i\) and \(\upsilon_i\) are the actual
and predicted
values respectively.
Examples
# 1) fit a linear
# regression
<- lm(
model ~ .,
mpg data = mtcars
)
# 1.1) define actual
# and predicted values
# to measure performance
<- mtcars$mpg
actual <- fitted(model)
predicted
# 2) evaluate in-sample model
# performance using Mean Squared Error (MSE)
cat(
"Mean Squared Error", mse(
actual = actual,
predicted = predicted,
),"Mean Squared Error (weighted)", weighted.mse(
actual = actual,
predicted = predicted,
w = mtcars$mpg/mean(mtcars$mpg)
),sep = "\n"
)