Thanks to Hadley’s plyr package ddply function we can take a dataframe, break it down into subdataframes by factors, send each to a function, and then combine the function results for each subdataframe into a new dataframe.
But what if the function returns an object of a class like glm or in my case, a c(“glm”, “lm”). Then, these can’t be combined into a dataframe can they? I get this error instead
Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class 'c("glm", "lm")' into a data.frame
Is there some more flexible data structure that will accommodate all the complex glm class results of my function calls, preserving the information regarding the dataframe subsets?
Or should this be done in an entirely different way?
Just to expand my comment:
plyrhas set of functions to combine input and output type. So when you function returns something inconvertible todata.frameyou should uselistas output. So instead of usingddplyusedlply.When you want to do something on each model and convert results to
data.framethenldplyis the key.Lets create some models using
dlplyIt gives
listof threelmmodels.Using
ldplyyou could create adata.frame, e.g.with predictions of each model:
with statistics to each model: