mse from mean in r

To calculate the mean squared error (MSE) from the mean in R, you can use the following code:

main.r
# Sample data
observed <- c(10, 12, 14, 16, 18)
mean_val <- mean(observed)

# Calculate MSE
mse <- sum((observed - mean_val)^2) / length(observed)
mse
151 chars
8 lines

In this code, we first define a vector of observed values observed. Then, we calculate the mean of these values using the mean() function and assign it to mean_val. Finally, we calculate the MSE using the formula sum((observed - mean_val)^2) / length(observed) and assign it to mse.

The formula for MSE is the sum of squared differences between each observed value and the mean, divided by the number of observations. This is commonly used as a measure of the variance or dispersion of a dataset.

gistlibby LogSnag