Quickly adapt starting values in MCMC using paste()

[This article was first published on joint posterior, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Waiting for convergence of MCMC models can take some time, therefore it may be a good idea to use better starting values. Using paste, one can quickly convert any (parameter) vector in the workspace into a R-style vector (with c()).

Here’s a function that takes any vector (for example 1:10) and turns it into c(1,2,3,4,5,6,7,8,9,10). Now it’s easy to quickly adapt starting values in your code.

[update 1]
Well, of course we can do the same with matrices.


Rvector = function(vec) {
out = paste("c(",paste(vec,sep="",collapse=","),")",sep="")
cat("\n",out,"\n","\n")
return(out)
}
Rmatrix = function(mat) {
out = paste("matrix(", Rvector(as.matrix(mat)), "," ,nrow(mat), ",", ncol(mat), ")", sep="")
cat("\n",out,"\n","\n")
return(out)
}
view raw gistfile1.r hosted with ❤ by GitHub

To leave a comment for the author, please follow the link and comment on their blog: joint posterior.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)