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()). Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
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.