R 滴虫?(将组内计算的lm残差添加回原始数据帧)

R 滴虫?(将组内计算的lm残差添加回原始数据帧),r,plyr,lm,R,Plyr,Lm,我有一个带因子grpfactor的数据框。我的目标是在每组中分别计算拟合lm模型的残差,并将其存储在原始数据帧中 需要在运行时指定模型,即不能是固定字符串 我发现 它似乎与静态指定的公式一起工作,但是如果我将代码放在函数中,并使用字符串变量而不是字符串文字常量,它将失败-为什么 d <- data.frame(x = rnorm(20), y = rnorm(20), g1 = c(rep('a', 10), rep('b', 10)),

我有一个带因子grpfactor的数据框。我的目标是在每组中分别计算拟合lm模型的残差,并将其存储在原始数据帧中

需要在运行时指定模型,即不能是固定字符串

我发现

它似乎与静态指定的公式一起工作,但是如果我将代码放在函数中,并使用字符串变量而不是字符串文字常量,它将失败-为什么

d <- data.frame(x = rnorm(20), 
            y = rnorm(20), 
            g1 = c(rep('a', 10), rep('b', 10)), 
            g2 = rep(c(rep('c', 5), rep('d', 5)), 2))


myfunc = function(d) {
xx = "y ~ x"
retval = ddply(d,.(g1,g2),transform,z= predict(lm(as.formula(xx))))
return(retval)
}

# This fails:
d.new = myfunc(d)

# With the error:
#Error: error in evaluating the argument 'object' in selecting a 
#method for function 'predict': Error in as.formula(xx) : object 'xx' not 
#found 


# But this works, because it's not encased in a function:
xx = "y ~ x"
retval = ddply(d,.(g1,g2),transform,z= predict(lm(as.formula(xx))))

#Now, with the variable "xx" defined two lines above this line, 
#the following will actually work now:
d.new = myfunc(d)

#But if we do this, it will fail again:
rm(xx)
d.new = myfunc(d)

#ddply works inside a function, but only with a model specified 
#as a literal string:
myfunc2 = function(d) {
retval = ddply(d,.(g1,g2),transform,z= predict(lm(formula=as.formula("y ~ x"))))
return(retval)
}
d.new2 = myfunc2(d)
但是如果as.formula不在函数中,那么同样的代码在没有as.formula的情况下也可以工作

发生了什么事

编辑:好的,这里是它变得非常疯狂的地方:这是可行的区别是,我使用在父环境中定义模型,您得到错误:

Error: object of type 'closure' is not subsettable
因为当ddply尝试在全局环境之前在局部环境中解析t时。事实上,它找到了转置函数闭包t,而不是全局变量t。 您只需更改为其他R内置函数t、c、,。。。例如,这将起作用:

xx <- "y ~ x"
一个完整的例子:

d <- data.frame(x = rnorm(20), 
                y = rnorm(20), 
                g1 = c(rep('a', 10), rep('b', 10)), 
                g2 = rep(c(rep('c', 5), rep('d', 5)), 2))
xx <- "y ~ x"
ddply(d,.(g1,g2),transform,z= predict(lm(as.formula(xx))))

#            x           y g1 g2           z
# 1 -0.2066509 -0.74159051  a  c -0.21886198
# 2 -0.9801753  1.38958373  a  c  0.62214098
# 3 -0.4626821  0.48195967  a  c  0.05950415
# 4  1.2255134 -1.72809777  a  c -1.77596158
# 5 -1.0922717  0.02898265  a  c  0.74401621
# 6 -1.4379229  0.96377879  a  d  0.18312800

看来我把它标上了,回答得太快了!一旦我将ddply放入函数中,它就不再工作了。我正在用一个更好的玩具示例编辑原始问题,以演示错误。@Dimitri这是ddply中的一个bug。您可以查看更多详细信息
Error: object of type 'closure' is not subsettable
xx <- "y ~ x"
d <- data.frame(x = rnorm(20), 
                y = rnorm(20), 
                g1 = c(rep('a', 10), rep('b', 10)), 
                g2 = rep(c(rep('c', 5), rep('d', 5)), 2))
xx <- "y ~ x"
ddply(d,.(g1,g2),transform,z= predict(lm(as.formula(xx))))

#            x           y g1 g2           z
# 1 -0.2066509 -0.74159051  a  c -0.21886198
# 2 -0.9801753  1.38958373  a  c  0.62214098
# 3 -0.4626821  0.48195967  a  c  0.05950415
# 4  1.2255134 -1.72809777  a  c -1.77596158
# 5 -1.0922717  0.02898265  a  c  0.74401621
# 6 -1.4379229  0.96377879  a  d  0.18312800