[R] saving a regression model to a file

From: Alison Callahan <alison.callahan_at_gmail.com>
Date: Thu, 14 Apr 2011 13:45:00 -0400


Hello all,

First off, I am using R version 2.13.0 in Ubuntu.

I have read previous posts in the R mailing list on saving models for later use, and the responses indicate using the R save() function to save a model and then using load() to load it is one way to go. However, when I use the save() function to save a model to a file, and then try to load it using load(), the loaded "model" is just an object of class Character. Here is the relevant R code:

#######creating the model#########

data <- read.table("../synthetic_data_4.csv", header=TRUE, sep=",")

data <- data.frame(data)

data$type <- factor(data$type)

pcrmodel <- pcr(formula=count~week+type+total, data=data, x=TRUE, y=TRUE)

#########getting summary of model########

summary(pcrmodel)

Output:

Data: 	X dimension: 318 4
	Y dimension: 318 1

Fit method: svdpc
Number of components considered: 4
TRAINING: % variance explained
       1 comps  2 comps  3 comps  4 comps
X        99.57   100.00   100.00   100.00
count 13.70 16.01 16.17 16.54

########saving the model#########

save(pcrmodel, file='model.rda')

########loading the model#########

model <- load('model.rda')

########getting summary of model after loading###########

summary(model)

Output:

 Length     Class      Mode
        1 character character


##################################

What am I doing wrong? Or is this not the correct way to save a model to be used later? Thanks,

Alison



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. Received on Thu 14 Apr 2011 - 17:49:14 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 14 Apr 2011 - 18:30:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive