I have imputed some NAs in my data, calculated a regression model and would like to present the results in a HTML table. I know how to do this for a regular model.
library(stargazer)
mydf <- iris
mydf_nonmice_model <- lm(
Petal.Length ~ Petal.Width + Sepal.Length + Sepal.Width,
data = mydf)
stargazer(mydf_nonmice_model, type = "html", out = "mice_reg.html")
However, when I try to do the same thing for model results obtained from MICE imputation, I can't do it.
library(mice)
library(stargazer)
mydf <- iris
# create some NAs
l <- nrow(mydf)
set.seed(1)
indeces <- sample(1:l, l/3)
mydf$Sepal.Length[indeces] <- NA
# impute data with 30 variations
m <- 30
mydf_mice <- mice::mice(data=mydf, m=m, maxit=10, seed=1, print=F)
# create models for imputed data
# (I know that you can also do this using 'with()', but the code below works ok for me)
mydf_mice_models <- lapply(1:m, function(i) {
lm(Petal.Length ~ Petal.Width + Sepal.Length + Sepal.Width,
data = mice::complete(data = mydf_mice, i))
})
# look at the results
mice::pool(mydf_mice_models)
# creating a stargazer table gives an error
stargazer(
mice::pool(mydf_mice_models),
type = "html",
out = "mice_reg.html")
The stargazer command gives me an error:
Error in start.lines[table.number]:length(all.latex.code) :
argument of length 0
So, is there a way to make model results of MICE imputed regressions display the same way as regular ones using stargazer? Many thanks in advance for any and all tips you might have!
I think the easiest way is to call stargazer()
on one of the models from single imputed dataset (e.g., mydf_mice_models[[1]]
) and you can replace the coefficient, standard errors, t-statistics and p-values with values calculated from the pooled model.
library(mice)
library(stargazer)
project.org/package=stargazer
mydf <- iris
# create some NAs
l <- nrow(mydf)
set.seed(1)
indeces <- sample(1:l, l/3)
mydf$Sepal.Length[indeces] <- NA
# impute data with 30 variations
m <- 30
mydf_mice <- mice::mice(data=mydf, m=m, maxit=10, seed=1, print=F)
# create models for imputed data
# (I know that you can also do this using 'with()', but the code below works ok for me)
mydf_mice_models <- lapply(1:m, function(i) {
lm(Petal.Length ~ Petal.Width + Sepal.Length + Sepal.Width,
data = mice::complete(data = mydf_mice, i))
})
mydf_mice_models2 <- lapply(1:m, function(i) {
lm(Petal.Length ~ Petal.Width + Sepal.Width,
data = mice::complete(data = mydf_mice, i))
})
# look at the results
pool.mod <- mice::pool(mydf_mice_models)
smry <- summary(pool.mod)
pool.mod2 <- mice::pool(mydf_mice_models2)
smry2 <- summary(pool.mod2)
b <- smry$estimate
names(b) <- smry$term
se <- smry$std.error
t_stat = smry$statistic
p_val <- smry$p.value
b2 <- smry2$estimate
names(b2) <- smry2$term
se2 <- smry2$std.error
t_stat2 = smry2$statistic
p_val2 <- smry2$p.value
Answering the last question in the comments, you can use add.lines()
to add to the table, just pass in the values you would calculate for the pooled model AIC and LL.
stargazer(mydf_mice_models[[1]],
mydf_mice_models2[[1]],
coef = list(b, b2),
se = list(se, se2),
t = list(t_stat, t_stat2),
p = list(p_val, p_val2),
add.lines = list(c("AIC", 1.234, 3.456),
c("log likelihood", 5.678, 6.789)),
keep.stat = "n",
type = "text")
#>
#> ===========================================
#> Dependent variable:
#> ----------------------------
#> Petal.Length
#> (1) (2)
#> -------------------------------------------
#> Petal.Width 1.507*** 2.156***
#> (0.068) (0.053)
#>
#> Sepal.Length 0.715***
#> (0.058)
#>
#> Sepal.Width -0.634*** -0.355***
#> (0.072) (0.092)
#>
#> Constant -0.287 2.258***
#> (0.298) (0.314)
#>
#> -------------------------------------------
#> AIC 1.234 3.456
#> log likelihood 5.678 6.789
#> Observations 150 150
#> ===========================================
#> Note: *p<0.1; **p<0.05; ***p<0.01
Created on 2023-05-07 with reprex v2.0.2