Tuesday, 6 August 2013

Appending results of dlply function to original table

Appending results of dlply function to original table

This question builds on the answer that Simon and James provided here:
http://stackoverflow.com/a/18082457/2647260 The dlply function worked well
to give me Y estimates within my data subsets. Now, my challenge is
getting these Y estimates and residuals back into the original data frame
to calculate goodness of fit statistics and for further analysis. I was
able to use cbind to convert the dlply output lists to row vectors, but
this doesn't quite work as the result is (sorry about the poor markdown).
model <- function(df){glm(Y~D+O+A+log(M), family=poisson(link="log"),
data=df)}
Modrpt <- ddply(msadata, "Dmsa", function(x)coef(model(x)))
Modest <- cbind(dlply(msadata, "Dmsa", function(x)fitted.values(model(x))))

Subset name | Y_Estimates
-------------------------
Dmsa 1 | c(4353.234, 234.34,...
Dmsa 2 | c(998.234, 2543.55,...
etc...
This doesn't really answer the mail, because I need to get the individual
Y estimates (separated by commas in the Y_estimates column of the Modest
data frame) into my msadata data frame.
Ideally, and I know this is incorrect, but I'll put it here for an
example, I'd like to do something like this:
msadata$Y_est <- cbind(dlply(msadata, "Dmsa",
function(x)fitted.values(model(x))))

If I can decompose the list into individual Y estimates, I could join this
to my msadata data frame by "Dmsa". I feel like this is very similar to
Michael's answer here (http://stackoverflow.com/a/18025667/2647260), but
something is needed to separate the list elements prior to employing
Michael's suggestion of join() or merge(). Any ideas?

No comments:

Post a Comment