Skip to content

Add code for find theta #2

Description

@jaseeverett
# A short script courtesy of Bill Venables that automates
# the search for theta in a neg bin model. 

m1 <- gam(richness ~ s(latitude, k = 5), family = poisson, dat_std)
c(deviance = deviance(m1), res_df = m1$df.residual)

m1 <- update(m1, family = negbin(theta = 2))       ### not quite enough
c(deviance = deviance(m1), res_df = m1$df.residual)

m1 <- update(m1, family = negbin(theta = 1))       ### a bit too much
c(deviance = deviance(m1), res_df = m1$df.residual)

fun <- function(theta) {
  m1 <- update(m1, family = negbin(theta = theta))
  m1$df.residual - deviance(m1)
}

(opt <- uniroot(fun, interval = c(1,2)))  ### can take a few seconds!

m1 <- update(m1, family = negbin(theta = 1.548))
c(deviance = deviance(m1), res_df = m1$df.residual)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions