【问题标题】:Convert the summary statistic from psych Package mediate() function to Latex将 psych Package mediate() 函数的汇总统计数据转换为 Latex
【发布时间】:2021-08-14 02:45:42
【问题描述】:

有没有可能将 psych Package mediate() 函数的汇总统计数据转换为 Latex ?

到目前为止,我尝试了 stargazer、texreg 和 xtable - 但没有成功...

#check and install required packages
if (!require("psych")) install.packages("psych")
if (!require("stargazer")) install.packages("stargazer")
if (!require("texreg")) install.packages("texreg")
if (!require("xtable")) install.packages("xtable")

#load required packages
library(psych)
library(stargazer)
library(texreg)
library(xtable)

#data from Preacher and Hayes (2004)
sobel <- structure(list(SATIS = c(-0.59, 1.3, 0.02, 0.01, 0.79, -0.35, 
                                  -0.03, 1.75, -0.8, -1.2, -1.27, 0.7, -1.59, 0.68, -0.39, 1.33, 
                                  -1.59, 1.34, 0.1, 0.05, 0.66, 0.56, 0.85, 0.88, 0.14, -0.72, 
                                  0.84, -1.13, -0.13, 0.2), 
                        THERAPY = structure(c(0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0), 
                                            value.labels = structure(c(1, 0), .Names = c("cognitive", "standard"))), 
                        ATTRIB = c(-1.17, 0.04, 0.58, -0.23, 0.62, -0.26, -0.28, 0.52, 0.34, -0.09, -1.09, 1.05, -1.84, -0.95, 0.15, 0.07, 
                                   -0.1, 2.35, 0.75, 0.49, 0.67, 1.21, 0.31, 1.97, -0.94, 0.11, -0.54, -0.23, 0.05, -1.07)), .Names = c("SATIS", "THERAPY", "ATTRIB"), 
                   row.names = c(NA, -30L), class = "data.frame", variable.labels = structure(c("Satisfaction", "Therapy", "Attributional Positivity"), 
                                                                                              .Names = c("SATIS", "THERAPY", "ATTRIB")))


#n.iter set to 50 (instead of default of 5000) for speed of example
res <- mediate(1,2,3,sobel,n.iter=50)  #The example in Preacher and Hayes
res

summary(res)

【问题讨论】:

    标签: r latex psych


    【解决方案1】:

    据我所知,任何主要的制表软件包都不支持这种类型的模型。但是,添加对modelsummary package (disclaimer: I am the author.) 的支持非常容易,您只需创建一个名为tidy.mediate 的函数,它会返回一个包含三列的data.frame:termestimatestd.error .然后,您定义一个名为 glance.mediate 的函数,该函数返回一个 data.frame,其中包含您希望在表底部包含的每个模型特征的一行和一列。

    这将需要您稍微查看一下您的模型对象(尝试str(res))以找出您想要的信息存储在哪里,但这很容易。您可以在网站的“添加新模型”部分找到所有详细信息:https://vincentarelbundock.github.io/modelsummary/articles/modelsummary.html

    这是一个最小的例子:

    library(psych)
    
    library(modelsummary)
    
    res <- mediate(1, 2, 3, sobel, n.iter = 50) # The example in Preacher and Hayes
    
    
    tidy.mediate <- function(x, ...) {
      out <- data.frame(
        row.names(x$a.reg$beta),
        x$a.reg$beta,
        x$a.reg$se
      )
      colnames(out) <- c("term", "estimate", "std.error")
      out
    }
    
    glance.mediate <- function(x, ...) {
      out <- data.frame(nobs = nrow(x$data))
      out
    }
    
    modelsummary(res)
    
    Model 1
    Intercept -0.354
    (0.218)
    THERAPY 0.819
    (0.299)
    Num.Obs. 30

    【讨论】:

    • 谢谢文森特!这很有帮助!
    猜你喜欢
    • 2018-08-08
    • 2022-09-30
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    • 2020-07-23
    • 1970-01-01
    相关资源
    最近更新 更多