models/individual_models/w_st.R

model {
  # Define likelihood model for data:
  # Carbapenem resistance in hospital (gp, volunteer, and outpatient) samples
  # is Bernoulli distributed with probability wc.prob (gp.prob, v.prob,
  # and o.prob)
  for (p in 1:N_patients)
  {
    h_resist[p] ~ dbern(wst.prob[ward[h_sample_GUID[p]],
                                 sample_type[h_sample_GUID[p]]])
  }

  for (gp in 1:N_gp)
  {
    gp_resist[gp] ~ dbern(gp.prob)
  }

  for (v in 1:N_volunteers)
  {
    v_resist[v] ~ dbern(v.prob)
  }

  for (o in 1:N_outpatients)
  {
    o_resist[o] ~ dbern(o.prob)
  }

  # ------------------------

  # Define the priors:
  # Prior distribution for ward.effect (log-odds for each hospital ward). Sample
  # different ward.effect from normal distribution for each hospital ward and
  # convert to a probability). Since there is only one response variable, put
  # intercept here.
  for (w in hosp_wards)
  {
    ward.effect[w] ~ dnorm(intercept, tau.ward)

    for (st in sampletypes)
    {
      wst.effect[w,st] ~ dnorm(ward.effect[w], tau.wst)
      logit(wst.prob[w,st]) <- wst.effect[w,st]
    }
  }

  # equivalent to ward.effect
  nh.effect ~ dnorm(intercept, tau.ward)
  logit(nh.prob) <- nh.effect

  # equivalent to wc.effect
  gp.effect ~ dnorm(nh.effect, tau.wst)
  logit(gp.prob) <- gp.effect

  v.effect ~ dnorm(nh.effect, tau.wst)
  logit(v.prob) <- v.effect

  o.effect ~ dnorm(nh.effect, tau.wst)
  logit(o.prob) <- o.effect

  # ------------------------

  # Prior value for intercept
  intercept ~ dnorm(0, 0.001)

  # Prior values for precision
  tau.ward ~ dgamma(0.001, 0.001)
  tau.wst ~ dgamma(0.001, 0.001)

  # Convert precisions to sd
  sd.ward <- 1/sqrt(tau.ward)
  sd.wst <- 1/sqrt(tau.wst)

  #monitor# full.pd, dic, deviance, gp.prob, v.prob, o.prob, sd.ward, sd.sc
}
soniamitchell/SpARKjags documentation built on May 5, 2022, 12:09 p.m.