使用已经创建的模型对R中的新数据集进行评分 [英] Using an already created model for scoring a new data set in R

查看:69
本文介绍了使用已经创建的模型对R中的新数据集进行评分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了R的线性回归.现在,我想存储模型并将其用于每周一次对新数据集进行评分.

I have built a linear regression i R. Now I wanna store the model and use it for scoring a new data set once a week.

有人可以帮助我吗?

如何保存模型以及如何导入模型并在新数据集上使用它.

How to save the model and how to import it and use it on an new dataset.

推荐答案

您可以将模型保存到文件中,并在需要时加载它.

You can save the model in a file and load it when you need it.

例如,您可能会有一条这样的线来训练您的模型:

For example, you might have a line like this to train your model:

the_model <- glm(my_formula, family=binomial(link='logit'),data=training_set)

此模型可以保存为:

save(file="modelfile",the_model) #the name of the file is of course arbitrary

稍后,假设该文件位于工作目录中,则可以通过先加载该模型来重用该模型

Later, assuming that the file is in the working directory, you can reuse that model by first loading it with

load(file="modelfile")

然后可以将模型应用于(新的)数据集test_set,例如

The model can then be applied to a (new) data set test_set like, e.g.,

test_set$pred <- predict(the_model, newdata=test_set, type='response')

请注意,在这种情况下,名称the_model不应分配给变量(请勿使用the_model <- load("modelfile")之类的名称).具有名称的模型可通过load()功能使用.此外,该模型与以前相同.新的观测值不会改变模型中的系数或任何内容,而是使用旧"模型对新数据进行预测.

Note that the name, in this case the_model should not be assigned to a variable (don't use something like the_model <- load("modelfile")). The model with its name becomes available with the load() function. Also, the model remains the same as it was before. The new observations are not changing the coefficients or anything in the model - the "old" model is applied to make predictions on new data.

但是,如果您有一个附加的标签集,并且希望基于这些新观察结果来训练/改进模型,则可以按照@David的回答中的建议进行操作.

If, however, you have an additional labeled set and you want to train / improve the model on the basis of these new observations, you can follow the suggestions in the answer by @David.

希望这会有所帮助.

这篇关于使用已经创建的模型对R中的新数据集进行评分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆