Package/function to make side-by-side tables of unmarked models?

131 views
Skip to first unread message

Michael Havrda

unread,
Apr 11, 2022, 5:36:47 PM4/11/22
to unmarked
I'd like to make a side-by-side table to show the results of multiple occupancy models, such as those shown here for linear models. However, neither the packages huxtable nor modelsummary work on unmarkedFitOccu objects.

Has anyone figured out a solution to this, or found a package that works on unmarked models?


Marc Kery

unread,
Apr 12, 2022, 4:57:25 AM4/12/22
to unmarked
Dear Michael,

I agree that this is an interesting form of presenting the results from multiple models fit, a sort of qualitative sentivity analysis in terms of the selected model.

I also don't have a solution but thought that perhaps you might write to that package developer and try to convince him that unmarked is a useful and widely used package and that it would be a good thing for him to expand the functionality of his package to include fitted model objects from unmarked also ?

Best regards  --- Marc




From: unma...@googlegroups.com <unma...@googlegroups.com> on behalf of Michael Havrda <mvha...@gmail.com>
Sent: Monday, April 11, 2022 23:36
To: unmarked <unma...@googlegroups.com>
Subject: [unmarked] Package/function to make side-by-side tables of unmarked models?
 
I'd like to make a side-by-side table to show the results of multiple occupancy models, such as those shown here for linear models. However, neither the packages huxtable nor modelsummary work on unmarkedFitOccu objects.

Has anyone figured out a solution to this, or found a package that works on unmarked models?


--
You received this message because you are subscribed to the Google Groups "unmarked" group.
To unsubscribe from this group and stop receiving emails from it, send an email to unmarked+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/unmarked/09002ff2-5b84-4995-a0d7-a49b8d61695fn%40googlegroups.com.

Ken Kellner

unread,
Apr 13, 2022, 2:36:42 PM4/13/22
to unmarked
Hi Michael,

huxtable requires that the tidy() method works on the model type you are trying to use, which converts the model output into a data frame. Unfortunately there's no existing tidy() method for unmarkedFit in the broom package, but I wrote one. See example below, I haven't tested on other model types besides occu.

library(unmarked)
library(huxtable)
library(broom)

# Load data
data(frogs)
pferUMF <- unmarkedFrameOccu(pfer.bin)

siteCovs(pferUMF) <- data.frame(sitevar1 = rnorm(numSites(pferUMF)))
obsCovs(pferUMF) <- data.frame(obsvar1 = rnorm(numSites(pferUMF) * obsNum(pferUMF)))
 
# Fit occupancy models
fm_null <- occu(~ 1 ~ 1, pferUMF)
fm_covs <- occu(~ obsvar1 ~ sitevar1, pferUMF)

# Write required tidy and glance methods
tidy.unmarkedEstimate <- function(x, ...){
  submod <- paste0("[",x...@short.name,"]")
  nul <- capture.output(out <- summary(x))
  names(out) <- c("estimate", "std.error", "statistic", "p.value")
  out <- cbind(term = paste(submod, rownames(out)), out)
  rownames(out) <- NULL
  tibble::as_tibble(out)
}

tidy.unmarkedFit <- function(x, submodel, ...){
  if(missing(submodel)) submodel <- names(x)
  stopifnot(all(submodel %in% names(x)))
  out <- lapply(x@estimates@estimates[submodel], tidy)
  do.call("rbind", out)
}

glance.unmarkedFit <- function(x, ...){
  tibble::tibble(AIC=x@AIC, nobs=numSites(x@data))
}

# Try out new methods
tidy(fm_covs)
tidy(fm_covs, submodel='state') # only occupancy submodel
glance(fm_covs)

# Print some tables
# Note that only the AIC statistic works
huxreg(fm_null, fm_covs, statistics=c("AIC"))

# Add CI
huxreg(fm_null, fm_covs, ci_level=0.95, error_format="{conf.low} to {conf.high}",
       statistics=c("AIC"))

# Include only occupancy submodel in output table
huxreg(fm_null, fm_covs, statistics=c("AIC"),
       tidy_args=list(submodel="state"))

Ken Kellner

unread,
Apr 13, 2022, 2:43:22 PM4/13/22
to unmarked
If you are viewing this on the webpage, rather than in your email client, google groups "helpfully" converted some of the code to an email link.

The first line of tidy.unmarkedEstimate method should be x(at)short.name not x...(at)short.name

Marc Kery

unread,
Apr 14, 2022, 6:34:59 AM4/14/22
to unmarked
Reply all
Reply to author
Forward
0 new messages