Learn R Programming

runjags (version 1.2.1-0)

read.winbugs: Extract Any Models, Data, Monitored Variables or Initial Values As Character Vectors from a Winbugs Type Textfile

Description

Read a user specified WinBUGS type textfile or character variable and extract any models, data, monitored variables or initial values as character vectors. Used by (auto)run.jags to interpret the input file(s) or strings.

Usage

read.winbugs(path)

Arguments

path
either a relative or absolute path to a textfile (including the file extension) containing a model in the JAGS language and possibly monitored variable names, data and/or initial values, or a character string of the same. May also be a vector of paths to

Value

  • A named list of 'model' containing the model description, 'data' containing the data given in the data block(s), 'autodata' containing data variables specified using '#data#' in the model block, 'inits' containing the initial values given in the initial value block(s), 'autoinits' containing initial value variables specified using '#inits#' in the model block, and 'monitor' containing the monitored variables specified in the monitor blocks and by using '#monitor#' within the model block. This function is specified primarily for WinBugs compatibility, so data blocks would normally contain the data in a list format rather than the code format that is allowed in JAGS to perform data transformations (see JAGS manual section 7.0.5). These JAGS format data blocks can be specified, and the function will attempt to differentiate the two types of data from the presence of syntactical cues such as square brackets, for loops, 'list' and .Dim structural assignments. If none of these are found, the data block is assumed to be a WinBugs type data block and is passed to JAGS as data. This behaviour can be over-ridden by inserting '#jagsdata#' or '#bugsdata#' into the data block as appropriate. More than one data block is allowed, and each will be differentiated independently.

See Also

run.jags

Examples

Run this code
# ALL SYNTAX GIVEN BELOW IS EQUIVALENT

# Use a modified WinBUGS text file with manual inits and manual data and
# a seperate monitor block (requires least modification from a WinBUGS
# file).  For compatibility with WinBUGS, the use of list() to enclose
# data and initial values is allowed and ignored, however all seperate
# variables in the data and inits blocks must be seperated with a line
# break (commas or semicolons before linebreaks are ignored).  'data{'
# and 'inits{' must also be added to WinBUGS textfiles so that the
# function can seperate data from initial values.  Iterative loops are
# allowed in data blocks but not in init blocks.  See also the differences
# in JAGS versus WinBUGS syntax in the JAGS help file.

# Contents of a textfile 'mymodel.bug':

model{

	for(i in 1:N){
		Count[i] ~ dpois(mean)
	}
	mean ~ dgamma(0.01, 100)
}

data{
list(Count <- c(1,2,3,4,5,6,7,8,9,10),
N <- 10)

}
inits{
list(
	mean <- 1)
}

inits{
list(
	mean <- 100)
}

monitor{
	mean
}

# end text file

read.winbugs('pathtofile/mymodel.bug')


# Use internal character variable, define monitors in the model, 
# use autodata and manual initial values:

string <- "
model{

	for(i in 1:N){
		Count[i] ~ dpois(mean) #data# Count, N
	}
	mean ~ dgamma(0.01, 100)
	#monitor# mean
}

inits{
	mean <- 1
}

inits{
	mean <- 100
}
"

read.winbugs(string)


# Use autoinits and a mixture of manual and autodata:
string <- "
model{

	for(i in 1:N){ 
		Count[i] ~ dpois(mean) #data# Count
	}
	mean ~ dgamma(0.01, 100) 
	#monitor# mean
	#inits# mean
}

data{

	N <- 10

}
"

read.winbugs(string)

Run the code above in your browser using DataLab