0

Maybe someone here with more expirience with mcmc objects can help me out.

Problem: I have a list with 20+ mcmc objects stored. I need to assign variable names for each of the mcmc object.

I have another list with all the variables names for each mcmc stored (as columns in dataframes).

I can do this individually, using the "varnames" function from the coda package, like this:

Being "votes" my dataframe list and "posterior" my mcmc object list..

names <- votes[[1]]$legis # extracts the names variable for the first dataframe on the list.
varnames(posterior[[1]]) <- names # assings the extracted names as variable names fo the first mcmc object on the list.

However, rather than doing this one by one, I would like to do it all at once. I've tried the following code...

p2 <- lapply(posterior, function(x)varnames(posterior[[x]]) <- names)

But i get Error in *tmp*[[x]] : Recursive indexing failed at level 2 . I've tried some variations of that line, but I'm having a hard time understanding how to properly index this, or how to do what I want to do at all.

I know its kind of an specific problem, but maybe someone here can give me a hint or something.

Thanks in advance for the help. Sorry I can't provide some data, but it's kinda difficult to get workable samples of this.

Regards, Federico

CHP
  • 16,981
  • 4
  • 38
  • 57
Federico C
  • 125
  • 2
  • 11
  • 1
    `lapply(posterior, function(x) varnames(x) <- names)` should work, otherwise you are indexing `posterior` by `x` when x is already `posterior[[1]]` or posterior[[2]]` – mnel Mar 07 '13 at 02:03
  • or `lapply(posterior, setNames, names)`, I think. – Ben Bolker Mar 07 '13 at 02:08
  • Mnel, i´ve tried your approach but i get a dim error. Error in dimnames(x)[[2]] <- value : "length of 'dimnames' [2] not equal to array extent" – Federico C Mar 07 '13 at 22:48

1 Answers1

1

You don't need to do any lapply.

try names(posterior) <- votes[[1]]$legis

CHP
  • 16,981
  • 4
  • 38
  • 57
  • Thanks, but that doesn´t work since all the mcmc objects have different lenghts. I need match the names column of df.1 with the varible names of mcmc.1 (the lenghts of each pair matches). – Federico C Mar 07 '13 at 22:41