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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
"read.yesno" <-
function (string, default=TRUE)
{
wrd <- ifelse(default, " (Y/n)?\n:", " (y/N)?\n:")
cat("\n", string, wrd, sep = "")
ans <- readline()
val <- if (default)
pmatch(ans, c("no","NO"), nomatch=0) == 0
else
pmatch(ans, c("yes","YES"), nomatch=0) != 0
return(val)
}
"change.tfoption" <-
function (string, option)
{
current.value <- coda.options(option)
if (!is.logical(current.value))
stop("Invalid option: must take logical values")
new.value <- read.yesno(string, current.value)
if (new.value != current.value) {
arg <- list(new.value)
names(arg) <- option
coda.options(arg)
}
return()
}
"coda.options" <-
function (...)
{
## Set and display coda options
single <- FALSE
copt <- if (exists(".Coda.Options", frame = 1)) {
if (is.R())
get(".Coda.Options", pos=1)
else
get(".Coda.Options")
}
else {
.Coda.Options.Default
}
if (nargs() == 0) {
return(copt)
}
else {
args <- list(...)
if (length(args) == 1) {
if (is.list(args[[1]]))
args <- args[[1]]
else if (is.null(names(args)))
single <- TRUE
}
}
if (is.null(names(args))) {
## Display options
args <- unlist(args)
value <- vector("list", length(args))
names(value) <- args
for (v in args) if (any(v == names(copt)))
value[v] <- copt[v]
if (single)
return(value[[1]])
else return(value)
}
else {
## Set options
oldvalue <- vector("list", length(args))
names(oldvalue) <- names(args)
if (any(names(args) == "default") && args$default ==
TRUE)
copt <- .Coda.Options.Default
for (v in names(args)) if (any(v == names(copt))) {
oldvalue[v] <- copt[v]
if (is.null(args[[v]]))
copt[v] <- list(NULL)
else if (mode(copt[[v]]) == mode(args[[v]]))
copt[v] <- args[v]
}
if (is.R())
assign(".Coda.Options", copt, pos=1)
else
assign(".Coda.Options", copt)
invisible(oldvalue)
}
}
"multi.menu" <- function (choices, title, header, allow.zero = TRUE)
{
## Select more than one value from a menu
##
if (!missing(title))
cat(title, "\n\n")
mat <- matrix(c(1:length(choices), choices), ncol = 2)
if (!missing(header)) {
if (length(header) == 2)
mat <- rbind(header, mat)
else stop("header is wrong length")
}
cat(paste(format(mat[, 1]), format(mat[, 2])), sep = "\n")
repeat {
cat("\nEnter relevant number(s), separated by commas",
"Ranges such as 3:7 may be specified)", sep = "\n")
if (allow.zero)
cat("(Enter 0 for none)\n")
ans <- scan(what = character(), sep = ",", strip.white = TRUE,
nlines = 1, quiet = TRUE)
if (length(ans) > 0) {
out <- numeric(0)
for (i in 1:length(ans)) {
nc <- nchar(ans[i])
wrd <- substring(ans[i], 1:nc, 1:nc)
colons <- wrd == ":"
err <- any(is.na(as.numeric(wrd[!colons]))) |
sum(colons) > 1 | colons[1] | colons[nc]
if (err) {
cat("Error: you have specified a non-numeric value!\n")
break
}
else {
out <- c(out, eval(parse(text = ans[i])))
if (min(out) < ifelse(allow.zero, 0, 1) | max(out) >
length(choices) | (any(out == 0) & length(out) >
1)) {
err <- TRUE
cat("Error: you have specified variable number(s) out of range!\n")
break
}
}
}
if (!err)
break
}
}
return(out)
}
"read.and.check" <-
function (message = "", what = numeric(), lower, upper, answer.in, default)
{
## Read data from the command line and check that it satisfies
## certain conditions. The function will loop until it gets
## and answer satisfying the conditions. This entails extensive
## checking of the conditions to make sure they are consistent
## so we don't end up in an infinite loop.
have.lower <- !missing(lower)
have.upper <- !missing(upper)
have.ans.in <- !missing(answer.in)
have.default <- !missing(default)
if (have.lower | have.upper) {
if (!is.numeric(what))
stop("Can't have upper or lower limits with non numeric input")
if (have.lower && !is.numeric(lower))
stop("lower limit not numeric")
if (have.upper && !is.numeric(upper))
stop("upper limit not numeric")
if ((have.upper & have.lower) && upper < lower)
stop("lower limit greater than upper limit")
}
if (have.ans.in) {
if (mode(answer.in) != mode(what))
stop("inconsistent values of what and answer.in")
if (have.lower)
answer.in <- answer.in[answer.in >= lower]
if (have.upper)
answer.in <- answer.in[answer.in <= upper]
if (length(answer.in) == 0)
stop("No possible response matches conditions")
}
if (have.default) {
if (mode(default) != mode(what))
stop("inconsistent values of what and default")
if (have.lower && default < lower)
stop("default value below lower limit")
if (have.upper && default > upper)
stop("default value above upper limit")
if (have.ans.in && !any(answer.in == default))
stop("default value does not satisfy conditions")
}
err <- TRUE
while (err) {
if (nchar(message) > 0) {
cat("\n", message, "\n", sep = "")
if (have.default)
cat("(Default = ", default, ")\n", sep = "")
}
repeat {
cat("1:")
ans <- readline()
if (length(ans) == 1 && nchar(ans) > 0)
break
else if (have.default) {
ans <- default
break
}
}
if (is.numeric(what)) {
err1 <- TRUE
ans <- as.numeric(ans)
message <- "You must enter a number"
if (is.na(ans))
NULL
else if ((have.lower & have.upper) && (ans < lower |
ans > upper))
message <- paste(message, "between", lower, "and",
upper)
else if (have.lower && ans < lower)
message <- paste(message, ">=", lower)
else if (have.upper && ans > upper)
message <- paste(message, "<=", upper)
else err1 <- FALSE
}
else err1 <- FALSE
if (have.ans.in) {
if (!is.na(ans) && !any(ans == answer.in)) {
message <- paste("You must enter one of the following:",
paste(answer.in, collapse = ","))
err2 <- TRUE
}
else err2 <- FALSE
}
else err2 <- FALSE
err <- err1 | err2
}
return(ans)
}
".Coda.Options.Default" <-
list(trace = TRUE,
densplot = TRUE,
lowess = FALSE,
combine.plots = TRUE,
bandwidth = function (x)
{
x <- x[!is.na(x)]
1.06 * min(sd(x), IQR(x)/1.34) * length(x)^-0.2
},
digits = 3,
quantiles = c(0.025, 0.25, 0.5, 0.75, 0.975),
frac1 = 0.1,
frac2 = 0.5,
q = 0.025,
r = 0.005,
s = 0.95,
combine.stats = FALSE,
combine.corr = FALSE,
halfwidth = 0.1,
user.layout = FALSE,
gr.bin = 10,
geweke.nbin = 20,
gr.max = 50,
data.saved = TRUE
)
|