【问题标题】:Output a table with standard errors below estimates输出标准误差低于估计值的表格
【发布时间】:2013-10-17 18:01:06
【问题描述】:

所以我有一些像这样的参数估计

est<-matrix(1:10,nrow=2)
colnames(est)<-c("a","b","c","d","e")

我有一些像这样的标准错误

se<-matrix(seq(0.1,1,by=0.1),nrow=2)
colnames(se)<-c("a","b","c","d","e")

我想输出一个 Latex(或 HTML)表格,其中每个参数估计值在其下方的括号中都有其标准误差。

表格应该是这样的

    a    &   b    &  c    &  d    &  e
    1    &   3    &  5    &  7    &  9
  (0.1)  &  (0.3) & (0.5) & (0.7) & (0.9)
    2    &   4    &  6    &  8    &  10
  (0.2)  &  (0.4) & (0.6) & (0.8) & (1.0)

除了,你知道,在适当的乳胶(或 HTML)。我怎样才能从 R 中做到这一点?

【问题讨论】:

    标签: r latex sweave


    【解决方案1】:

    如果您不介意使用行标签,texreg 包可以提供解决方案:

    # your original code:
    est <- matrix(1:10, nrow = 2)
    colnames(est) <- c("a", "b", "c", "d", "e")
    se <- matrix(seq(0.1, 1, by = 0.1), nrow = 2)
    colnames(se) <- c("a", "b", "c", "d", "e")
    
    # add row labels:
    rownames(est) <- c("row 1", "row 2")
    rownames(se) <- c("row 1", "row 2")
    
    library("texreg")
    
    # create a texreg object:
    tr <- list()
    for (j in 1:ncol(est)) {
      tr[[j]] <- createTexreg(
          coef.names = rownames(est), 
          coef = est[, j], 
          se = se[, j]
      )
    }
    
    # for text output:
    screenreg(tr, custom.model.names = colnames(est), 
        custom.note = "")
    
    # for LaTeX output:
    texreg(tr, custom.model.names = colnames(est), 
        custom.note = "")
    
    # for HTML output:
    htmlreg(tr, custom.model.names = colnames(est), 
        custom.note = "")
    

    例如,文本输出如下所示:

    =============================================
           a       b       c       d       e     
    ---------------------------------------------
    row 1   1.00    3.00    5.00    7.00    9.00 
           (0.10)  (0.30)  (0.50)  (0.70)  (0.90)
    row 2   2.00    4.00    6.00    8.00   10.00 
           (0.20)  (0.40)  (0.60)  (0.80)  (1.00)
    =============================================
    

    您也可以通过为 screenreg 函数(inner.rule = "" 和 outer.rule = "")指定其他参数来省略顶部、底部和中间规则。

    请注意,您应该已安装 texreg (>= 1.29.7)。

    【讨论】:

      【解决方案2】:

      两步:

      用表格中的数据创建矩阵

      M <- matrix(as.vector(rbind(as.character(est),
                                  paste("(",as.vector(se),")", sep="")
                                  )
                   ), nrow=4)
      colnames(M) <- colnames(est)
      

      将矩阵写成 Latex 或 html 表格:

      library(xtable)
      print(xtable(M),type="latex") # or type="html" 
      

      【讨论】:

        【解决方案3】:

        看看 apsrtable 包是否适合你。根据您拥有的模型对象的类型,这可能是解决方案。该软件包也很容易扩展到其他型号。

        -----------------更新

        你为什么不只使用一个简单的 for 循环和一些粘贴命令?可能比找到一个通用的解决方案更容易做一些像这样稍微有点 hack-ish 的事情。

        est<-matrix(1:10,nrow=2)
        colnames(est)<-c("a","b","c","d","e")
        
        se<-matrix(seq(0.1,1,by=0.1),nrow=2)
        colnames(se)<-c("a","b","c","d","e")
        
        se <- apply(se, 2, function(i) paste('(', i, ')', sep=''))
        
        output <- NULL
        for (i in 1:nrow(est)){
          output <- rbind(output, est[i,])
          output <- rbind(output, se[i,])
        }
        output <- apply(output, 1, paste, collapse=' & ')
        output <- paste(output, '\\\\')
        cat(output, sep='\n')
        

        【讨论】:

        • 假设我没有模型对象。 (我的数据实际上是我描述的两个矩阵的形式。)我如何将它们合二为一?
        • 我之前的评论是针对这个问题的,而不是针对这个答案的。
        猜你喜欢
        • 2021-05-26
        • 1970-01-01
        • 2021-02-28
        • 1970-01-01
        • 1970-01-01
        • 2020-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多