R: Calculating MSE

Els picture Els · Jan 8, 2013 · Viewed 13k times · Source

I've got a function, I've added noise to it, then smoothed it to get a regression line. How can I find the MSE between the original function and the regression line at 30 equally spaced points?

Or, how can I give R an x value and get the y value on a regression line?

This is a scaled down version of my problem:

> test<- function(m) {3*m^2+7*m+2}  
> r=rnorm(10)  
> m=1:10/10  
> plot(test(m)+r)  
> lines(smooth.spline(1:10,test(m)+r),col="red")  

So I've got the true function values at the 10 equally spaced points i.e. test(m). I just need a way to extract the smooth.spline values at those 10 points, then I should be able to calculate MSE.

Answer

Ben Bolker picture Ben Bolker · Jan 8, 2013

How about:

y <- test(m)+r
y.pred <- predict(smooth.spline(1:10,y))$y
mean((y-y.pred)^2)        ## variance (could mult by n/(n-r) for an unbiased estimate)
mean((test(m)-y.pred)^2)  ## MSE