Changeset 8a4804f in tol


Ignore:
Timestamp:
Nov 3, 2009, 5:30:49 PM (17 years ago)
Author:
Víctor de Buen <vdebuen@…>
Branches:
1941-docker-image-to-test, devel, master
Children:
ac73d05
Parents:
bca7675
Message:

Preparing for call to Gelman diagnostics of R-Coda package
Related to #638

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@1586 be4846ae-f051-483a-831f-b64e7d08527f

Location:
tol/stdlib/math/stat/models/bayesian/diagnosis
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • tol/stdlib/math/stat/models/bayesian/diagnosis/_rcoda.tol

    rbca7675 r8a4804f  
    4848  Text _tmp.mcmc.bbm = _tmpdir+"/"+"mcmc%S.bbm";
    4949  Text _tmp.raftery.bbm = _tmpdir+"/"+"raftery%S.bbm";
     50  Text _tmp.gelman.bbm = _tmpdir+"/"+"gelman%S.bbm";
    5051  Text _tmp.varstat.bbm = _tmpdir+"/"+"varstat%S.bbm";
    5152  Text _tmp.varquant.bbm = _tmpdir+"/"+"varquant%S.bbm";
    … …  
    174175  };
    175176 
    176  
     177  //////////////////////////////////////////////////////////////////////////////
     178  Text _.autodoc.member.gelman.diag =
     179  "Function RCODA::gelman.diag is a TOL port to R function "
     180  "gelman.diag {coda} \n"
     181  "\n"
     182  "URL: http://cran.r-project.org/doc/packages/coda.pdf\n"
     183  "\n"         
     184  "Description\n"
     185  "The ‘potential scale reduction factor’ is calculated for each variable in "
     186  "x, together with upper and lower confidence limits. Approximate convergence "
     187  "is diagnosed when the upper limit is close to 1. For multivariate chains, "
     188  "a multivariate value is calculated that bounds above the potential scale "
     189  "reduction factor for any linear combination of the (possibly transformed) "
     190  "variables.\n"
     191  "The confidence limits are based on the assumption that the stationary "
     192  "distribution of the variable under examination is normal. Hence the "
     193  "‘transform’ parameter may be used to improve the normal approximation.\n" 
     194  "\n"         
     195  "Usage examples\n"
     196  "\n"
     197  "  Matrix RCODA::gelman.diag(data, {[[\n"
     198  "    Real verbose=FALSE, \n"
     199  "    Real varByCol=TRUE, \n"
     200  "    Real confidence=0.95, \n"
     201  "    Real transform=FALSE, \n"
     202  "    Real autoburnin=TRUE]]})\n"
     203  "\n"
     204  "  Matrix RCODA::gelman.diag(data, {[[\n"
     205  "    Real varByCol=FALSE, \n"
     206  "    Real confidence=0.95]]})\n"
     207  "\n"
     208  "  Matrix RCODA::gelman.diag(data,Empty)\n"
     209  "\n"
     210  "Arguments\n"
     211  "  data: an mcmc object that could be a Set variable wich two or more "
     212          "elements are Matrix or a Text one with the path to a BBM file\n"
     213  "  optArgs: supplies optional arguments:\n"
     214  "    Real verbose: If TRUE some traces will be send to standard output"
     215  "    Real varByCol: If TRUE then each column contains the Markov Chain of a "
     216                     "variable and each row has a simulation. In other case "
     217                     "each row is a variable and each column is a simulation"
     218                     "Default value is TRUE\n"
     219  "    Real confidence: the coverage probability of the confidence interval "
     220                        "for the potential scale reduction factor\n"
     221  "    Real transform: a logical flag indicating whether variables in x "
     222                      "should be transformed to improve the normality of "
     223                      "the distribution. If set to TRUE, a log transform or "
     224                      "logit transform, as appropriate, will be applied.\n"
     225  "    Real autoburnin: a logical flag indicating whether only the second half "
     226                       "of the series should be used in the computation. If set "
     227                       "to TRUE (default) and start(x) is less than end(x)/2 "
     228                       "then start of series will be adjusted so that only "
     229                       "second half of series is used.\n"
     230  "\n"         
     231  "Returns ? (Uncompleted)"
     232  "\n"
     233  "Acknowledgements:\n"+ _coda_citation(0);
     234  Matrix gelman.diag(Anything data, Set optArgs)
     235  //////////////////////////////////////////////////////////////////////////////
     236  {
     237    Real verbose    = getOptArg(optArgs,"verbose",    FALSE);
     238    Real varByCol   = getOptArg(optArgs,"varByCol",   TRUE);
     239    Real confidence = getOptArg(optArgs,"confidence", 0.95);
     240    Real transform  = getOptArg(optArgs,"transform",  FALSE);
     241    Real autoburnin = getOptArg(optArgs,"autoburnin", TRUE);
     242
     243    Set args.bbm = [[_mat.in(data)]];
     244    Set result.names = [[ _tmp.gelman.bbm ]];
     245   
     246    Text script =
     247      "source('"+_tolcoda.R+"'); gelman.bbm('"+args.bbm[1][1]+"','"
     248      +_tmp.raftery.bbm+"',"
     249      <<FormatReal(verbose,     "%.0lg")+","
     250      <<FormatReal(varByCol,    "%.0lg")+","
     251      <<FormatReal(confidence,  "%.15lg")+","
     252      <<FormatReal(transform,   "%.0lg")+","
     253      <<FormatReal(autoburnin,  "%.0lg")
     254      <<")";
     255    Rapi::RunRScript(script, args.bbm, "",
     256                     result.names,
     257                     verbose)[1]
     258  };
     259   
    177260  Set _def.quantiles = [[0.025, 0.25, 0.5, 0.75, 0.975]];
    178261  //////////////////////////////////////////////////////////////////////////////
  • tol/stdlib/math/stat/models/bayesian/diagnosis/_tolcoda.R

    rbca7675 r8a4804f  
    7373}
    7474
     75
     76################################################################################
     77gelman.bbm <-
     78################################################################################
     79function(numChain, fin, fout, verbose=FALSE, varByCol=TRUE,
     80         confidence=0.95, transform=FALSE, autoburnin=TRUE) {
     81  mcmc <- list()
     82  for(i in 1:numChain) {
     83  mcmc[i] <- get.mcmc.bbm(paste(fin,i),verbose,varByCol,"gelman.diag")
     84  }
     85  result <- gelman.diag(mcmc,confidence,transform,autoburnin)
     86  if(verbose)
     87  {
     88    print(result)
     89  }
     90  write.bbm(fout, result$resmatrix)
     91}
     92
    7593################################################################################
    7694### RGA This is a wrapper for spectrum0 which returns NA if
Note: See TracChangeset for help on using the changeset viewer.