简体   繁体   English

将汇总统计从 psych Package mediate() function 转换为 Latex

[英]Convert the summary statistic from psych Package mediate() function to Latex

Is there any possibility to convert the the summary statistic from psych Package mediate() function to Latex?是否有可能将汇总统计数据从 psych Package mediate() function 转换为 Latex?

I tried stargazer, texreg and xtable so far - but without success...到目前为止,我尝试了 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)

As far as I can tell, this type of model is not supported by any of the main table-making packages.据我所知,任何主要的制表软件包都不支持这种类型的 model。 However, it is quite easy to add support for the modelsummary package (disclaimer: I am the author.) All you need to do is create a function called tidy.mediate which returns a data.frame with three columns called: term , estimate , and std.error .但是,添加对modelsummary的支持非常容易(免责声明:我是作者。)您需要做的就是创建一个名为tidy.mediate的 function ,它返回一个包含三个列的 data.frame: termestimate ,和std.error Then, you define a function called glance.mediate which returns a data.frame with one row and one column for each of the model characteristic you want to include at the bottom of the table.然后,您定义一个名为glance.mediate的 function ,它返回一个 data.frame,其中包含您想要包含在表底部的每个 model 特征的一行和一列。

This will require you to look into your model object (try str(res) ) a bit to figure out where the information you want is stored, but it's pretty easy.这将需要您查看您的 model object (尝试str(res) )以确定您想要的信息的存储位置,但这很容易。 You'll find all the details in the “Adding New Models” section of the website: https://vincentarelbundock.github.io/modelsummary/articles/modelsummary.html您可以在网站的“添加新模型”部分找到所有详细信息: https://vincentarelbundock.github.io/modelsummary/articles/modelsummary.ZFC35FDC70D5FC67D23E2C70D5FC67D23E2C8

Here's a minimal example:这是一个最小的例子:

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 Model 1
Intercept截距 -0.354 -0.354
(0.218) (0.218)
THERAPY治疗 0.819 0.819
(0.299) (0.299)
Num.Obs.数量。 30 30

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM