R:如何阻止 update.formula 将 RHS 包装在额外的括号中

R: How to stop update.formula from wrapping RHS in extra brackets

我正在编写一个函数,除其他外,它根据更新的公式从 PLM 包中调用 pgmm。但是,当我使用 stats 中的 update.formula 时,公式的右侧 (RHS) 会莫名其妙地包含在括号中。我不在乎 pgmm 中的 formula= 参数不接受这种语法。

我的基准公式:

 model.AR1.1X = y ~ lag(y,1) + lag(x,1)  

我将其用于一些初步估计,然后通过以下方式更新:

gmm.form = update.formula(model.AR1.1X, . ~ . | lag(y, 2:6) 
# calling this formula shows:
gmm.form
y ~ (lag(yi, 1) + lag(x, 1) | lag(y, 2:6))

注意波浪号 ~( lag(y ... 等..) 和末尾的双括号 2:6 ))

然后调用 pgmm 时出现错误:

 Error in terms.default(formula) : no terms component nor attribute

我自己写公式没有问题(没有额外的括号),但这会使我的代码复杂化。

有没有办法在 update.formula中防止这种行为? 或者,有没有 gsub可用于公式对象以删除这些括号的等价物?

您可以修改调用或使用评论中的粘贴。这是修改方法。

gmm.form[[3]] <- gmm.form[[3]][[2]]
#y ~ lag(y, 1) + lag(x, 1) | lag(y, 2:6)