如何从步进回归中提取公式?

4

我正在进行一个步骤回归,并希望提取最终公式以在另一个回归中使用。

使用以下示例:

lm1 <- lm(Fertility ~ ., data = swiss)
slm1 <- step(lm1)

我希望能将此分配给公式对象:
Fertility ~ Agriculture + Education + Catholic + 
    Infant.Mortality

看看作为 lm 对象一部分的调用。即: slm1$call - Ricardo Saporta
谢谢。唯一缺少的是一个 as.formula 函数。 - Pierre Lapointe
2个回答

9
你可以使用lm对象的formula方法从slm1对象中提取它。
formula(slm1)
Fertility ~ Agriculture + Education + Catholic + Infant.Mortality

@PLapointe 不客气。在R中,总有不止一种方法来做到这一点... :) - dickoa
@dickoa -- 你的解决方案不仅仅是另一个,而是更好的。正如这里讨论的call元素(在P. Lapointe的答案中使用)不能可靠地捕获实际活动的公式。 - Josh O'Brien

4

Got it:

> as.formula(slm1$call)
Fertility ~ Agriculture + Education + Catholic + Infant.Mortality

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接