I am using nested loops to perform multiple runs of MCMCglmm in R, outputting the posterior distributions in to data frames. The outer loop performs the MCMC process, generating posterior distributions, and then the inner loop lifts each of these in to separate data frames (where F is the number of posterior distributions in the fixed effects of each run of the chain, and n is the number of randomisations to be performed); therefore, there will be F (in this case, 4) data frames, each with n columns.
for(i in 1:n){
# Randomise
MortR = data.frame(Year, Mortalities)
MortR[,2] = sample(MortR[,2], replace = F)
DF1 = merge(DF1, MortR, by = "Year")
DF1$Mort_R = DF1[,9]
DF1[,9] = NULL
prior1 = list(G = list( G1 = list(V = 1, nu = 1, alpha.mu = 0, alpha.V = 1000)
),
R = list(V = 1, nu = 0.002))
# Chain
assign("chainX",paste0("chain",i))
chainX = MCMCglmm(Life ~ Sex*Mort_R - 1,
random = ~Pop:Year,
rcov = ~units,
nitt = nitt,
burnin = burn,
thin = thin,
prior = prior1,
pr = T,
family = "gaussian",
start = list(QUASI = FALSE),
data = DF1)
for(j in 1:F){
assign(paste0("chainSol",j), chainX$Sol[,j])
A = as.vector(get(paste0("chainSol",j)))
assign(paste0("F",j), A)
# Aim: get vector F*j* (or A) in to column *i* of dataframe chainSolDF_*j*
# ie.: chainSolDF_*j*[,*i*] = F*j*
#chainSolDF_1[,i] = as.vector(get(paste0("chainSol",j))) # Puts it in the columns of DF_1
#assign(paste0("chainSolDF_",j)[,i], A) # Incorrect number of dimensions
#assign(paste0("chainSolDF_",j)[,i], paste0("F",j)) # Error in paste0("chainSolDF_", j)[, i] : incorrect number of dimensions
#assign(paste0("chainSolDF_",j,"[,",i,"]"), paste0("F",j)) # Does not assign to the data frame
#paste0("chainSolDF_",j,"[,",i,"]") = get(paste0("F",j)) # Invalid first argument
}
#B = matrix(c(F1,F2,F3,F4), ncol = 4)
print(paste0("Randomisation ",i, " Complete"))
}
You can see some (this is just the "highlights") of the things I've tried (#) with descriptions of the errors they generate - all to no avail! Generally I've been looking at solutions for get()
, assign()
and $<-
. Having spent three days trying to fix this myself I'm turning to this community.
How can one assign a vector to a specific column and specific data frame, both of which are simultaneously specified within the internal nested loop?
"Aim: get vector
Fj
(or A) in to columni
of dataframechainSolDF_j