2011-03-19 18 views
2

所以我有一些參數估計像這樣輸出低於標準誤差表估計

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做到這一點?

回答

0

看看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') 
+0

假設我沒有模型對象。 (我的數據實際上是像我描述的兩個矩陣的形式。)我如何將它們合併爲一個? – njt 2011-03-19 16:00:03

+0

查看更新。你需要更好的東西嗎? – Vincent 2011-03-19 17:52:54

+0

-1從r-help發送過帳 – 2011-03-19 18:34:06

3

兩個步驟:

表創建一個數據矩陣

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