coraliewilliams / glmmTMB

glmmTMB
0 stars 1 forks source link

Weights in glmmTMB #1

Open coraliewilliams opened 1 year ago

coraliewilliams commented 1 year ago

How are weights specified as a default in glmmTMB function?

If weights are not specified within the function a vector of "1s" is set. Non-NULL weights can be used to indicate that different observations have different variances (with the values in weights being inversely proportional to the variances)


[......]
 if (is.null(weights)) weights <- rep(1, nobs)

  ## binomial family:
  ## binomial()$initialize was only executed locally
  ## yobs could be a factor -> treat as binary following glm
  ## yobs could be cbind(success, failure)
  ## yobs could be binary
  ## (yobs, weights) could be (proportions, size)
  ## On the C++ side 'yobs' must be the number of successes.
  if ( binomialType(family$family) ) {
    if (is.factor(yobs)) {
      ## following glm, ‘success’ is interpreted as the factor not
      ## having the first level (and hence usually of having the
      ## second level).
      yobs <- pmin(as.numeric(yobs)-1,1)
      size <- rep(1, nobs)
    } else {
      if(is.matrix(yobs)) { # yobs=cbind(success, failure)
        size <- yobs[,1] + yobs[,2]
        yobs <- yobs[,1] #successes
      } else {
      if(all(yobs %in% c(0,1))) { #binary
        size <- rep(1, nobs)
      } else { #proportions
          yobs <- weights * yobs
          size <- weights
          weights <- rep(1, nobs)
        }
      }
[.....]
coraliewilliams commented 1 year ago

glmmTMB weight argument seems to be dealt with differently from the weight argument in glm and lm.

1. lm

Weights are used when data are only available in an averaged form (and actual data is unavailable). If non-NULL, weighted least squares is used with weights (that is, minimizing ‘sum(w*e^2)’)

2. glmmTMB

Weights are simple multipliers on the log-likelihood contributions of the observations. Weights are added to an observation by pretending multiple replicates are available. For binomial and poison families this coincides with the lm definition. However, it does not coincide with the Gaussian family (see example code in issue below from Ben Bolker).

Additional comment: "It is probably not possible for us to adopt the glm definition consistently in glmmTMB as it generally requires a convolution property of the family to get the likelihood contribution of the average right. We do not have such properties for all our families."

This is discussed in the following issue on glmmTMB master branch: https://github.com/glmmTMB/glmmTMB/issues/285#issuecomment-355035400

coraliewilliams commented 1 year ago

--> Side comment when using REML=T for fixed variance components:

This comment (https://github.com/glmmTMB/glmmTMB/issues/833#issuecomment-1182959872) shows an example when a REML model in glmmTMB is not working compared to a ML model. The feedback from Ben Bolker is that it is an edge effect (they didn't foresee anyone fitting a REML model with all random effects parameters fixed in Gaussian family context).

This was resolve partly in merged into master branch on glmmTMB: https://github.com/glmmTMB/glmmTMB/commit/320a78d0b906e7c24f8c6a36f6e38b33df24245f

coraliewilliams commented 1 year ago

Another important note from: https://bbolker.github.io/mixedmodels-misc/glmmFAQ.html