glmmBUGS {glmmBUGS} | R Documentation |
Creates ragged arrays, writes a model file, and generates sensible starting estimates.
glmmBUGS(formula, data, effects, modelFile = "model.bug", initFile = "getInits.R", family = c("bernoulli", "binomial", "poisson", "gaussian"), spatial=NULL, spatialEffect = NULL)
formula |
A formula for the fixed effects portion of the model |
data |
A data frame containing the response, covariates, and group membership |
effects |
A vector of character strings containing the grouping levels, from most general to most specific |
modelFile |
File for saving the bugs model |
initFile |
File for saving the function for generating initial values |
family |
distribution of responses |
spatial |
Spatial adj matrix, in forms of ploygon, nb object or a list of adj and num |
spatialEffect |
spatial variable from data |
Consider the following model, where Y_ijk is the number of absences from individual k from class j in school k.
Y_ijk ~ Poisson(mu_i)
log(mu_i) = intercept + age_ijk beta + classSize_ij alpha + schoolCategory_i gamma + U_i + V_ij
U_i ~ N(0, sigma^2)
V_ij ~ N(0, nu^2)
Here there are covariates which apply to each of the three levels, and random effects at the school and class level. If data
is a data frame with one line per individual, the following would impliment this model:
glmmBUGS(data, effects=c("school","class"), covariates = list(school="schoolCategory", class="classSize", observations="age"), observations = "absences"), family="poisson")
To aid in convergence, the bugs model is actually the following:
log(mu_i) = age_ijk beta + V_ij
V_ij ~ N(U_i + classSize_ij alpha , nu^2)
U_i ~ N(intercept + schoolCategory_i gamma, sigma^2)
and the funciton restoreParams
subtracts the means from the random effects to restore the original set of equations.
glmmBUGS
calls the following functions:
getDesignMatrix
winBugsRaggedArray
glmmPQLstrings
writeBugsModel
getStartingValues
startingFunction
Type glmmBUGS
on the R command line to see the source code, it provides a good summary of the roles of the various functions in the glmmBUGS
package.
Returns a list with the ragged array, from winBugsRaggedArray
, and the list of starting values from getStartingValues
. Writes a model file and an initial value function. Note that the initial value function in initFile
will look for an object called startingValues
, which does not exist as this is part of a list. Either create startingValues <- result$startingValues
or edit initFile
.
You are strongly encouraged to modify the model file and the intial value function file prior to using them.
glmmBUGS uses the inprod2
function, which isn't implimented in OpenBugs, the model file will have to be modified for use with OpenBUGS.
Patrick Brown, patrick.brown@utoronto.ca
"Handling unbalanced datasets" in the "Tricks: Advanced Use of the BUGS Language" section of the bugs manual, at http://mathstat.helsinki.fi/openbugs/data/Docu/Tricks.html#HandlingUnbalancedDatasets
winBugsRaggedArray
, glmmPQLstrings
, writeBugsModel
, getStartingValues
, startingFunction
,bugs
library(nlme) data(Muscle) muscleRagged = glmmBUGS(conc ~ length, data=Muscle, effects="Strip", family="gaussian") startingValues = muscleRagged$startingValues ## Not run: source("getInits.R") require(BRugs) 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="openbugs") ## End(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(model, popdata, casedata) ontario = ontario@data[,c("CSDUID","Observed","logExpected")] library(spdep) popDataAdjMat = poly2nb(popdata,row.names=as.character(popdata[["CSDUID"]])) ## End(Not run) data(popDataAdjMat) data(ontario) forBugs = glmmBUGS(formula=Observed + logExpected ~ 1, effects="CSDUID", family="poisson", spatial=popDataAdjMat, data=ontario) startingValues = forBugs$startingValues source("getInits.R") ## Not run: ontarioResult = 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) ## End(Not run) data(ontarioResult) ontarioParams = restoreParams(ontarioResult, forBugs$ragged) ontarioSummary = summaryChain(ontarioParams) ## Not run: ontario = mergeBugsData(popdata, ontarioSummary) # get rid of CSDUID 3551034 and 3551035 because theyre only neighbours # of each other and they have no data ontario = ontario[! ontario$CSDUID %in% c("3551034", "3551035"),] spplot(ontario, "FittedRateCSDUID.mean") # posterior probability of having 4x excess risk postProb = apply(exp(ontarioParams$FittedCSDUID), 3, function(x) mean(x>4)) ontario = mergeBugsData(ontario, postProb, newcol="postProb4", by.x="CSDUID") spplot(ontario, "postProb4") ## End(Not run)