Ravi Varadhan
2002-Nov-25 21:14 UTC
[R] How top print intermediate values from inside a function?
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Ravi Varadhan
2002-Nov-25 22:37 UTC
[R] How top print intermediate values from inside a function?
Hi: I tried both "print(mh.new)" and "cat(mh.new)", but the results are displayed only after all the computations inside the function are completed. Is there a way to display the intermediate variable immediately after it is evaluated? thanks again, Ravi. ----- Original Message ----- From: rossini at blindglobe.net (A.J. Rossini) Date: Monday, November 25, 2002 5:15 pm Subject: Re: [R] How top print intermediate values from inside a function?> >>>>> "ravi" == Ravi Varadhan <rvaradha at jhsph.edu> writes: > > > ravi> In R, how do I display some intermediate results > calculated in a "for" > ravi> loop within a function? For example, in the attached > code, how do I > ravi> get it to print the intermediate variable "mh.new" for > each simulation, > ravi> when I call the function "MHsim.ind"? > > other than print(mh.new) or cat(mh.new) ? > > -- > A.J. Rossini Rsrch. Asst. Prof. of > BiostatisticsU. of Washington Biostatistics > rossini at u.washington.edu FHCRC/SCHARP/HIV Vaccine Trials > Net rossini at scharp.org-------------- > http://software.biostat.washington.edu/ ---------------- > FHCRC: M: 206-667-7025 (fax=4812)|Voicemail is pretty sketchy/use > EmailUW: Th: 206-543-1044 (fax=3286)|Change last 4 digits of > phone to FAX > (my tuesday/wednesday/friday locations are completely unpredictable.) > > > >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Dear R experts, Is there any function in R that I can calculate the permutation lists? I would like to know if there is any generic one for finding all the permutation of n choose k? For example: perms <- list(1:2) perms <- add.ttt(3:4, perms) should give me > perms [[1]] [1] 3 4 1 2 [[2]] [1] 3 1 4 2 [[3]] [1] 3 1 2 4 and perms <- add.ttt(5:6, perms) should give me: [[1]] [1] 5 6 3 4 1 2 [[2]] [1] 5 3 6 4 1 2 [[3]] [1] 5 3 4 6 1 2 [[4]] [1] 5 3 4 1 6 2 [[5]] [1] 5 3 4 1 2 6 [[6]] [1] 5 6 3 1 4 2 [[7]] [1] 5 3 6 1 4 2 [[8]] [1] 5 3 1 6 4 2 [[9]] [1] 5 3 1 4 6 2 [[10]] [1] 5 3 1 4 2 6 [[11]] [1] 5 6 3 1 2 4 [[12]] [1] 5 3 6 1 2 4 [[13]] [1] 5 3 1 6 2 4 [[14]] [1] 5 3 1 2 6 4 [[15]] [1] 5 3 1 2 4 6 This example is just for those with k=2. How would I calculate those for any k? Thanks in advance for your help. Pauline -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Ravi Varadhan
2002-Nov-26 15:59 UTC
[R] How top print intermediate values from inside a function?
Dear R-group: I would like to thank Peter Dalgaard and James Holtman for their help. After turning the "buffered output" option off under the "Misc" menu (in Windows), the results were displayed immediately. thanks very much, Ravi. ----- Original Message ----- From: Peter Dalgaard BSA <p.dalgaard at biostat.ku.dk> Date: Monday, November 25, 2002 7:12 pm Subject: Re: [R] How top print intermediate values from inside a function?> Ravi Varadhan <rvaradha at jhsph.edu> writes: > > > Hi: > > > > I tried both "print(mh.new)" and "cat(mh.new)", but the results > are > > displayed only after all the computations inside the function > are > > completed. Is there a way to display the intermediate variable > > immediately after it is evaluated? > > Windows, right? There's a checkbox for "buffered output" or something > like that in the menus. Uncheck it. > > -- > O__ ---- Peter Dalgaard Blegdamsvej 3 > c/ /'_ --- Dept. of Biostatistics 2200 Cph. N > (*) \(*) -- University of Copenhagen Denmark Ph: (+45) > 35327918~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: > (+45) 35327907 >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._