KingDan KingDan - 2 months ago 20
R Question

Finding Mean Squared Error?

I have produced a linear data set and have used

lm()
to fit a model to that dataset. I am now trying to find the MSE using
mse()


I know the formula for MSE but I'm trying to use this function. What would be the proper way to do so? I have looked at the documentation, but I'm either dumb or it's just worded for people who actually know what they're doing.

library(hydroGOF)

x.linear <- seq(0, 200, by=1) # x data
error.linear <- rnorm(n=length(x.linear), mean=0, sd=1) # Error (0, 1)
y.linear <- x.linear + error.linear # y data

training.data <- data.frame(x.linear, y.linear)
training.model <- lm(training.data)
training.mse <- mse(training.model, training.data)

plot(training.data)


mse()
needs two data frames. I'm not sure how to get a data frame out of
lm()
. Am I even on the right track to finding a proper MSE for my data?

Answer

Try this:

mean((training.data - predict(training.model))^2)
#[1] 0.4467098