我有一系列的lme4模型,我想在R中针对不同子集的不同结果运行(每个模型都在意向治疗(ITT)和按协议(PP)子集上运行,并且我有不同的结果),然后使用texreg()打印LaTeX表以比较结果并在knitr文档中很好地打印。

由于我要运行许多微妙的不同模型,因此我认为明智的做法是将模型和texreg()调用转换为函数,为此我编写了...

pleasant.regression <- function(data       = proportion,
                                time.frame = "September 2013",
                                outcome    = "unscheduled",
                                family     = binomial,
                                caption    = "ITT and PP Linear Mixed Effects Model Coefficients and Standard Errors for \\emph{any} Unscheduled Visits in September 2013.",
                                label    = "logistic-unscheduled",
                                ...){
    ## Require packages
    require(lme4)
    require(ResourceSelection)
    require(texreg)
    ## Set defaults for texreg tables, can be modified with additional arguments

    texreg.digits        <- 2
    texreg.table         <- TRUE
    texreg.caption.above <- TRUE
    texreg.booktabs      <- TRUE
    texreg.dcolumn       <- TRUE
    texreg.use.packages  <- FALSE
    texreg.float.pos     <- "!htpb"
    texreg.ci.force      <- TRUE
    texreg.ci.test       <- 0
    ## Parse the outcome into a formula with the desired mixed
    ## effects model
    .formula <- reformulate(response = outcome, termlabels = c("allocation", "gender", "age", "n.unscheduled.2012", "(1 | pracid)"))
    ## Logistic Regresion
    if(family == "binomial"){
        ## ITT
        itt.mixed <- lmer(.formula,
                          data   = subset(data,
                                          period == time.frame & age.include == TRUE),
                          family = family)
        ## PP
        pp.mixed <- lmer(.formula,
                         data   = subset(data,
                                         period == time.frame & age.include == TRUE & pp == 1),
                         family = family)
    }
    ## Negative Binomial
    else if(family == "negbin"){
        ## ITT
        itt.mixed <- glmer.nb(.formula,
                              data   = subset(data,
                                              period == time.frame & age.include == TRUE))
        ## PP
        pp.mixed <- glmer.nb(.formula,
                             data   = subset(data,
                                             period == time.frame & age.include == TRUE & pp == 1))
    }
    ## Save table comparing ITT to PP using texreg()
    results <- invisible(texreg(list(itt.mixed, pp.mixed),
                                custom.model.names = c("ITT", "PP"),
                                custom.coef.names  = c("Intercept", "Allocation (Letter)", "Gender (Female)", "Age", "$N_{Unscheduled}$ September 2012"),
                                digits             = texreg.digits,
                                caption            = caption,
                                table              = texreg.table,
                                caption.above      = texreg.caption.above,
                                label              = label,
                                booktabs           = texreg.booktabs,
                                dcolumn            = texreg.dcolumn,
                                use.packages       = texreg.use.packages,
                                float.pos          = texreg.float.pos,
                                ci.force           = texreg.ci.force,
                                ci.test            = texreg.ci.test))
    return(results)
}


当我对此进行调用时,虽然texreg()从函数内部打印出结果,但不会将表作为打印对象返回...

> my.results <- pleasant.regression(data       = proportion,
                               time.frame = "September 2013",
                               outcome    = "unscheduled",
                               family     = "binomial",
                               caption    = "ITT and PP Linear Mixed Effects Model Coefficients and Standard Errors for \\emph{any} Unscheduled Visits in September 2013.",
                               label    = "logistic-unscheduled") ## Not expecting any output
Computing profile confidence intervals ...
Confidence intervals not available for this model. Using naive p values instead.
Computing profile confidence intervals ...
Confidence intervals not available for this model. Using naive p values instead.

\begin{table}[!htpb]
\caption{ITT and PP Linear Mixed Effects Model Coefficients and Standard Errors for \emph{any} Unscheduled Visits in September 2013. \textbf{NB} P-values are not explicitly calculated in favour of 95\% confidence intervals}
\begin{center}
\begin{tabular}{l D{.}{.}{5.11}@{} D{.}{.}{5.11}@{} }
\toprule
                                    & \multicolumn{1}{c}{ITT} & \multicolumn{1}{c}{PP} \\
\midrule
Intercept                           & -0.73^{*}       & -0.71^{*}       \\
                                    & [-0.95;\ -0.51] & [-0.95;\ -0.47] \\
Allocation (Letter)                 & -0.11           & -0.12           \\
                                    & [-0.33;\ 0.12]  & [-0.36;\ 0.12]  \\
Gender (Female)                     & 0.06            & 0.06            \\
                                    & [-0.03;\ 0.15]  & [-0.03;\ 0.16]  \\
Age                                 & -0.01           & -0.01           \\
                                    & [-0.03;\ 0.00]  & [-0.03;\ 0.00]  \\
$N_{Unscheduled}$ September 2012    & 1.18^{*}        & 1.15^{*}        \\
                                    & [1.12;\ 1.25]   & [1.08;\ 1.22]   \\
\midrule
AIC                                 & 12828.97        & 11597.78        \\
BIC                                 & 12873.10        & 11641.29        \\
Log Likelihood                      & -6408.49        & -5792.89        \\
Deviance                            & 12816.97        & 11585.78        \\
Num. obs.                           & 11547           & 10415           \\
Number of Practices                 & 142             & 136             \\
Variance : Practice                 & 0.37            & 0.40            \\
Variance : Residual                 & 1.00            & 1.00            \\
\bottomrule
\multicolumn{3}{l}{\scriptsize{$^*$ 0 outside the confidence interval}}
\end{tabular}
\label{logistic-unscheduled}
\end{center}
\end{table}

> print(my.results) ## Would expect this to hold the above table and print it again
NULL


我尝试根据StackOverflow线程heretexreg()调用包装在invisible()中,以便不打印结果并返回结果,但是这似乎没有达到我的预期。

我希望我遗漏了一些明显的东西,但无法解决,任何指针/建议都将不胜感激。

最佳答案

无论出于何种原因,如果没有提供输出文件名,texreg()函数都会选择通过cat()将结果直接打印到屏幕上。这实际上并不是R中大多数功能的工作方式,但是对于用户提交的程序包,程序包作者可以执行任何所需的操作。

因此,从技术上讲,默认情况下texreg()不返回任何内容。您可以通过设置return.string=TRUE让它返回一个字符串,但它仍然也会打印到屏幕上。防止自动打印的最简单方法是用capture.output()包裹调用。这将抑制屏幕输出,并将结果转换为字符向量,并在每行输出中都有一个条目。因此,您可以将函数的结尾更改为

results <- paste(capture.output(texreg(... rest of code ...)), collapse="\n")
return(results)


那应该返回您期望的字符值,而不是打印到屏幕上。

07-28 07:43