[R] saving a regression model to a file

Alison Callahan alison.callahan at gmail.com
Thu Apr 14 19:45:00 CEST 2011


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



More information about the R-help mailing list