1

I want to write a function that can take a variable number of inputs and regress the first input on the rest of the inputs. More specifically,

Hypothetically, suppose the function had been supplied with 2 or 3 or 4 variables, I would defined it as:

egen_neut<-function(x,y) residuals(lm(x~y,na.action=na.exclude)
egen_neut<-function(x,y,z) residuals(lm(x~y+z,na.action=na.exclude)
egen_neut<-function(x,y,z,w) residuals(lm(x~y+z+w,na.action=na.exclude)

how can I convert the dot-dot-dot, i.e. "...", such that it can be interpreted as a formula with a "+" between the variables, i.e. what will go in place of the ????? below

egen_neut<-function(x,...) {
  residuals(lm(x ~ ?????,na.action=na.exclude)
}
4

3 回答 3

3

这是一种方法:

ff <- function(x, ...) {
    mc <- as.list(match.call())[-1]
    ll <- as.character(mc[[1]])
    rr <- paste(sapply(mc[-(1)], as.character), collapse="+")
    fm <- as.formula(paste(ll, "~", rr))

    # now you can execute `lm` as:
    lm(fm, na.action = na.exclude)
}

# now you can do
ff(x, y, z, w)

所有这些输入变量都驻留在全局环境中。希望这可以帮助。


既然你正在处理data.frame,这就是我的做法:

ff <- function(df, ...) {
    mc <- as.list(match.call())[-(1:2)]
    ll <- as.character(mc[[1]])
    rr <- paste(sapply(mc[-(1)], as.character), collapse="+")
    fm <- as.formula(paste(ll, "~", rr))

    # now you can execute `lm` as:
    lm(fm, data = df, na.action = na.exclude)
}

假设您的 data.frame 是 DF 列 x、y、z 并且您想要这样做x ~ y

ff(DF, x, y)

或 x ~ y + z,则:

ff(DF, x, y, z)

你明白了。

于 2013-08-04T10:26:18.880 回答
1

你不必那样做。只需编写函数以获取公式参数:

egen_neut <- function(fm)
resid(lm(fm, na.action=na.exclude))

egen_neut(x ~ y)
egen_neut(x ~ y + z)
egen_neut(x ~ y + z + w)
于 2013-08-04T06:49:25.783 回答
0

如果将所有变量放入数据框中,则可以使用循环来提取列的子集和 . 在公式对象中,以将加法模型拟合到数据框中尚未在公式中的所有变量。假设您的数据框 d 包含 x、y、z 等列。然后类似

sapply(seq(2, length(d)), function(ix, d) residuals(lm(x ~ ., d[, seq(ix)])), d = d)

应该可以解决问题,但这尚未尝试过,因此可能需要进行一些调整

于 2013-08-04T11:16:09.183 回答