gpt4 book ai didi

r - 用于 knitr 的 stargazer 的多列输出

转载 作者:行者123 更新时间:2023-12-04 12:07:29 25 4
gpt4 key购买 nike

这是 MWE:

library(pscl)

data("bioChemists", package = "pscl")

fm_pois <- glm(art ~ ., data = bioChemists, family = poisson)
fm_qpois <- glm(art ~ ., data = bioChemists, family = quasipoisson)
fm_nb <- glm.nb(art ~ ., data = bioChemists)
fm_zinb <- zeroinfl(art ~ . | 1, data = bioChemists, dist = "negbin")

library(stargazer)
stargazer(
fm_pois, fm_qpois, fm_nb, fm_zinb
, type = "text"
)

=============================================================================
Dependent variable:
-----------------------------------------------------------
art
Poisson glm: quasipoisson negative zero-inflated
link = log binomial count data
(1) (2) (3) (4)
-----------------------------------------------------------------------------
femWomen -0.225*** -0.225*** -0.216*** -0.216***
(0.055) (0.074) (0.073) (0.073)

marMarried 0.155** 0.155* 0.150* 0.150*
(0.061) (0.083) (0.082) (0.082)

kid5 -0.185*** -0.185*** -0.176*** -0.176***
(0.040) (0.054) (0.053) (0.053)

phd 0.013 0.013 0.015 0.015
(0.026) (0.036) (0.036) (0.036)

ment 0.026*** 0.026*** 0.029*** 0.029***
(0.002) (0.003) (0.003) (0.003)

Constant 0.305*** 0.305** 0.256* 0.256*
(0.103) (0.139) (0.137) (0.139)

-----------------------------------------------------------------------------
Observations 915 915 915 915
Log Likelihood -1,651.056 -1,561.958 -1,560.959
theta 2.264*** (0.271)
Akaike Inf. Crit. 3,314.113 3,135.917
=============================================================================
Note: *p<0.1; **p<0.05; ***p<0.01

我正在寻找这样的多列输出:
=============================================================================
Dependent variable:
-----------------------------------------------------------
art
Poisson Negative Binomial

Poisson QuasiPoisson NB ZINB

(1) (2) (3) (4)
-----------------------------------------------------------------------------
femWomen -0.225*** -0.225*** -0.216*** -0.216***
(0.055) (0.074) (0.073) (0.073)

marMarried 0.155** 0.155* 0.150* 0.150*
(0.061) (0.083) (0.082) (0.082)

kid5 -0.185*** -0.185*** -0.176*** -0.176***
(0.040) (0.054) (0.053) (0.053)

phd 0.013 0.013 0.015 0.015
(0.026) (0.036) (0.036) (0.036)

ment 0.026*** 0.026*** 0.029*** 0.029***
(0.002) (0.003) (0.003) (0.003)

Constant 0.305*** 0.305** 0.256* 0.256*
(0.103) (0.139) (0.137) (0.139)

-----------------------------------------------------------------------------
Observations 915 915 915 915
Log Likelihood -1,651.056 -1,561.958 -1,560.959
theta 2.264*** (0.271)
Akaike Inf. Crit. 3,314.113 3,135.917
=============================================================================
Note: *p<0.1; **p<0.05; ***p<0.01
  • 第一行应该有 Poisson对于前两列和 Negative Binomial对于接下来的两列。
  • 第二行应该有类似 Poisson 的列名, Quasi Poisson , Negative BinomialZero Inflated Negative Binomial .

  • 我找到了这个 link但无法想出如何得到这个。

    最佳答案

    像尼克肯尼迪一样,我不认为 stargazer可以直接产生你想要的输出。

    因此,这里有一个解决方法:保存 stargazer表并手动添加所需的行。
    我在这里进行了硬编码;通过更多的努力,应该可以自动将文本居中放置在相应的列上方。
    请注意,我稍微更改了您的 stargazer调用以隐藏(错误的)模型名称。

    library(pscl)
    library(stargazer)

    data("bioChemists", package = "pscl")

    fm_pois <- glm(art ~ ., data = bioChemists, family = poisson)
    fm_qpois <- glm(art ~ ., data = bioChemists, family = quasipoisson)
    fm_nb <- glm.nb(art ~ ., data = bioChemists)
    fm_zinb <- zeroinfl(art ~ . | 1, data = bioChemists, dist = "negbin")

    byLine <-
    do.call("c",
    strsplit(
    capture.output(
    stargazer(fm_pois, fm_qpois, fm_nb, fm_zinb,
    type = "text", model.names = FALSE)
    ),
    "\n"))

    result <- append(
    byLine,
    c(
    " Poisson Negative Binomial",
    "",
    " Poisson QuasiPoisson NB ZINB"
    ),
    after = c(4, 5, 6))

    cat(paste(result, collapse = "\n"))

    # ==================================================================
    # Dependent variable:
    # ------------------------------------------------
    # art
    # Poisson Negative Binomial
    #
    # Poisson QuasiPoisson NB ZINB
    # (1) (2) (3) (4)
    # ------------------------------------------------------------------
    # femWomen -0.225*** -0.225*** -0.216*** -0.216***
    # (0.055) (0.074) (0.073) (0.073)
    #
    # marMarried 0.155** 0.155* 0.150* 0.150*
    # (0.061) (0.083) (0.082) (0.082)
    #
    # kid5 -0.185*** -0.185*** -0.176*** -0.176***
    # (0.040) (0.054) (0.053) (0.053)
    #
    # phd 0.013 0.013 0.015 0.015
    # (0.026) (0.036) (0.036) (0.036)
    #
    # ment 0.026*** 0.026*** 0.029*** 0.029***
    # (0.002) (0.003) (0.003) (0.003)
    #
    # Constant 0.305*** 0.305** 0.256* 0.256*
    # (0.103) (0.139) (0.137) (0.139)
    #
    # ------------------------------------------------------------------
    # Observations 915 915 915 915
    # Log Likelihood -1,651.056 -1,561.958 -1,560.959
    # theta 2.264*** (0.271)
    # Akaike Inf. Crit. 3,314.113 3,135.917
    # ==================================================================
    # Note: *p<0.1; **p<0.05; ***p<0.01

    关于r - 用于 knitr 的 stargazer 的多列输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31461202/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com