打印“漂亮" R中的h2o模型的表格 [英] Print "pretty" tables for h2o models in R

查看:108
本文介绍了打印“漂亮" R中的h2o模型的表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R有多个软件包,可帮助从统计模型输出中打印漂亮"表(LaTeX/HTML/TEXT),并轻松比较替代模型规格的结果.

There are multiple packages for R which help to print "pretty" tables (LaTeX/HTML/TEXT) from statistical models output AND to easily compare the results of alternative model specifications.

其中一些软件包是apsrtablextablememisctexregoutregstargazer(有关示例,请参见此处:

Some of these packages are apsrtable, xtable, memisc, texreg, outreg, and stargazer (for examples see here: https://www.r-statistics.com/2013/01/stargazer-package-for-beautiful-latex-tables-from-r-statistical-models-output/).

是否有可比的R软件包支持h2o软件包的模型?

Is there any comparable R package that does support the models of the h2o package?

这是两个带有h2o的简单GLM模型的示例,我希望将它们彼此并排打印为美丽"表.

Here is an example of two simple GLM models with h2o which I like to print beside each other as "beautiful" tables.

# Load package and setup h2o
library(h2o)
localH2O <- h2o.init(ip = 'localhost', port = 54321, max_mem_size = '4g')

# Load data
prostatePath <- system.file("extdata", "prostate.csv", package = "h2o")
prostate.hex <- h2o.importFile(path = prostatePath, destination_frame = "prostate.hex")

# Run GLMs
model.output.1 <- h2o.glm(y = "CAPSULE", x = c("RACE","PSA","DCAPS"),
  training_frame = prostate.hex,family = "binomial", nfolds = 0, 
  alpha = 0.5, lambda_search = FALSE)
model.output.2 <- h2o.glm(y = "CAPSULE", x = c("AGE","RACE","PSA","DCAPS"), 
  training_frame = prostate.hex, family = "binomial", nfolds = 0, 
  alpha = 0.5, lambda_search = FALSE)

这是使用texreg软件包中的screenreg()的常规GLM对象的外观:

This is how it would look like with an regular GLM object using screenreg() from the texreg package:

library(data.table)
library(texreg)
d <- fread(prostatePath)
model.output.1.glm <- glm(CAPSULE ~ RACE + PSA + DCAPS, data=d)
model.output.2.glm <- glm(CAPSULE ~ AGE + RACE + PSA + DCAPS, data=d)
screenreg(list(model.output.1.glm, model.output.2.glm))

推荐答案

texreg程序包作者在此处. texreg基于通用功能,这意味着任何用户都可以为任意模型添加自定义extract方法,并使它们与texreg一起使用.我将在下面引导您完成此过程.我希望这个详细的展览会帮助过去曾问过类似问题的其他人设计自己的texreg扩展名.

texreg package author here. texreg is based on generic functions, which means any user can add custom extract methods for arbitrary models and make them work with texreg. I will walk you through this below. I am hoping that this detailed exposition will help other people who have asked similar questions in the past to devise their own texreg extensions.

另请参见《统计软件杂志》 2013年论文中的第6节再举一个例子.但是,首先,我将描述texreg架构如何更一般地工作,以使您了解可能的情况.

See also Section 6 in the 2013 paper in the Journal of Statistical Software for another example. First, however, I will describe how the texreg architecture works more generally to give you an idea what is possible.

共有三个功能:texreg(用于LaTeX输出),htmlreg(用于HTML输出,在大多数使用情况下也可以由Word或Markdown解释)和screenreg(用于在Windows中输出ASCII文本)控制台).这三个功能用于将一些清理后的信息(系数,标准误差,置信区间,p值,拟合优度统计信息,模型标签等)转换为相应的输出格式.这并不完美,甚至可以更加灵活,但是我认为目前有很多自定义参数,包括booktabsdcolumn支持之类的东西.因此,最大的挑战是首先获得清理后的模型信息.

There are three functions: texreg (for LaTeX output), htmlreg (for HTML output, which can also be interpreted by Word or Markdown in most usage scenarios), and screenreg (for ASCII text output in the console). These three functions serve to convert some cleaned-up information (coefficients, standard errors, confidence intervals, p-values, goodness-of-fit statistics, model labels etc.) into the respective output formats. This is not perfect and could be even more flexible, but I think it has quite a few arguments for customization at this point, including things like booktabs and dcolumn support. So the big challenge is rather to get cleaned-up model information in the first place.

这是通过为这三个函数中的任何一个提供texreg对象来完成的. texreg对象只是系数等的容器,并使用S4类正式定义.要创建texreg对象,可以使用构造函数createTexreg(如帮助页面中所述),该函数接受所有不同的信息作为参数,例如标准错误等.或者(更好)您可以使用extract函数从某些估计的模型中提取这些信息,并返回texreg对象以与这三个函数中的任何一个一起使用.通常的方法是将几个模型的列表移交给texregscreenreg等函数,此函数将在内部调用extract来创建texreg对象,然后处理来自这些对象.

This is done by providing a texreg object to any of these three functions. A texreg object is just a container for coefficients etc. and is formally defined using an S4 class. To create a texreg object, you can either use the constructor function createTexreg (as documented on the help pages), which accepts all the different pieces of information as arguments, such as the standard errors etc. Or (better) you can use the extract function to extract those pieces of information from some estimated model and return a texreg object for use with any of the three functions. The way you typically do this is by just handing over a list of several models to the texreg or screenreg etc. function, and this function will internally call extract to create texreg objects and then process the information from those objects.

但是,将extract函数的调用输出保存到一个对象,可能会操纵该texreg对象,然后在被操纵的对象上调用texreg函数以显示它,同样有效作为一张桌子.这样可以灵活地调整结果.

However, it is equally valid to just save the output of a call of the extract function to an object, possibly manipulate this texreg object, and then call the texreg function on the manipulated object to display it as a table. This permits some flexibility in tweaking the results.

之前,我提到该程序包使用通用函数.这意味着extract函数是通用的,因为您可以为其注册适用于任意类模型的方法.例如,如果extract函数不知道如何处理h2o对象以及如何从此类对象中提取相关信息,则可以编写一种方法来实现该功能,并在extract函数中注册它.下面,我将逐步引导您完成该过程,希望人们可以从这个详细的博览会中学到东西并开始编写自己的扩展. (注意:如果有人开发了一种有用的方法,请给我发送电子邮件,我可以将其包含在下一个texreg版本中.)还值得指出的是,该软件包的源文件包含70多个可用作模板的方法.这些示例存储在文件R/extract.R中.

Earlier on, I mentioned that the package uses generic functions. This means that the extract function is generic in the sense that you can register methods for it that work with arbitrary classes of models. For example, if the extract function does not know how to handle h2o objects and how to extract the relevant information from such an object, you can just write a method to do that and register it with the extract function. Below, I will walk you through this step by step in the hope that people will learn from this detailed exposition and start writing their own extensions. (Note: if somebody develops a useful method, please e-mail me and I can include it in the next texreg release.) It is also worth pointing out that the source files of the package contain more than 70 examples of extract methods which you can use as templates. These examples are stored in the file R/extract.R.

第一步是识别对象的类名.在您的示例中,class(model.output.1)返回以下类标签:"H2OBinomialModel"和"h2o".第一个标签是更具体的标签,第二个标签是更一般的标签.如果所有h2o模型对象的结构都相似,则为h2o对象编写扩展名,然后在方法内决定如何进行特定模型将是有意义的.由于我对h2o软件包几乎一无所知,因此我更喜欢在这种情况下从针对H2OBinomialModel对象的更具体的extract方法开始.以后可以根据需要进行调整.

The first step is to identify the class name of the object. In your example, class(model.output.1) returns the following class labels: "H2OBinomialModel" and "h2o". The first label is the more specific one, and the second label is the more general one. If all h2o model objects are structured in a similar way, it will make sense to write an extension for h2o objects and then decide within the method how to proceed with the specific model. As I know virtually nothing about the h2o package, I prefer to start out with a more specific extract method for H2OBinomialModel objects in this case. It can be adjusted later on should we wish to do so.

extract方法的结构如下:您编写一个名为extract.xyz的函数(用类标签替换"xyz"),至少具有一个名为model的参数,该参数接受模型对象(例如,<在您的示例中为c55>),在主体中放置一些代码,以从model对象中提取相关信息,使用createTexreg构造函数创建texreg对象,然后返回该对象.这是一个空容器:

extract methods are structured as follows: you write a function called extract.xyz (replace "xyz" by the class label), have at least one argument called model, which accepts the model object (e.g., model.output.1 in your example), put some code in the body that extracts the relevant information from the model object, create a texreg object using the createTexreg constructor, and return this object. Here is an empty container:

extract.H2OBinomialModel <- function(model, ...) {
  s <- summary(model)

  # extract information from model and summary object here

  # then create and return a texreg object (replace NULL with actual values):
  tr <- createTexreg(
    coef.names = NULL,    # character vector of coefficient labels
    coef = NULL,          # numeric vector with coefficients
    se = NULL,            # numeric vector with standard error values
    pvalues = NULL,       # numeric vector with p-values
    gof.names = NULL,     # character vector with goodness-of-fit labels
    gof = NULL,           # numeric vector of goodness-of-fit statistics
    gof.decimal = NULL    # logical vector: GOF statistic has decimal points?
  )
  return(tr)
}

请注意,函数定义还包含...参数,该参数可用于自定义参数,这些自定义参数应移交给extract方法内的函数调用.

Note that the function definition also contains the ... argument, which can be used for custom arguments that should be handed over to function calls within the extract method.

还请注意,函数定义主体中的第一行将模型摘要保存在名为s的对象中.这通常很有用,因为许多软件包编写者决定将一些信息存储在摘要中的更简单版本中,因此通常应将模型及其摘要都视为有用的信息源.在某些情况下,可能必须查看相应程序包中摘要方法的实际定义,以找出调用summary命令时摘要页上显示的信息是如何计算的,因为并非所有summary方法存储显示在summary对象中的不同元素.

Note also that the first line in the body of the function definition saves the summary of the model in an object called s. This is often useful because many package writers decide to store some of the information in a simpler version in the summary, so one should usually consider both, the model and its summary, as useful sources of information. In some cases, one may have to look at the actual definition of the summary method in the respective package to find out how the pieces of information displayed on the summary page are computed when the summary command is called because not all summary methods store the different elements displayed in the summary object.

下一步是检查对象并找到最终表中应显示的所有详细信息.通过查看model.output.1的输出,我猜想以下部分应该构成表格底部的GOF块:

The next step is to examine the object and locate all the details that should be displayed in the final table. By looking at the output of model.output.1, I would guess that the following part should constitute the GOF block at the bottom of the table:

MSE:  0.202947
R^2:  0.1562137
LogLoss:  0.5920097
Mean Per-Class Error:  0.3612191
AUC:  0.7185655
Gini:  0.4371311
Null Deviance:  512.2888
Residual Deviance:  449.9274
AIC:  457.9274

以下部分可能应该构成表格中间的系数块:

And the following part should probably constitute the coefficient block in the middle of the table:

Coefficients: glm coefficients
      names coefficients standardized_coefficients
1 Intercept    -1.835223                 -0.336428
2      RACE    -0.625222                 -0.193052
3     DCAPS     1.314428                  0.408336
4       PSA     0.046861                  0.937107

在许多情况下,摘要包含相关信息,但是在这里打印模型可以得到我们所需的信息.我们将需要在model.output.1对象(或其摘要,如果适用)中找到所有这些对象.为此,有几个有用的命令.其中有str(model.output.1)names(summary(model.output.1))和类似的命令.

In many cases, the summary contains the relevant information, but here printing the model yields what we need. We will need to locate all of this in the model.output.1 object (or its summary if applicable). To do that, there are several useful commands. Among them are str(model.output.1), names(summary(model.output.1)), and similar commands.

让我们从系数块开始.调用str(model)将显示S4对象中有一个名为model的插槽.我们可以通过调用model.output.1@model来查看其内容.结果是一个包含几个命名元素的列表,其中包括coefficients_table.因此,我们可以通过调用model.output.1@model$coefficients_table来访问系数表.结果是一个数据框,我们可以使用$运算符访问其列.特别是,我们需要名称和系数.这里有两种类型的系数,标准化的和非标准化的,以后我们可以在我们的提取方法中添加一个参数,让用户决定他或她想要什么.这是我们提取系数及其标签的方法:

Let's start with the coefficient block. Calling str(model) reveals that there is a slot called model in the S4 object. We can look at its contents by calling model.output.1@model. The result is a list with several named elements, among them coefficients_table. So we can access the coefficient table by calling model.output.1@model$coefficients_table. The result is a data frame the columns of which we can access using the $ operator. In particular, we need the names and the coefficients. There are two types of coefficients here, standardized and unstandardized, and we can add an argument to our extract method later to let the user decide what he or she wants. Here is how we extract the coefficients and their labels:

coefnames <- model.output.1@model$coefficients_table$names
coefs <- model.output.1@model$coefficients_table$coefficients
coefs.std <- model.output.1@model$coefficients_table$standardized_coefficients

据我所知,对象中没有存储任何标准错误或p值.如果愿意,我们可以编写一些额外的代码来计算它们,但是在这里,我们将重点介绍作为模型输出的一部分容易提供的内容.

As far as I can see, there are no standard errors or p-values stored in the object. We could write some additional code to compute them should we wish to do that, but here we will focus on things that are readily provided as part of the model output.

重要的是,我们不应覆盖R中的任何现有函数名称,例如namescoef.从技术上讲,这样做应该可以工作,因为代码稍后会在函数中执行,但是在尝试进行操作时,很容易导致混淆,因此您最好避免这种情况.

It is important that we should not overwrite any existing function names in R, such as names or coef. While doing this should technically work because the code is executed within a function later, this can easily lead to confusion while trying things out, so you should better avoid that.

接下来,我们需要找到拟合优度统计信息.通过仔细检查str(model.output.1)的输出,我们看到拟合优度统计信息包含在model@model$training_metrics@metrics下的几个插槽中.让我们将它们保存到一些更易于访问的对象中:

Next, we need to locate the goodness-of-fit statistics. By examining the output of str(model.output.1) carefully, we see that the goodness-of-fit statistics are contained in several slots under model@model$training_metrics@metrics. Let's save them to some objects that are easier to access:

mse <- model.output.1@model$training_metrics@metrics$MSE
r2 <- model.output.1@model$training_metrics@metrics$r2
logloss <- model.output.1@model$training_metrics@metrics$logloss
mpce <- model.output.1@model$training_metrics@metrics$mean_per_class_error
auc <- model.output.1@model$training_metrics@metrics$AUC
gini <- model.output.1@model$training_metrics@metrics$Gini
nulldev <- model.output.1@model$training_metrics@metrics$null_deviance
resdev <- model.output.1@model$training_metrics@metrics$residual_deviance
aic <- model.output.1@model$training_metrics@metrics$AIC

在某些情况下,但不是在此情况下,程序包的作者编写了通用函数的方法,这些方法可用于提取一些常用信息,例如观测值(nobs(model)),AIC(AIC(model)),BIC( BIC(model)),偏差(deviance(model))或对数似然(logLik(model)[[1]]).因此,您可能首先要尝试这些.但是h2o软件包似乎没有提供这种便捷方法.

In some cases, but not here, the author of a package writes methods for generic functions that can be used to extract some common information like the number of observations (nobs(model)), AIC (AIC(model)), BIC (BIC(model)), deviance (deviance(model)), or the log likelihood (logLik(model)[[1]]). So these are things you may want to try first; but the h2o package does not seem to offer such convenience methods.

现在我们已经找到了所需的所有信息,可以将它们添加到上面定义的extract.H2OBinomialModel函数中.

Now that we have located all pieces of information we need, we can add them to the extract.H2OBinomialModel function we defined above.

但是,我们希望让用户决定他还是她更喜欢原始系数还是标准化系数,并且我们希望让用户决定应该报告哪个拟合优度统计信息,因此我们在其中添加了各种逻辑参数函数标题,然后在函数内部使用if-conditions来检查是否应将相应的统计信息嵌入到生成的texreg对象中.

However, we would like to let the user decide if he or she prefers raw or standardized coefficients, and we would like to let the user decide which goodness-of-fit statistics should be reported, so we add various logical arguments to the function header and then use if-conditions inside the function to check whether we should embed the respective statistics in the resulting texreg object.

在这种情况下,我们也删除了行s <- summary(model),因为实际上我们不需要从摘要中获取任何信息,因为我们在模型对象中找到了所需的一切.

We also remove the line s <- summary(model) in this case because we don't actually need any kind of information from the summary since we found everything we need in the model object.

完整的功能可能如下所示:

The complete function may look like this:

# extension for H2OBinomialModel objects (h2o package)
extract.H2OBinomialModel <- function(model, standardized = FALSE, 
      include.mse = TRUE, include.rsquared = TRUE, include.logloss = TRUE, 
      include.meanerror = TRUE, include.auc = TRUE, include.gini = TRUE, 
      include.deviance = TRUE, include.aic = TRUE, ...) {

  # extract coefficient table from model:
  coefnames <- model@model$coefficients_table$names
  if (standardized == TRUE) {
    coefs <- model@model$coefficients_table$standardized_coefficients
  } else {
    coefs <- model@model$coefficients_table$coefficients
  }

  # create empty GOF vectors and subsequently add GOF statistics from model:
  gof <- numeric()
  gof.names <- character()
  gof.decimal <- logical()
  if (include.mse == TRUE) {
    mse <- model@model$training_metrics@metrics$MSE
    gof <- c(gof, mse)
    gof.names <- c(gof.names, "MSE")
    gof.decimal <- c(gof.decimal, TRUE)
  }
  if (include.rsquared == TRUE) {
    r2 <- model@model$training_metrics@metrics$r2
    gof <- c(gof, r2)
    gof.names <- c(gof.names, "R^2")
    gof.decimal <- c(gof.decimal, TRUE)
  }
  if (include.logloss == TRUE) {
    logloss <- model@model$training_metrics@metrics$logloss
    gof <- c(gof, logloss)
    gof.names <- c(gof.names, "LogLoss")
    gof.decimal <- c(gof.decimal, TRUE)
  }
  if (include.meanerror == TRUE) {
    mpce <- model@model$training_metrics@metrics$mean_per_class_error
    gof <- c(gof, mpce)
    gof.names <- c(gof.names, "Mean Per-Class Error")
    gof.decimal <- c(gof.decimal, TRUE)
  }
  if (include.auc == TRUE) {
    auc <- model@model$training_metrics@metrics$AUC
    gof <- c(gof, auc)
    gof.names <- c(gof.names, "AUC")
    gof.decimal <- c(gof.decimal, TRUE)
  }
  if (include.gini == TRUE) {
    gini <- model@model$training_metrics@metrics$Gini
    gof <- c(gof, gini)
    gof.names <- c(gof.names, "Gini")
    gof.decimal <- c(gof.decimal, TRUE)
  }
  if (include.deviance == TRUE) {
    nulldev <- model@model$training_metrics@metrics$null_deviance
    resdev <- model@model$training_metrics@metrics$residual_deviance
    gof <- c(gof, nulldev, resdev)
    gof.names <- c(gof.names, "Null Deviance", "Residual Deviance")
    gof.decimal <- c(gof.decimal, TRUE, TRUE)
  }
  if (include.aic == TRUE) {
    aic <- model@model$training_metrics@metrics$AIC
    gof <- c(gof, aic)
    gof.names <- c(gof.names, "AIC")
    gof.decimal <- c(gof.decimal, TRUE)
  }

  # create texreg object:
  tr <- createTexreg(
    coef.names = coefnames, 
    coef = coefs, 
    gof.names = gof.names, 
    gof = gof, 
    gof.decimal = gof.decimal
  )
  return(tr)
}

对于拟合优度块,您可以看到我首先创建了空向量,然后在填充了其他统计信息的情况下填充了这些向量,前提是用户使用相应的参数打开了相应的统计信息.

For the goodness-of-fit block, you can see that I first created empty vectors and then subsequently populated them with additional statistics provided that the respective statistic was switched on by the user using the respective argument.

gof.decimal逻辑矢量为每个GOF统计信息指示其是否具有小数位(TRUE)(例如,以观察数为单位,FALSE).

The gof.decimal logical vector indicates for each GOF statistic whether it has decimal places (TRUE) or not (FALSE, as in the number of observations, for example).

最后,需要将新功能注册为通用extract功能的方法.这是使用一个简单的命令完成的:

Finally, the new function needs to be registered as a method for the generic extract function. This is done using a simple command:

setMethod("extract", signature = className("H2OBinomialModel", "h2o"), 
definition = extract.H2OBinomialModel)

在这里,className的第一个参数是类标签,第二个参数是在其中定义类的程序包.

Here, the first argument of className is the class label, and the second one is the package in which the class is defined.

总而言之,编写自定义扩展名仅需要做两件事:1)编写提取方法,以及2)注册该方法.也就是说,该代码可以在运行时执行,而不必插入任何包中.

Summing up, the only two things that need to be done in order to write a custom extension are 1) writing an extract method, and 2) registering the method. That is, this code can be executed at runtime and doesn't have to be inserted into any package.

但是,为了方便起见,我在texreg版本1.36.13中添加了H2OBinomialModel方法,该方法在CRAN上可用.

However, for your convenience, I have added the H2OBinomialModel method to texreg version 1.36.13, which is available on CRAN.

请注意,此处提供的解决方案不适用于texreg的任何先前版本,因为先前版本无法处理既没有标准误差也没有置信区间的模型.我认为这是一个相当专业的设置,我没有遇到过一个仅提供估计值而没有任何不确定性度量的程序包.我现在已经在texreg中修复了该问题.

Note that the solution presented here does not work with any previous version of texreg because previous versions could not deal with models that had neither standard errors nor confidence intervals. This is a fairly specialized setup in my opinion, and I hadn't come across a package that merely provides the estimates without any uncertainty measures. I have now fixed this in texreg.

一旦在运行时执行了功能定义和setMethod命令,就可以使用以下命令创建表:

Once the function definition and the setMethod command have been executed at runtime, the following command can be used to create a table:

screenreg(list(model.output.1, model.output.2), custom.note = "")

这是输出:

======================================
                      Model 1  Model 2
--------------------------------------
Intercept              -1.84    -1.11 
RACE                   -0.63    -0.62 
DCAPS                   1.31     1.31 
PSA                     0.05     0.05 
AGE                             -0.01 
--------------------------------------
MSE                     0.20     0.20 
R^2                     0.16     0.16 
LogLoss                 0.59     0.59 
Mean Per-Class Error    0.36     0.38 
AUC                     0.72     0.72 
Gini                    0.44     0.44 
Null Deviance         512.29   512.29 
Residual Deviance     449.93   449.51 
AIC                   457.93   459.51 
======================================

这里的custom.note = ""参数是有意义的,因为我们不想要显着性的图例,因为模型没有报告任何不确定性度量.

The custom.note = "" argument makes sense here because we don't want a significance legend since the models do not report any uncertainty measures.

还可以抑制某些GOF度量和/或使用标准化系数:

It is also possible to suppress some of the GOF measures and/or use standardized coefficients:

screenreg(list(model.output.1, model.output.2), custom.note = "", 
    include.deviance = FALSE, include.auc = FALSE, standardized = TRUE)

结果:

======================================
                      Model 1  Model 2
--------------------------------------
Intercept              -0.34    -0.34 
RACE                   -0.19    -0.19 
DCAPS                   0.41     0.41 
PSA                     0.94     0.94 
AGE                             -0.07 
--------------------------------------
MSE                     0.20     0.20 
R^2                     0.16     0.16 
LogLoss                 0.59     0.59 
Mean Per-Class Error    0.36     0.38 
Gini                    0.44     0.44 
AIC                   457.93   459.51 
======================================

其他可与createTexreg

一起使用的插槽

在任何extract方法中都调用createTexreg构造函数.上面的示例显示了一些简单的信息.除了示例中包含的详细信息之外,texreg对象中还提供了以下插槽:

Other slots that can be used with createTexreg

The createTexreg constructor function is called within any extract method. The example above shows some simple pieces of information. Besides the details contained in the example, the following slots are available in texreg objects:

  • se:标准错误
  • pvalues:p值
  • ci.low:置信区间的下限
  • ci.up:置信区间的上限
  • model.name:当前模型的标题

请注意,应该使用置信区间或标准误差和p值,而不能同时使用.

Note that either confidence intervals or standard errors and p-values should be used, not both.

除了将模型直接移交给screenregtexreghtmlreg函数外,还可以先将提取的信息保存到texreg对象,然后在显示或保存表格之前对其进行操作. .附加值在于,即使对表进行复杂的更改也很容易以这种方式应用.例如,可以重命名系数或GOF统计信息,添加新行,重命名模型,修改值或更改系数或GOF统计信息的顺序.方法如下:首先,调用extract函数将信息保存到texreg对象中:

Besides handing over the models to the screenreg, texreg, or htmlreg functions directly, it is possible to save the extracted information to a texreg object first and manipulate it before the table is displayed or saved. The added value is that even complex changes to a table are easy to apply this way. For example, it is possible to rename coefficients or GOF statistics, add new rows, rename a model, modify the values, or change the order of coefficients or GOF statistics. Here is how you can do that: First, you call the extract function to save the information to a texreg object:

tr <- extract(model.output.1)

您可以通过调用tr来显示texreg对象的内容,这将显示以下输出:

You can display the contents of the texreg object by just calling tr, which shows the following output:

No standard errors and p-values were defined for this texreg object.

                coef.
Intercept -1.83522343
RACE      -0.62522179
DCAPS      1.31442834
PSA        0.04686106

                             GOF dec. places
MSE                    0.2029470        TRUE
R^2                    0.1562137        TRUE
LogLoss                0.5920097        TRUE
Mean Per-Class Error   0.3612191        TRUE
AUC                    0.7185655        TRUE
Gini                   0.4371311        TRUE
Null Deviance        512.2888402        TRUE
Residual Deviance    449.9273825        TRUE
AIC                  457.9273825        TRUE

或者,这是对象的结构,如str(tr)所示:

Alternatively, this is the structure of the object as shown by str(tr):

Formal class 'texreg' [package "texreg"] with 10 slots
  ..@ coef.names : chr [1:4] "Intercept" "RACE" "DCAPS" "PSA"
  ..@ coef       : num [1:4] -1.8352 -0.6252 1.3144 0.0469
  ..@ se         : num(0) 
  ..@ pvalues    : num(0) 
  ..@ ci.low     : num(0) 
  ..@ ci.up      : num(0) 
  ..@ gof.names  : chr [1:9] "MSE" "R^2" "LogLoss" "Mean Per-Class Error" ...
  ..@ gof        : num [1:9] 0.203 0.156 0.592 0.361 0.719 ...
  ..@ gof.decimal: logi [1:9] TRUE TRUE TRUE TRUE TRUE TRUE ...
  ..@ model.name : chr(0) 

现在,您可以以任意方式操作此对象,例如,添加GOF统计信息:

Now you can just manipulate this object in arbitrary ways, e.g., add a GOF statistic:

tr@gof.names <- c(tr@gof.names, "new statistic")
tr@gof <- c(tr@gof, 12)
tr@gof.decimal <- c(tr@gof.decimal, FALSE)

或者您可以更改系数的顺序:

Or you can change the order of coefficients:

tr@coef.names <- tr@coef.names[c(4, 1, 2, 3)]
tr@coef <- tr@coef[c(4, 1, 2, 3)]

完成操作后,可以在调用时移交texreg对象而不是原始模型,例如screenreg:

When you are done with the manipulations, you can hand over the texreg object instead of the original model when you call, for example, screenreg:

screenreg(list(tr, model.output.2), custom.note = "")

新结果将如下所示:

======================================
                      Model 1  Model 2
--------------------------------------
PSA                     0.05     0.05 
Intercept              -1.84    -1.11 
RACE                   -0.63    -0.62 
DCAPS                   1.31     1.31 
AGE                             -0.01 
--------------------------------------
MSE                     0.20     0.20 
R^2                     0.16     0.16 
LogLoss                 0.59     0.59 
Mean Per-Class Error    0.36     0.38 
AUC                     0.72     0.72 
Gini                    0.44     0.44 
Null Deviance         512.29   512.29 
Residual Deviance     449.93   449.51 
AIC                   457.93   459.51 
new statistic          12             
======================================

TL; DR

texreg可以由用户自定义.只需编写上面显示的提取方法,然后使用setMethods调用进行注册.我在最新的texreg版本 1.36.13中包含了H2OBinomialModel方法,并修正了一个错误使用没有标准错误(例如此错误)的模型.

TL;DR

texreg can be customized by users. Just write an extract method like the one shown above and register it using a setMethods call. I have included the H2OBinomialModel method in the latest texreg version 1.36.13, along with a bugfix for using models without standard errors (such as this one).

这篇关于打印“漂亮" R中的h2o模型的表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆