缩短 R 中函数调用的长度 - revoScaleR rxGLM()
Shortening Length of Function Calls in R - revoScaleR rxGLM()
我目前正在使用 R 在大型数据集上创建一些 GLM 模型。由于它的大小,我使用了 revoScaleR 包中的 rxGlm() 函数——它比基本的 glm() 函数运行得快得多。
我将所有函数调用保存在 R 脚本中,以便以后可以重现我的工作 - 审计跟踪等。
我的函数调用很长,因为我有很多因素 (~50)。它们看起来都像这样:
rxGlm_C <- rxGlm(Dependent.Variable ~
1 +
Factor 1 +
Factor 2 +
Factor 3 +
...........
Factor N,
family = tweedie(var.power = 1.5, link.power = 0),
data = myDataFrame,
pweights = "Weight.Variable",
)
如果之后,我想重新运行模型拟合,但可能只是对公式稍作更改 - 通常一次删除一个因子 - 是否有任何 shorthand 表示法?目前我正在将函数调用复制并粘贴到我的脚本文件中并手动删除单行。是否有某种语法表示:
"please fit the exact same GLM as last time, but remove Factor 13"?
它会使我的脚本文件短很多。我现在有大约 3,000 行代码,我还没有完成!
谢谢。
艾伦
有两种情况。如果您使用 myDataFrame
中的所有变量,那么您可以简单地编写
rxGlm(Dependent.Variable ~ .,
family = tweedie(var.power = 1.5, link.power = 0),
data = myDataFrame, pweights = "Weight.Variable")
对于完整模型,然后,比如说,
rxGlm(Dependent.Variable ~ . - Factor13,
family = tweedie(var.power = 1.5, link.power = 0),
data = myDataFrame, pweights = "Weight.Variable")
放弃 Factor13
。
如果您没有使用所有变量,那么您可以保存完整的公式,例如,
frml <- y ~ Factor1 + Factor2 + Facto3
然后使用update
:
update(frml, ~ . - Factor3)
# y ~ Factor1 + Factor2
但请注意,在这种情况下 .
表示 "the same right hand side as in frml
",而不是前一个选项中的 "all the variables"。
此外,如果是后一种选择,您可以使用 paste
和 formula
来构建完整的公式。
我目前正在使用 R 在大型数据集上创建一些 GLM 模型。由于它的大小,我使用了 revoScaleR 包中的 rxGlm() 函数——它比基本的 glm() 函数运行得快得多。
我将所有函数调用保存在 R 脚本中,以便以后可以重现我的工作 - 审计跟踪等。
我的函数调用很长,因为我有很多因素 (~50)。它们看起来都像这样:
rxGlm_C <- rxGlm(Dependent.Variable ~
1 +
Factor 1 +
Factor 2 +
Factor 3 +
...........
Factor N,
family = tweedie(var.power = 1.5, link.power = 0),
data = myDataFrame,
pweights = "Weight.Variable",
)
如果之后,我想重新运行模型拟合,但可能只是对公式稍作更改 - 通常一次删除一个因子 - 是否有任何 shorthand 表示法?目前我正在将函数调用复制并粘贴到我的脚本文件中并手动删除单行。是否有某种语法表示:
"please fit the exact same GLM as last time, but remove Factor 13"?
它会使我的脚本文件短很多。我现在有大约 3,000 行代码,我还没有完成!
谢谢。 艾伦
有两种情况。如果您使用 myDataFrame
中的所有变量,那么您可以简单地编写
rxGlm(Dependent.Variable ~ .,
family = tweedie(var.power = 1.5, link.power = 0),
data = myDataFrame, pweights = "Weight.Variable")
对于完整模型,然后,比如说,
rxGlm(Dependent.Variable ~ . - Factor13,
family = tweedie(var.power = 1.5, link.power = 0),
data = myDataFrame, pweights = "Weight.Variable")
放弃 Factor13
。
如果您没有使用所有变量,那么您可以保存完整的公式,例如,
frml <- y ~ Factor1 + Factor2 + Facto3
然后使用update
:
update(frml, ~ . - Factor3)
# y ~ Factor1 + Factor2
但请注意,在这种情况下 .
表示 "the same right hand side as in frml
",而不是前一个选项中的 "all the variables"。
此外,如果是后一种选择,您可以使用 paste
和 formula
来构建完整的公式。