# NOT RUN {
library(nlme)
data(Muscle)
muscleRagged = glmmBUGS(conc ~ length, data=Muscle,
effects="Strip", family="gaussian",
modelFile='model.bug', reparam='Strip')
startingValues = muscleRagged$startingValues
# }
# NOT RUN {
# run with winbugs
source("getInits.R")
require('R2WinBUGS')
muscleResult = bugs(muscleRagged$ragged, getInits,
parameters.to.save = names(getInits()),
model.file="model.bug", n.chain=3, n.iter=1000,
n.burnin=100, n.thin=10, program="winbugs",
working.directory=getwd()
)
# a jags example
require('R2jags')
muscleResultJags = jags(
muscleRagged$ragged, getInits, parameters.to.save = names(getInits()),
model.file="model.bug", n.chain=3, n.iter=1000,
n.burnin=100, n.thin=10,
working.directory=getwd())
muscleParamsJags = restoreParams(
muscleResultJags$BUGSoutput,
muscleRagged$ragged)
checkChain(muscleParamsJags)
# }
# NOT RUN {
data(muscleResult)
muscleParams = restoreParams(muscleResult, muscleRagged$ragged)
summaryChain(muscleParams)
checkChain(muscleParams)
# a spatial example
# }
# NOT RUN {
library(diseasemapping)
data('popdata')
data('casedata')
model = getRates(casedata, popdata, ~age*sex)
ontario = getSMR(popdata, model, casedata)
ontario = ontario@data[,c("CSDUID","observed","logExpected")]
popDataAdjMat = spdep::poly2nb(popdata,
row.names=as.character(popdata[["CSDUID"]]))
data('popDataAdjMat')
data('ontario')
forBugs = glmmBUGS(formula=observed + logExpected ~ 1,
effects="CSDUID", family="poisson", spatial=popDataAdjMat,
spatialEffect="CSDUID",
data=ontario)
startingValues = forBugs$startingValues
source("getInits.R")
# find patrick's OpenBUGS executable file
if(Sys.info()['user'] =='patrick') {
obExec = system(
"find /store/patrick/ -name OpenBUGS",
TRUE)
obExec = obExec[length(obExec)]
} else {
obExec = NULL
}
bugsResult = bugs(forBugs$ragged, getInits,
parameters.to.save = names(getInits()),
model.file="model.bug", n.chain=3, n.iter=50, n.burnin=10,
n.thin=2,
program="winbugs", debug=T,working.directory=getwd())
data('ontarioResult')
ontarioParams = restoreParams(ontarioResult, forBugs$ragged)
ontarioSummary = summaryChain(ontarioParams)
# posterior probability of having 10x excess risk
postProb = apply(ontarioParams$FittedRCSDUID, 3, function(x) mean(x>log(10)) )
hist(postProb)
ontario = mergeBugsData(popdata, ontarioSummary)
spplot(ontario, "FittedRateCSDUID.mean")
ontario = mergeBugsData(ontario, postProb, newcol="postProb", by.x="CSDUID")
spplot(ontario, "postProb")
# }
# NOT RUN {
# geostatistical example
# }
# NOT RUN {
rongelap= read.table(url(
paste("http://www.leg.ufpr.br/lib/exe/fetch.php/",
"pessoais:paulojus:mbgbook:datasets:rongelap.txt",
sep="")
),header=TRUE
)
library('spdep')
coordinates(rongelap) = ~cX+cY
rongelap$logOffset = log(rongelap$time)
rongelap$site = seq(1, length(rongelap$time))
forBugs = glmmBUGS(
formula=counts + logOffset ~ 1, family="poisson",
data=rongelap@data, effects="site", spatial=rongelap,
priors=list(phisite="dgamma(100,1)"))
startingValues = forBugs$startingValues
startingValues$phi$site = 100
source("getInits.R")
rongelapResult = bugs(forBugs$ragged, getInits,
parameters.to.save = names(getInits()),
model.file="model.bug", n.chain=2, n.iter=20, n.burnin=4, n.thin=2,
program="winbugs", debug=TRUE,
working.directory=getwd())
data('rongelapResult')
rongelapParams = restoreParams(rongelapResult, forBugs$ragged)
checkChain(rongelapParams)
rongelapParams$siteGrid = CondSimuPosterior(rongelapParams, rongelap,
gridSize=100)
rongelapSummary=summaryChain(rongelapParams)
# plot posterior probabilities of being above average
image(rongelapSummary$siteGrid$pgt0)
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab