使用R以编程方式命名对象的最佳方法? [英] Best way to name objects programmatically using R?

查看:110
本文介绍了使用R以编程方式命名对象的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对数据集运行各种建模算法.通过一次对响应的输入变量进行建模,我获得了最佳结果,例如:

I'm running various modeling algorithms on a data set. I've had best results by modeling my input variables to my responses one at a time, e.g.:

model <- train(y ~ x1 + x2 + ... + xn, ...)

训练完模型后,我不想每次都重新运行它们,因此我一直试图将它们另存为.rda文件.这是一个随机森林模型的示例循环(随意提出比循环更好的方法!):

Once I train my models, I'd like to not re-run them each time, so I've been trying to save them as .rda files. Here's an example loop for a random forest model (feel free to suggest a better way than a loop!):

# data_resp contains my measured responses, one per column
# data_pred contains my predictors, one per column

for (i in 1:ncol(data_resp)) {

  model <- train(data_pred_scale[!is.na(data_resp[, i]), ],
                 data_resp[!is.na(data_resp[, i]), i],
                 method = "rf",
                 tuneGrid = data.frame(.mtry = c(3:6)),
                 nodesize = 3,
                 ntrees = 500)

  save(model, file = paste("./models/model_rf_", names(data_resp)[i], ".rda", sep = ""))

但是,当我加载模型时,它将被称为model.

When I load the model, however, it's going to be called model.

我还没有找到一种很好的方法来保存带有相应名称的模型,以供日后尝试使用.我发现可以将一个对象分配给这样的字符串:

I haven't found a good way to save the model with it's corresponding name to try and refer back to it later. I found that one can assign an object to a string like so:

assign(paste("./models/model_rf_", names(data_resp)[i], ".rda", sep = ""), train(...))

但是保存该对象时,我仍然剩下如何引用该对象的信息:

But I'm still left with how to refer to the object when I save it:

save(???, file = ...)

我不知道如何通过其自定义名称来调用该对象.

I don't know how to call the object by it's custom name.

最后,即使是加载也存在问题.我已经尝试过assign("model_name", load("./model.rda")),但是生成的对象string最终仅包含对象名称"model"的字符串.

Lastly, even loading has presented a problem. I've tried assign("model_name", load("./model.rda")), but the resultant object, called string ends up just holding a string of the object name, "model".

在环顾四周时,我发现了这个问题,这个问题似乎很相关,但我想弄清楚如何将其应用于我的情况.

In looking around, I found THIS question, which seems relevant, but I'm trying to figure out how to apply it to my situation.

我可以创建一个列表,其中包含data_resp(我测得的响应)中每个列名称的名称,然后使用lapply来使用train(),但是我仍然对如何动态引用有些犹豫新的对象名称以保留生成的模型.

I could create a list with the names of each column name in data_resp (my measured responses) and then use lapply to use train(), but I'm still a bit stuck on how to dynamically refer to the new object name to keep the resultant model in.

推荐答案

保存模型时,保存另一个名为名称"的对象,该对象是您想要为其命名的事物的字符串:

When you save the model, save another object called 'name' which is a character string of the thing you want to name it as:

> d=data.frame(x=1:10,y=rnorm(10))
> model=lm(y~x,data=d)
> name="m1"
> save(model,name,file="save1.rda")
> d=data.frame(x=1:10,y=rnorm(10))
> model=lm(y~x,data=d)
> name="m2"
> save(model,name,file="save2.rda")

现在每个文件都知道它要调用其结果对象的内容.您如何重新获得负载?加载到新环境中,并分配:

Now each file knows what it wants its resulting object to be called. How do you get that back on load? Load into a new environment, and assign:

> e=new.env()
> load("save1.rda",env=e)
> assign(e$name,e$model)
> summary(m1)

Call:
lm(formula = y ~ x, data = d)

您现在可以安全地rm或重新使用"e"对象.您当然可以将其包装在一个函数中:

You can now safely rm or re-use the 'e' object. You can of course wrap this in a function:

> blargh=function(f){e=new.env();load(f,env=e);assign(e$name,e$model,.GlobalEnv)}
> blargh("save2.rda")
> m2

Call:
lm(formula = y ~ x, data = d)

请注意,这是一件双重的坏事-首先,您可能应该将所有模型作为名称列表存储在一个文件中.其次,此函数有副作用,如果您已经有一个名为m2的对象,它将被踩踏.

Note this is a double bad thing to do - firstly, you should probably store all the models in one file as a list with names. Secondly, this function has side effects, and if you had an object called m2 already it would get stomped on.

像这样使用assign几乎总是一个标志(dyswidt?),您应该改为使用列表.

Using assign like this is nearly always a sign (dyswidt?) that you should use a list instead.

B

这篇关于使用R以编程方式命名对象的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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