如何将合并插补回归结果添加到stargazer表

nx7onnlm  于 2023-05-04  发布在  其他
关注(0)|答案(1)|浏览(108)

我在我的数据中输入了一些NA,计算了一个回归模型,并希望在HTML表格中呈现结果。我知道如何做一个普通的模型。

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")

然而,当我试图对从MICE估算获得的模型结果做同样的事情时,我做不到。

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")

stargazer命令给我一个错误:

Error in start.lines[table.number]:length(all.latex.code) : 
  argument of length 0

那么,有没有一种方法可以使MICE估算回归的模型结果与使用stargazer的常规回归结果显示相同的方式?非常感谢提前任何和所有的提示,你可能有!

hkmswyz6

hkmswyz61#

我认为最简单的方法是在单个插补数据集的一个模型上调用stargazer()(e.例如,mydf_mice_models[[1]]),您可以使用从合并模型计算的值替换系数、标准误、t统计量和p值。

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
pool.mod <- mice::pool(mydf_mice_models)
smry <- pool.mod$pooled

b <- smry[,"estimate"]
names(b) <- smry[,"term"]
se <- sqrt(smry[,"t"])
t_stat = b/se
p_val <- pt(t_stat, df=smry[,"df"])
stargazer(mydf_mice_models[[1]], 
          coef = list(b), 
          se = list(se),
          t = list(t_stat), 
          p = list(p_val),
          keep.stat = "n", 
  type = "text")
#> 
#> ========================================
#>                  Dependent variable:    
#>              ---------------------------
#>                     Petal.Length        
#> ----------------------------------------
#> Petal.Width             1.507           
#>                        (0.068)          
#>                                         
#> Sepal.Length            0.715           
#>                        (0.058)          
#>                                         
#> Sepal.Width           -0.634***         
#>                        (0.072)          
#>                                         
#> Constant               -0.287           
#>                        (0.298)          
#>                                         
#> ----------------------------------------
#> Observations             150            
#> ========================================
#> Note:        *p<0.1; **p<0.05; ***p<0.01

创建于2023-05-02使用reprex v2.0.2

相关问题