The measure reported by the model doesn't match the manual error I calculate. I am using the cv.glmnet
function from the R package glmnet
. I believe when I set the function variable keep
to True
, the cross validation fits will be kept in fit.preval
. However, my manual calculation of mean squared error using fit.preval
and the index of the minimum cv error doesn't match what the model summary is saying. My manual calculation is always much less.
library(glmnet)
x <- data.frame(runif(100),runif(100),runif(100),runif(100),runif(100))
y <- data.frame(runif(100),runif(100),runif(100))
cvfit <- cv.glmnet(x = as.matrix(x), y = as.matrix(y), keep = T, family = "mgaussian")
mean(unlist((cvfit$fit.preval[,,cvfit$index["min",]] - y)^2))
## [1] 0.08803571
cvfit
## Call: cv.glmnet(x = as.matrix(x), y = as.matrix(y), keep = T, family = "mgaussian")
## Measure: Mean-Squared Error
## Lambda Index Measure SE Nonzero
## min 0.06003 1 0.2641 0.01158 1
## 1se 0.06003 1 0.2641 0.01158 1
My manually calculated mean-squared error doesn't match the models measure.
For mgaussian
response, cv computes the mean squared Frobenius norm of the error.
So
errormin = (cvfit$fit.preval[,,cvfit$index["min",]] - y)^2
mean(rowSums(errormin))
will match the reported error