输出一个标准误差低于估计值的表格

发布于 2024-10-24 05:05:57 字数 675 浏览 8 评论 0原文

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

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")

,我想输出一个乳胶(或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)

除了,你知道,在正确的 Latex(或 HTML)中。我怎样才能从 R 中做到这一点?

So I've got some parameter estimates like so

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

and I've got some standard errors like so

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

and I want to output a Latex (or HTML) table where each parameter estimate has its standard error in parens just below it.

The table should look something like this

    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)

except, you know, in proper Latex (or HTML). How can I do this from R?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓬勃野心 2024-10-31 05:05:57

如果您不介意有行标签,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)。

If you don't mind having row labels, the texreg package can offer a solution:

# 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 = "")

For example, the text output would look like this:

=============================================
       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)
=============================================

You can as well omit the top, bottom and mid rules by specifying additional arguments to the screenreg function (inner.rule = "" and outer.rule = "").

Note that you should have texreg (>= 1.29.7) installed.

疯狂的代价 2024-10-31 05:05:57

两步:

使用表中的数据创建矩阵

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

将矩阵写入乳胶或html表:

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

Two steps:

Create matrix with data in table

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

Write matrix as latex or html table:

library(xtable)
print(xtable(M),type="latex") # or type="html" 
混吃等死 2024-10-31 05:05:57

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

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

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

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')

See if the apsrtable package works for you. Depending on the kind of model object you have, this may be the solution. The package is quite easily extendable to other models too.

----------------- UPDATE

Why don't you just use a simple for loop and some paste commands? Probably easier to do something slightly hack-ish like this than to find a general solution.

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')
因为看清所以看轻 2024-10-31 05:05:57

在 R markdown 中,您可以使用语法 $$\hat{y}=\underset{(se)}{\beta1}$$

In R markdown, you can use the syntax $$\hat{y}=\underset{(se)}{\beta1}$$.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文