jagsresults: Print results for specified rjags or mcmc.list parameters

Description Usage Arguments Value See Also Examples

View source: R/jagsresults.R

Description

Prints results for rjags or mcmc.list parameters, which are specified with a regular expression, or by exact name.

Usage

1
2
jagsresults(x, params, regex = FALSE, invert = FALSE, probs = c(0.025,
  0.25, 0.5, 0.75, 0.975), signif, ...)

Arguments

x

The rjags, rjags.parallel, or mcmc.list object for which results will be printed.

params

Character vector or a regular expression pattern. The parameters for which results will be printed (unless invert is FALSE, in which case results for all parameters other than those given in params will be returned). If regex is FALSE, only those parameters that match params exactly will be returned. If regex is TRUE, param should be a character string giving the regular expression pattern to be matched.

regex

Logical. If regex is TRUE, then param is expected to be a single string giving a text pattern to be matched. Parameters with names matching the pattern will be returned (unless invert is TRUE, which results in all parameters that do not match the pattern being returned). Text pattern matching uses regular expressions (regex).

invert

Logical. If invert is TRUE, only those parameters that do not match elements of params will be returned.

probs

A numeric vector of probabilities within range [0, 1], representing the sample quantiles to be calculated and returned.

signif

If supplied, all columns other than n.eff will have their values rounded such that the most extreme value has the specified number of significant digits.

...

Additional arguments accepted by grep, e.g. perl=TRUE, to allow look-around pattern matching.

Value

A matrix with one row for each parameter that matches param, and one column for each of mean, sd, percentiles 2.5, 25, 50, 75, and 97.5. In addition, if x is an rjags object, columns for Rhat and neff are returned.

See Also

rhats for simplified rhat output, rearray for recovering array structure of vector-valued parameters.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
## Data
N <- 100
temp <- runif(N)
rain <- runif(N)
wind <- runif(N)
a <- 0.13
beta.temp <- 1.3
beta.rain <- 0.86
beta.wind <- -0.44
sd <- 0.16
y <- rnorm(N, a + beta.temp*temp + beta.rain*rain + beta.wind*wind, sd)
dat <- list(N=N, temp=temp, rain=rain, wind=wind, y=y)

### jags example
library(R2jags)

## Model
M <- function() {
  for (i in 1:N) {
    y[i] ~ dnorm(y.hat[i], sd^-2)
    y.hat[i] <- a + beta.temp*temp[i] + beta.rain*rain[i] + beta.wind*wind[i]
    resid[i] <- y[i] - y.hat[i]
  }
  sd ~ dunif(0, 100)
  a ~ dnorm(0, 0.0001)
  beta.temp ~ dnorm(0, 0.0001)
  beta.rain ~ dnorm(0, 0.0001)
  beta.wind ~ dnorm(0, 0.0001)
}

## Fit model
jagsfit <- jags(dat, inits=NULL, 
                parameters.to.save=c('a', 'beta.temp', 'beta.rain', 
                                     'beta.wind', 'sd', 'resid'), 
                model.file=M, n.iter=10000)

## Output
# model summary
jagsfit

# Results for beta.rain only
jagsresults(x=jagsfit, param='beta.rain')

# Results for 'a' and 'sd' only
jagsresults(x=jagsfit, param=c('a', 'sd'))
jagsresults(x=jagsfit, param=c('a', 'sd'), 
            probs=c(0.01, 0.025, 0.1, 0.25, 0.5, 0.75, 0.9, 0.975))

# Results for all parameters including the string 'beta'
jagsresults(x=jagsfit, param='beta', regex=TRUE)

# Results for all parameters not including the string 'beta'
jagsresults(x=jagsfit, param='beta', regex=TRUE, invert=TRUE)

# Note that the above is NOT equivalent to the following, which returns all
# parameters that are not EXACTLY equal to 'beta'.
jagsresults(x=jagsfit, param='beta', invert=TRUE)

# Results for all parameters beginning with 'b' or including 'sd'.
jagsresults(x=jagsfit, param='^b|sd', regex=TRUE)

# Results for all parameters not beginning with 'beta'.
# This is equivalent to using param='^beta' with invert=TRUE and regex=TRUE
jagsresults(x=jagsfit, param='^(?!beta)', regex=TRUE, perl=TRUE)

## mcmc.list example
library(rjags)
simgrowth$model$recompile()
simgrowth_mcmclist <- 
  coda.samples(simgrowth$model, 
               setdiff(simgrowth$parameters.to.save, 'deviance'), 1000)
is(simgrowth_mcmclist)
jagsresults(simgrowth_mcmclist, c('b[3]', 'log.r[2,1]'))
jagsresults(simgrowth_mcmclist, c('b[3]', 'log.r[2,1]'), invert=TRUE)
jagsresults(simgrowth_mcmclist, c('lambda[3,1]', 'sd.b'))
jagsresults(simgrowth_mcmclist, c('lambda\\[3,1\\]|sd\\.'), regex=TRUE)

johnbaums/jagstools documentation built on May 19, 2019, 3:03 p.m.