【问题标题】:returning texreg() objects返回 texreg() 对象
【发布时间】:2014-07-20 09:48:16
【问题描述】:

我有一系列 lme4 模型,我想在 R 中针对不同子集的不同结果运行(每个模型都在意向治疗 (ITT) 和按协议 (PP) 子集上运行,并且我' ve 不同的结果),我使用 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() 中,这样结果就没有被打印出来并被返回,但这似乎并没有像我预期的那样工作。

我希望我遗漏了一些明显但无法解决的问题,我们将不胜感激地收到任何指示/建议。

【问题讨论】:

    标签: r knitr texreg


    【解决方案1】:

    无论出于何种原因,如果您没有提供输出文件名,texreg() 函数会选择通过cat() 将结果直接打印到屏幕上。这并不是 R 中大多数函数的实际工作方式,但是通过用户提交的包,包作者可以做任何他们喜欢的事情。

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

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

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

    【讨论】:

    • 谢谢 MrFlick,这解决了我遇到的问题。
    • 这适用于交互式终端,但是当我在我的 Knitr .Rnw 文件中包含对我的函数的调用时,它不会产生任何输出。分配返回的对象(例如my.results &lt;- pleasant.regression())也无济于事,因为如果我尝试显示它们(print(my.results)),我会收到一条错误消息,指出’print’: Error: object ’my.results’ not found。我会玩一玩,做更多的阅读,看看我能不能弄清楚为什么 Knitr 不喜欢这个。
    • 也许knitr 已经在拦截输出。我不知道。我自己不使用knitr
    • 好的,我会看看这个(尽管没有使用knitr,但感谢您的帮助),尽管如果我只是调用我的函数并且不分配返回的结果字符值也不显示。我希望它是knitr 特定的东西,并且可能值得它自己的线程,因为您为我的原始问题提供了一个可行的解决方案。再次感谢。
    • 抱歉,我确实意识到最小可重复示例的实用性,但目前没有足够的工作时间。我发现它与 knitr 相关的问题是由在函数中包含对无法读取的文件的引用引起的,现在已排序。顺便问一下texreg 的作者 Philip Liefield 关于使用cat() 来返回对象的问题,他已经更新了他的包,见r-forge.r-project.org/forum/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 2018-07-11
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多