2
votes

I have got a technical problem which, as it seems, I am not able to solve by myself. I ran an estimation with the mcmcglmm package. By results$Sol I get access to the estimated posterior distributions. Applying class() tells me that the object is of class "mcmc". Using as.data.frame() results in a nested data frame which contains other data frames (one data frame which contains many other data frames). I would like to rbind() all data frames within the main data frame in order to produce one data frame (or rather a vector) with all values of all posterior distributions and the name of the (secondary) data frame as a rowname., Any ideas? I would be grateful for every hint!

Update: I didn't manage to produce a useful data set for the purpose of stackoverflow, with all these sampling chains these data sets would be always too large. If you want to help me, please consider to run the following (exemplaric) model

require(MCMCglmm)
data(PlodiaPO)
result <- MCMCglmm(PO ~ plate + FSfamily, data = PlodiaPO, nitt = 50, thin = 2, burn = 10, verbose = FALSE)

result$Sol (an mcmc object) is where all the chains are stored. I want to rbind all chains in order to have a vector with all values of all posterior distributions and the variable names as rownames (or since no duplicated rownames are allowed, as an additional character vector).

1
as.data.frame(model1):Error in as.data.frame.default(model1) : cannot coerce class '"MCMCglmm"' into a data.frame`. How did you do that?Metrics
@Metrics I imagine they meant the $Sol component specifically, since this is class mcmc not MCMCglmmJoe
Type ? followed by the name of the function that generated results. Check the details section; usually, package documentaion explains how to access components of their specific objects.Ferdinand.kraft
@Metric: Joe is right, results$Sol is an "mcmc" object on which "as.data.frame" can be applied.chameau13
@Joe Please see my edited question! Thank you!chameau13

1 Answers

3
votes

I can't (using the example code from MCMCglmm) construct an example where as.data.frame(model$Sol) gives me a dataframe of dataframes. So although there's probably a simple answer I can't check it very easily.

That said, here's an example that might help. Note that if your child dataframes don't have the same colnames then this won't work.

# create a nested data.frame example to work on
a.df <- data.frame(c1=runif(10),c2=runif(10))
b.df <- data.frame(c1=runif(10),c2=runif(10))
full.df <- data.frame(1:10)
full.df$a <- a.df
full.df$b <- b.df
full.df <- full.df[,c("a","b")]

# the solution
res <- do.call(rbind,full.df)

EDIT

Okay, using your new example,

require(MCMCglmm) 
data(PlodiaPO) 
result<- MCMCglmm(PO ~ plate + FSfamily, data=PlodiaPO,nitt=50,thin=2,burn=10,verbose=FALSE)
melt(do.call(rbind,(as.data.frame(result$Sol))))