您可以将模型保存在文件中,并在需要时加载它.
例如,您可能有这样的一行来训练您的模型:
the_model <- glm(my_formula, family=binomial(link='logit'),data=training_set)
Run Code Online (Sandbox Code Playgroud)
该模型可以保存:
save(file="modelfile",the_model) #the name of the file is of course arbitrary
Run Code Online (Sandbox Code Playgroud)
稍后,假设该文件位于工作目录中,您可以通过首先加载该模型来重用该模型
load(file="modelfile")
Run Code Online (Sandbox Code Playgroud)
然后可以将该模型应用于(新)数据集test_set,例如,
test_set$pred <- predict(the_model, newdata=test_set, type='response')
Run Code Online (Sandbox Code Playgroud)
请注意,在这种情况下,名称the_model不应该分配给变量(不要使用类似的东西the_model <- load("modelfile")).具有该名称的模型随该load()功能一起提供.此外,该模型保持与以前相同.新观察结果并未改变模型中的系数或任何内容 - 应用"旧"模型对新数据进行预测.
但是,如果您有一个额外的标记集,并且您希望根据这些新观察结果训练/改进模型,则可以按照@David的答案中的建议进行操作.
希望这可以帮助.