[R] How top print intermediate values from inside a function?
Ravi Varadhan
rvaradha at jhsph.edu
Mon Nov 25 22:14:38 CET 2002
Hi:
In R, how do I display some intermediate results calculated in a "for"
loop within a function? For example, in the attached code, how do I
get it to print the intermediate variable "mh.new" for each simulation,
when I call the function "MHsim.ind"?
thanks for any help,
Ravi.
####################################################################
MHsim.ind <- function(nsim=1000,start=0,spread=1,likfn=bpllkd){
# Independence Metropolis algorithm with candidate density a
#multivariate Student t-distribution with 1 degree of freedom, whose
#mean and variance are taken to be MLE and its covariance matrix
mh.0 <- start
npar <- length(start)
mh.sim <- matrix(0,nrow=nsim,ncol=npar)
i <- 0
while (i < nsim){
mh.new <- mvrnorm(n=1,mu=rep(0,npar),Sigma=spread)/
sqrt(rchisq(n=1,df=1)/1) + start
ratio <- exp(-likfn(mh.new)+ likfn(mh.0))* dmvt(mh.0, mu=start,
sigma=spread)/dmvt(mh.new, mu=start, sigma=spread)
if (is.nan(ratio)) next
i <- i+1
if (runif(1) < ratio) {mh.0 <- mh.sim[i,] <- mh.new}
else mh.sim[i,] <- mh.0
}
mh.sim
}
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list