I have this function:
> λ.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])
}
that fit a distribution and retuns the parameter estimate, std. error, z value and p for my model.
I have to apply this function to different subsets of my original data frame size
defined by a combination of factor pond,habitat,treatment,date
, and to do this I'm using the ddply function:
> mle.λ <- ddply(size, .(pond,habitat,treatment,date),
summarise, λ=λ.est(x=mass.wei))
the problem is that, by doing this, I can only add one column a time to the new data frame mle.λ
, wereas I need to add to mle.λ
four new columns, one for each of the outputs of λ.est
basically something that look like this:
> mle.λ
pond habitat treatment date estimate std. error z value Pr(z)
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
...
So far I've been writing a different function for each output needed, but I'd like to do some code economy...is there any way to do it all in one go?
thanks matteo
Since you already have a summary function, you don't need to additionally use the summarise
function. Further, it is possible to return more than one output at once. Since there is no example data, here is one that should demonstrate clearly how to do this:
n = 20
set.seed(12345)
data = data.frame(cbind(pond=1:2, habitat=1:3, value = rnorm(n)))
> 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With