2

In MATLAB we can perform cross-validated LASSO with

[w, FitInfo] = lasso(X, y, 'CV', 3);

and obtain the best weights with

w(:,FitInfo.IndexMinMSE)

How can we find the coefficient of determination?

2 Answers 2

2

If I understand well the MATLAB documentation and the coefficient of determination definition, I think the following code should do the job:

 optimal_weights = w(:,FitInfo.IndexMinMSE);
 SStot = var(y)*length(y);
 predicted_values = X*optimal_weights;
 SSres = sum( (y(:)-predicted_values(:)).^2 );
 R2 = 1 - SSres/SStot;

Note SStot could be computed with sum(), but I think using var() is a bit faster.

0

The above answer, by Yellows is correct, but you have to make sure to include the intercept term ==> Predicted_values = X*2(:,fitInfo.IndexMinMSE) + fitInfo.Intercept(fitInfo.IndexMinMSE)

2
  • 2
    it can be a comment Sep 14, 2017 at 1:25
  • Okay @AniruddhaDas Sep 27, 2017 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.