我有这个功能:
> ?.est <- function(x){
mle.optim <- mle2(paretoNLL,start=list(?=-0.7),data=list(x=x),trace=TRUE)
return(summary(mle.optim)@coef[1,1:4])
}
Run Code Online (Sandbox Code Playgroud)
适合分布并返回参数估计值,std.我的模型的错误,z值和p.我必须将此函数应用于size由因子组合定义的原始数据框的不同子集,pond,habitat,treatment,date为此,我使用ddply函数:
> mle.? <- ddply(size, .(pond,habitat,treatment,date),
summarise, ?=?.est(x=mass.wei))
Run Code Online (Sandbox Code Playgroud)
问题在于,通过这样做,我只能在新数据框中添加一列mle.?,我需要添加到 mle.?四个新列,每个列的输出?.est
基本上是这样的:
> mle.?
pond habitat treatment date estimate std. error z value Pr(z)
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
...
Run Code Online (Sandbox Code Playgroud)
到目前为止,我一直在为所需的每个输出编写不同的函数,但我想做一些代码经济...有没有办法一次性完成所有操作?
谢谢matteo
由于您已经具有汇总功能,因此无需另外使用该summarise功能.此外,它是可以一次返回多个输出.由于没有示例数据,这里应该清楚地说明如何执行此操作:
n = 20
set.seed(12345)
data = data.frame(cbind(pond=1:2, habitat=1:3, value = rnorm(n)))
Run Code Online (Sandbox Code Playgroud)
> ddply(data, .(habitat, pond), function(x) summary(x$value))
habitat pond Min. 1st Qu. Median Mean 3rd Qu. Max.
1 1 1 0.3706 0.5318 0.6078 0.6767 0.7528 1.1210
2 1 2 -0.9193 -0.6864 -0.4535 -0.1853 0.1817 0.8169
3 2 1 -0.8864 -0.5013 -0.1162 -0.1322 0.2448 0.6059
4 2 2 -0.2762 0.1550 0.4095 0.3131 0.5675 0.7095
5 3 1 -0.7505 -0.5173 -0.2842 -0.3813 -0.1967 -0.1093
6 3 2 -1.8180 -1.0750 -0.3316 -0.1107 0.7429 1.8170
Run Code Online (Sandbox Code Playgroud)