首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >返回texreg()对象

返回texreg()对象
EN

Stack Overflow用户
提问于 2014-07-20 09:48:16
回答 1查看 799关注 0票数 3

我有一系列的lme4模型,我想在R中对不同子集中的不同结果运行(每个模型运行在意图治疗(ITT)和Per-Protocol (PP)子集上,并且我有不同的结果),我使用texreg()打印一个LaTeX表来比较结果,并在knitr文档中很好地打印出来。

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

代码语言:javascript
复制
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()打印出函数内部的结果,但不将表作为打印对象返回.

代码语言:javascript
复制
> 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

我尝试基于一个texreg()线程这里invisible()中包装StackOverflow调用,这样结果就不会被打印并返回,但这似乎不像我预期的那样有效。

我希望我错过了一些显而易见的东西,但无法解决,任何建议/建议都会被感激地收到。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-20 19:21:27

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

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

代码语言:javascript
复制
results <- paste(capture.output(texreg(... rest of code ...)), collapse="\n")
return(results)

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24849273

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档