Ni, Melody Zhifang
2010-Nov-22 15:02 UTC
[R] save a regression model that can be used later
Hi everyone I have a question about how to save a regression model in R and how to retrieve it for making predictions in a new session. To be more specific, I fitted a multilevel logistic regression model using the lmer from the "lme4" package. I then successfully make predictions using fitted(mymodel). Since data are complex (three levels, nested, numerous categorical and continuous data describing types of laparoscopic surgery), the computer takes quite a while to fit the MLM model. I wonder whether it's possible to save the fitted model so that I don't have to fit it again for making predictions every time I start a new R session. I searched the mailing-list archive. Suggestions include using save () to save the model as "mymodel.rda" and then use load(mymodel.rda) into the workspace. I tried without success (in Windows), returning the error message: "Error in object$fitted : $ operator is invalid for atomic vectors" Did I do anything wrong? Any help on this topic is much appreciated BW, Melody -- Dr Melody Ni Imperial College Department of Surgery and Cancer 10th floor, QEQM Building St. Mary's Hospital London W2 1NY Tel/Fax: +44 (0) 20 331 27657/26309 z.ni@imperial.ac.uk<mailto:z.ni@imperial.ac.uk> [[alternative HTML version deleted]]
On Nov 22, 2010, at 10:02 AM, Ni, Melody Zhifang wrote:> Hi everyone > > I have a question about how to save a regression model in R and how > to retrieve it for making predictions in a new session. > > To be more specific, I fitted a multilevel logistic regression model > using the lmer from the "lme4" package. I then successfully make > predictions using fitted(mymodel). > > Since data are complex (three levels, nested, numerous categorical > and continuous data describing types of laparoscopic surgery), the > computer takes quite a while to fit the MLM model. I wonder whether > it's possible to save the fitted model so that I don't have to fit > it again for making predictions every time I start a new R session. > > I searched the mailing-list archive. Suggestions include using save > () to save the model as "mymodel.rda" and then use load(mymodel.rda) > into the workspace. I tried without success (in Windows), returning > the error message: "Error in object$fitted : $ operator is invalid > for atomic vectors"How? ... did you "try" that is. Need code, not vague reports of failure.> > Did I do anything wrong? Any help on this topic is much appreciated > > BW, Melody > > -- > Dr Melody Ni > Imperial College > Department of Surgery and Cancer > 10th floor, QEQM Building-- David Winsemius, MD West Hartford, CT
Hi r-help-bounces at r-project.org napsal dne 22.11.2010 16:02:20:> Hi everyone > > I have a question about how to save a regression model in R and how to > retrieve it for making predictions in a new session. > > To be more specific, I fitted a multilevel logistic regression modelusing the> lmer from the "lme4" package. I then successfully make predictionsusing> fitted(mymodel). > > Since data are complex (three levels, nested, numerous categorical and > continuous data describing types of laparoscopic surgery), the computertakes> quite a while to fit the MLM model. I wonder whether it's possible tosave> the fitted model so that I don't have to fit it again for makingpredictions> every time I start a new R session.When you quit R session with option save =Yes you get a file .Rdata. Whenever you start R with this file you get saved environment back, together with your "mymodel". I recommend for each bigger task to create separate directory in which you can save your .Rhelp and .Rdata file without interfering other tasks. Regards Petr> > I searched the mailing-list archive. Suggestions include using save ()to save> the model as "mymodel.rda" and then use load(mymodel.rda) into theworkspace.> I tried without success (in Windows), returning the error message:"Error in> object$fitted : $ operator is invalid for atomic vectors" > > Did I do anything wrong? Any help on this topic is much appreciated1.11 Data permanency and removing objects from R-intro Regards Petr> > BW, Melody > > -- > Dr Melody Ni > Imperial College > Department of Surgery and Cancer > 10th floor, QEQM Building > St. Mary's Hospital > London W2 1NY > Tel/Fax: +44 (0) 20 331 27657/26309 > z.ni at imperial.ac.uk<mailto:z.ni at imperial.ac.uk> > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
One possibility for what is happening is that when you load the object it does not automatically load the package as well, so you get an error when working with the object. Try loading lme4 package in a new session, then load the saved object and see if things work for you. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Ni, Melody Zhifang > Sent: Monday, November 22, 2010 8:02 AM > To: 'r-help at r-project.org' > Subject: [R] save a regression model that can be used later > > Hi everyone > > I have a question about how to save a regression model in R and how to > retrieve it for making predictions in a new session. > > To be more specific, I fitted a multilevel logistic regression model > using the lmer from the "lme4" package. I then successfully make > predictions using fitted(mymodel). > > Since data are complex (three levels, nested, numerous categorical and > continuous data describing types of laparoscopic surgery), the computer > takes quite a while to fit the MLM model. I wonder whether it's > possible to save the fitted model so that I don't have to fit it again > for making predictions every time I start a new R session. > > I searched the mailing-list archive. Suggestions include using save () > to save the model as "mymodel.rda" and then use load(mymodel.rda) into > the workspace. I tried without success (in Windows), returning the > error message: "Error in object$fitted : $ operator is invalid for > atomic vectors" > > Did I do anything wrong? Any help on this topic is much appreciated > > BW, Melody > > -- > Dr Melody Ni > Imperial College > Department of Surgery and Cancer > 10th floor, QEQM Building > St. Mary's Hospital > London W2 1NY > Tel/Fax: +44 (0) 20 331 27657/26309 > z.ni at imperial.ac.uk<mailto:z.ni at imperial.ac.uk> > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.