Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mice package compatibility #194

Open
abrisco17 opened this issue Jul 1, 2022 · 1 comment
Open

Mice package compatibility #194

abrisco17 opened this issue Jul 1, 2022 · 1 comment

Comments

@abrisco17
Copy link

Hi, is there any intention in adding mice comparability? I wanted to have a table for the pooled regression, so I wrote an extractor function for the "mira" object, which is the output after the new imputed datasets are fitted onto the original data. The pooling occurs in the extraction.

library(texreg)
library(mice)

extract.mira <- function(model, include.rsquared = TRUE, include.adjrs = TRUE, include.nobs = TRUE, ...){
  s <- summary(pool(model, ...))
  names <- as.character(s$term)
  co <- s$estimate
  se <- s$std.error
  pval <- s$p.value
  rs <- pool.r.squared(model)[1,1]
  adj <- pool.r.squared(model, adjusted = T)[1,1]
  n <- nobs(summary(model))[1]
  gof <- numeric()
  gof.names <- character()
  gof.decimal <- logical()
  
  if (include.rsquared == T) {
    gof <- c(gof, rs)
    gof.names <- c(gof.names, "R$^2$")
    gof.decimal <- c(gof.decimal, TRUE)
  }
  if (include.adjrs == T) {
    gof <- c(gof, adj)
    gof.names <- c(gof.names, "Adj.\ R$^2$")
    gof.decimal <- c(gof.decimal, TRUE)
  }
  if (include.nobs == T) {
    gof <- c(gof, n)
    gof.names <- c(gof.names, "Num.\ obs.")
    gof.decimal <- c(gof.decimal, FALSE)
  }
  
  tr <- createTexreg(
    coef.names=names,
    coef=co,
    se=se,
    pvalues=pval,
    gof.names=gof.names,
    gof=gof,
    gof.decimal=gof.decimal
  )
  return(tr)
}

setMethod("extract", signature = className("mira", "mice"), definition = extract.mira)

airquality_lm <- lm(Temp ~ Solar.R + Ozone + Wind, airquality)
airquality_imp <- mice(airquality, seed = 1)
airquality_fitted <- with(airquality_imp, lm(Temp ~ Solar.R + Ozone + Wind))

screenreg(list(airquality_lm, airquality_fitted))

It seems to work fine, but I wasn't sure if this was the best way, especially mice has some many objects, and it's not necessarily clear what to do if you want all the calculated regressions instead. Is there anymore development here?

@leifeld
Copy link
Owner

leifeld commented Jul 3, 2022

Thanks so much for your contribution! The texreg package currently uses a workaround to support mice: If you try it on the pooled object (e.g., screenreg(pool(airquality_fitted)), it will use the definitions from the broom package to parse the mipo object and present it as a table. It works, but comes at a price: The GOF statistics have different names from the ones usually used in texreg, which means you have to rename them if you want to display multiple models together. So I'd prefer to see an implementation directly in texreg. Your code seems to work, but I am wondering if it wouldn't make more sense to use those pooled mipo objects rather than mira objects because it is the pooled model we are interested in, after all. If you are interested in making this happen, I would love to see a pull request for a revised version of the above for mipo objects, ideally including the roxygen2 help page documentation as in the remaining extract.R file and ideally with a unit test case using testthat (see here for examples).

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

No branches or pull requests

2 participants