简体   繁体   English

尝试在另一台计算机上使用glm模型进行预测时出错

[英]An error while trying to use glm model for prediction on another computer

I would like to save a glm object in one R machine and use it for prediction on another data set located on another machine that has a newer data.I try to use save and load but with no success.What am I doing wrong? 我想在一台R机器中保存一个glm对象并用它来预测位于另一台有更新数据的机器上的另一台数据集。我尝试使用saveload但没有成功。我做错了什么? Here is a toy example: 这是一个玩具示例:

# on machine 1:
glm<-glm(y~x1+x2,data=dat1, family=binomial(link="logit")
save(glm,file="glm.Rdata") # the file is stored in a folder.

# on machine 2:
load(glm.RData) # got an error:"Error in load(glm.RData) : object 'glm.RData' not found"
#I tried :
 load(file='glm.RData') # no error was displayed
  print(glm) # got an error:"Error in load(glm.RData) : object 'glm.RData' not found"

Any help will be great. 任何帮助都会很棒。

As per @user3710546's advice, I would avoid saving your model using the name glm , as it'll mask (ie. block) the glm() function, making it difficult for you to use it in your session. 根据@ user3710546的建议,我会避免使用名称glm来保存你的模型,因为它会掩盖(即阻塞) glm()函数,这使你很难在你的会话中使用它。

Using save() and load() 使用save()load()

save() is generally used to save a list of objects to a file, rather than a single object. save()通常用于将对象列表保存到文件,而不是单个对象。 The first argument to save() is list , 'A character vector containing the names of objects to be saved.' save()的第一个参数是list ,'包含要保存的对象名称的字符向量'。 (Emphasis mine.) So you'd want to use it like this: (强调我的。)所以你想要像这样使用它:

# On machine 1:
save(list = 'glm', file = '/path/to/glm.RData')

# On machine 2:
load(file = '/path/to/glm.RData')

Note that the file extensions are often case-sensitive: you saved to a file with the extension .RData but loaded from one with the extension .Rdata , which is different. 请注意,文件扩展名通常区分大小写:您保存到扩展名为.RData的文件,但是从扩展名为.Rdata的文件加载,这是不同的。 This may explain why the file isn't found. 这可以解释为什么找不到该文件。

Using saveRDS() and readRDS() 使用saveRDS()readRDS()

An alternative to using save() and load is to use saveRDS() and readRDS() , which are designed to be used with one object. 使用save()load的另一种方法是使用saveRDS()readRDS() ,它们被设计用于一个对象。 They're used slightly differently: 它们的使用方式略有不同:

# On machine 1
saveRDS(glm, file = '/path/to/glm.rds')

# On machine 2
glm = readRDS(file = '/path/to/glm.rds')

Note the .rds file extension and the fact that readRDS() isn't automatically put in the environment (it needs to be assigned to something). 请注意.rds文件扩展名以及readRDS()不会自动放入环境中的事实(需要将其分配给某些内容)。

Saving parts of a GLM 保存GLM的部分内容

If you just want the formula saved—that is, the actual text string—you can find it in glm$formula , where glm is the name of your object. 如果你只想保存公式 - 即实际的文本字符串 - 你可以在glm$formula找到它,其中glm是你的对象的名字。 It comes back as a formula object, but you can convert it to a string with as.character(glm$formula) , to then be written to a text file or whatever. 它作为formula对象返回,但您可以将其转换为带有as.character(glm$formula)的字符串,然后将其写入文本文件或其他任何内容。

If, however, you want the model itself without the dataset it was created from (to cut down on disk space), have a look at this article , which discusses which parts of a glm object can be safely deleted. 但是,如果您希望模型本身没有创建数据集(以减少磁盘空间),请查看本文该文章讨论了可以安全删除glm对象的哪些部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM