R:如何从几个GLM的lapply输出中创建一个好的结果表(sjPlot)?

时间:2016-11-23 19:59:06

标签: r lapply glm sjplot

亲爱的Stackoverflow社区

我正在研究R,我正在做几个不同家属的二项式逻辑回归。这些分析是通过微小的变化反复进行的,我和我的同事分享结果,最好是在漂亮的表格而不是凌乱的R结果。如果我只打算这样做几次,我可以将所有分析作为单个回归进行,然后使用sjt.glm来制作漂亮的表格。虽然我一遍又一遍地进行这些类似的分析,但我正在使用lapply循环来加速和简化过程。不幸的是,我无法使lappply和sjt.glm合作。最理想的是,我只需从lapply循环中获取结果,并使用sjt.glm创建一个不错的水平对齐表。

参见示例(对于丑陋的编码感到抱歉)

    library(sjPlot)
    swiss$y1 <- ifelse(swiss$Fertility < median(swiss$Fertility), 0, 1)
    swiss$y2 <- ifelse(swiss$Infant.Mortality < median(swiss$Infant.Mortality), 0, 1)
    swiss$y3 <- ifelse(swiss$Agriculture < median(swiss$Agriculture), 0, 1)

    #Normal slow way would be
    fitOR1 <- glm(y1 ~ Education + Examination + Catholic, data = swiss,
         family = binomial(link = "logit"))
    fitOR2 <- glm(y2 ~ Education + Examination + Catholic, data = swiss,
          family = binomial(link = "logit"))
    fitOR3 <- glm(y3 ~ Education + Examination + Catholic, data = swiss,
          family = binomial(link = "logit"))

    #and then simply use summary and other formulas to look at the results
    summary(fitOR1);exp(cbind(OR = coef(fitOR1), confint(fitOR1)))

    #but with 20+ dependents, this would become tedious

    #Doing the same analysis as a laply loop, is relatively easy (and non-tedious)
    varlist <- names(swiss[c(7:9)])

    results <- lapply(varlist, function(x){
      glm(substitute(i ~ Education + Examination + Catholic, list(i=as.name(x))), 
  family =binomial, data = swiss)})

    for (i in 1:3) print(summary(results[[i]]))
    for (i in 1:3) print(exp(cbind(OR = coef(results[[i]]), confint(results[[i]]))))

    #Though here is the catch. To get the output/results into a nice table
    #I can easily use sjt.glm for the "standard" single logistic regressions.
    sjt.glm(fitOR1,fitOR2,fitOR3, file = "SwissFits.html")

    #Though I can't think of how I could do this for the loop-results.
    #The closest I have come is perhaps something like
    for(i in 1:3)(sjt.glm(results[[i]],file="LoopSwissFits.html"))

    #but then I only get the results from the last regression. 

    #One alternative is to do
    lapply(varlist,function(x){ sjt.glm(
      glm(substitute(i ~ Education + Examination + Catholic, list(i=as.name(x))), 
  family =binomial, data = swiss), file = paste0("SwissFits_",(i=as.name(x)),".html"))})

    #but then I get three separate files, when it would be preferable to
     #have the results in one horizontally oriented file

你们有没有对我的问题有一个整洁而优雅的解决方案?

非常感谢你!

3 个答案:

答案 0 :(得分:1)

stargazer套装非常适合这种情况。使用write函数将结果存储在文本文件中。要在控制台中显示,只需使用 -

install.packages("stargazer")  
library(stargazer)

stargazer(results, align = TRUE, type = "text")

# To write to a word file
write(stargazer(results, align = TRUE, type = "text"), "results.txt")

答案 1 :(得分:1)

只需将列表作为第一个参数:@Override public Fragment getItem(int i) { Fragment fragment; switch(i){ case 0: fragment = new MyFragment1(); break; case 1: fragment = new MyFragment2(); break; case 2: fragment = new MyFragment3(); break; default: throw new IllegalArgumentException("Invalid section number"); } return fragment; }

答案 2 :(得分:0)

@code_is_entropy。

Stargazer很棒,但是在添加expcoef = T时它会给出错误的CI,不幸的是我在使用lapply循环时不知道如何修复它。见例子

library(stargazer);library(sjPlot)
mydata <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
varlist <- names(mydata[c(2:4)])
results <- lapply(varlist, function(x){
glm(substitute(admit ~i, list(i=as.name(x))), 
  family =binomial, data = mydata)})

stargazer(results, apply.coef = exp, align = TRUE, 
column.labels = varlist, model.names = T, ci = T, type = "text")
#vs
sjt.glm(results,digits.est = 5, digits.ci = 5,p.numeric = T, digits.p=5)
#vs
for (i in 1:3)print (exp(confint(results[[i]])))

因此,如果你比较exp(confint),sjt.glm和stargazer的结果,你会发现观星者的置信区间只是偏离。

顺便说一句,抱歉发布一个答案作为评论,我只需要超过字母数量就可以给出一个正确的例子。