With a regular lm-object, I can extract sigma like this:
library(mice)
nhanes$group <- as.factor(c(rep("A", 12), rep("B", 13)))
m_1 <- lm(bmi ~ group + age, nhanes)
sigma(m_1)
However, extracting sigma on models conducted on imputed data sets results in an error:
imp <- mice::mice(nhanes, maxit = 2, m = 2)
m_2 <- with(imp, lm(bmi ~ group + age))
sigma(m_2)
# numeric(0)
# Warning message:
# In nobs.default(object, use.fallback = use.fallback) :
# no 'nobs' method is available
How would one proceed to extract sigma in the above example?
I have tried various combinations of with() and pool(), but they result in one of two errors:
with(m_2, sigma())
# Error in sigma.default() : argument "object" is missing, with no default
sigma(pool(m_2))
# numeric(0)
# Warning message:
# In nobs.default(object, use.fallback = use.fallback) :
# no 'nobs' method is available
Since you chose mice(., m=<m>)
(m=2
are very few!), you have m
regressions, thus m
sigma
s,
If we reveal the str
ucture, we see a list of m
in the analyses element.
str(m_2, max.level=1)
# List of 4
# $ call : language with.mids(data=imp, expr=lm(bmi ~ group + age))
# $ call1 : language mice::mice(data=nhanes, m=2, maxit=2)
# $ nmis : Named int [1:5] 0 9 8 10 0
# ..- attr(*, "names")= chr [1:5] "age" "bmi" "hyp" "chl" ...
# $ analyses:List of 42
# - attr(*, "class")= chr [1:2] "mira" "matrix"
We get the sigmas using lapply
.
lapply(m_2$analyses, sigma)
# [[1]]
# [1] 2.711988
#
# [[2]]
# [1] 2.666487
#
# [[3]]
# [1] 2.876525
# ...
To get a kind of pooled sigma, you could calculate mean
and sd
:
sapply(c(mean=mean, sd=sd), \(f) f(sapply(m_2$analyses, sigma)))
# mean sd
# 2.8383536 0.2103296
Data:
data('nhanes', package='mice')
set.seed(42) ## mice involves stochastic processes!
nhanes$group <- gl(2, 13, labels=LETTERS[1:2])[-1]
imp <- mice::mice(nhanes, maxit=2, m=42, printFlag=F)
m_2 <- with(imp, lm(bmi ~ group + age))