{"action":"created","issue":{"url":"https://api.github.com/repos/topepo/caret/issues/130","labels_url":"https://api.github.com/repos/topepo/caret/issues/130/labels{/name}","comments_url":"https://api.github.com/repos/topepo/caret/issues/130/comments","events_url":"https://api.github.com/repos/topepo/caret/issues/130/events","html_url":"https://github.com/topepo/caret/issues/130","id":63287487,"number":130,"title":"glmnet prediction defaults to first lambda value if glmnet did not save coefficients for lambdaOpt","user":{"login":"pgswartz","id":2933787,"avatar_url":"https://avatars.githubusercontent.com/u/2933787?v=3","gravatar_id":"","url":"https://api.github.com/users/pgswartz","html_url":"https://github.com/pgswartz","followers_url":"https://api.github.com/users/pgswartz/followers","following_url":"https://api.github.com/users/pgswartz/following{/other_user}","gists_url":"https://api.github.com/users/pgswartz/gists{/gist_id}","starred_url":"https://api.github.com/users/pgswartz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pgswartz/subscriptions","organizations_url":"https://api.github.com/users/pgswartz/orgs","repos_url":"https://api.github.com/users/pgswartz/repos","events_url":"https://api.github.com/users/pgswartz/events{/privacy}","received_events_url":"https://api.github.com/users/pgswartz/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/topepo/caret/labels/next%20release","name":"next release","color":"eb6420"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-03-20T19:26:00Z","updated_at":"2015-04-23T20:29:21Z","closed_at":null,"body":"glmnet sometimes returns a matrix of predicted probabilities / classes / link values, rather than a vector that corresponds to predictions using the 'best' lambda (however chosen).  This occurs when ```lambdaOpt``` does not correspond to one of the lambda's which glmnet has iterated through and stored optimal betas for.  In such a case, ```predict.glmnet``` just returns predictions for all lambda values.  I believe this case is usually related to glmnet not storing optimal betas for more than the first 100 lambdas in the tune grid, even if iterating through more 100.\r\n\r\nThe approach taken in caret, if I'm understanding correctly, is to just take the first column of predictions in such a case.  Looking at predict implementation, line 65 of models/files/glmnet.R, I see:\r\n```\r\nif(is.matrix(out)) out <- out[,1]\r\n```\r\n\r\nFundamentally, I believe the issue is related to glmnet rather than caret, but I'm wondering if, for caret's purposes, it might be reasonable to take the lambda stored in ```finalModel$lambda``` that is closest to ```lambdaOpt``` rather than effectively taking the first lambda.  Happy to make a pull request for this if that'd be helpful!\r\n\r\nAs I further investigate glmnet, I may also have a cleverer solution related to storing more lambda values / beta coefficients.  In that case, we would have a less hacky solution than the one I'm proposing.\r\n\r\nThanks!\r\nPGS"},"comment":{"url":"https://api.github.com/repos/topepo/caret/issues/comments/95710899","html_url":"https://github.com/topepo/caret/issues/130#issuecomment-95710899","issue_url":"https://api.github.com/repos/topepo/caret/issues/130","id":95710899,"user":{"login":"topepo","id":5731043,"avatar_url":"https://avatars.githubusercontent.com/u/5731043?v=3","gravatar_id":"","url":"https://api.github.com/users/topepo","html_url":"https://github.com/topepo","followers_url":"https://api.github.com/users/topepo/followers","following_url":"https://api.github.com/users/topepo/following{/other_user}","gists_url":"https://api.github.com/users/topepo/gists{/gist_id}","starred_url":"https://api.github.com/users/topepo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/topepo/subscriptions","organizations_url":"https://api.github.com/users/topepo/orgs","repos_url":"https://api.github.com/users/topepo/repos","events_url":"https://api.github.com/users/topepo/events{/privacy}","received_events_url":"https://api.github.com/users/topepo/received_events","type":"User","site_admin":false},"created_at":"2015-04-23T20:29:21Z","updated_at":"2015-04-23T20:29:21Z","body":"Normally, the `predict` module that `train` uses would use a model that has the final parameter \"built-in\". I don't remember the details for `glmnet` but there was a technical reason why this wouldn't work. That's were the `lambdaOpt` bit came in. We attached the final lambda value to the `glmnet` object and, in the predict module, use:\r\n```r\r\nout <- predict(modelFit, newdata, s = modelFit$lambdaOpt)\r\n```\r\nEven though `modelFit` is a `glmnet` object that can predict from many possible lambda values, we only get predictions from this one. `predict.glmnet` handles how that happens. \r\n\r\n(One question I get sometimes (for `rpart` models) is \"why are you predicting using a `cp` value not observed in the model?\". The same thing can happen with `glmnet`; there is a path of possible lambda values that are possible for a given data set. The value of  `lambdaOpt` might not be in that path because it was chosen via resampling from a pre-specificed grid. It's not that big of a deal but I do get questions about it.)\r\n\r\nSo, to finally answer your question: `bestTune` values are not directly used for this particular model; the optimized `alpha` is \"baked into\" the final model object and the amount of regularization used in prediction comes from `lambdaOpt` where \r\n\r\n```r\r\nfinalModel$lambdaOpt <- bestTune$lambda\r\n```\r\n"}}