You can use an anonymous function in your call to ddply
e.g.
require(plyr)
data(iris)
model <- function(df){
lm( Petal.Length ~ Sepal.Length + Sepal.Width , data = df )
}
ddply( iris , "Species" , function(x) fitted.values( model(x) ) )
This has the advantage that you can also, without rewriting your model
function, get thecoef
values by doing
ddply( iris , "Species" , function(x) coef( model(x) ) )
As @James points out, this will fall down if you have splits of unequal size, better to use dlply
which puts the result of each subset in it's own list element.
(I make no claims for statistical relevance or correctness of the example model - it is just an example)