Manually create Latex Regression Table from R

user1172558

I have a regression table that I want to export to Latex. But the object I'm working with does not support any traditional package like 'stargazer', or 'texreg'. I have to build it manually. This are the results:

                   Estimate   Std. Error    z value
(Intercept):1 -4.652887e-01 3.149742e-01  -1.477228
x1            -3.277175e-05 1.014935e-05  -3.228952
x2            -2.446376e-05 6.717509e-06  -3.641790
x3             1.666235e+00 5.228731e-01   3.186691
x4             2.507294e-08 5.276769e-09   4.751570

What I want is:

(Intercept)   -4.652887e-01 
              (3.149742e-01)
x1            -3.277175e-05 
              (1.014935e-05)
x2            -2.446376e-05 
              (6.717509e-06)
x3             1.666235e+00
              (5.228731e-01)
x4             2.507294e-08
              (5.276769e-09)

I could do this manually (see below), but I was looking for something more dynamic that I could apply in several contexts.

res <- matrix(NA, nrow=2*dim(ctable), ncol=1)

res[1,] <- ctable[1,1]
res[3,] <- ctable[2,1]
res[5,] <- ctable[3,1]
res[7,] <- ctable[4,1]
res[9,] <- ctable[5,1]

res[2,] <- ctable[1,2]
res[4,] <- ctable[2,2]
res[6,] <- ctable[3,2]
res[8,] <- ctable[4,2]
res[10,] <- ctable[5,2]

Thanks!

Djongs

I dont know if I understood well your problem. But if you want just create your table dynamically, you can use the function below.

create_table = function(table_)
{
    p_table = data.frame(Variables=NA, Values=NA)
    for(i in 1:dim(table_)[1])
    {
        est_error = table_[i,1:2] # Im assuming that you know that 2 first columns are the values that you want
        name = c(rownames(table_)[i], '') # The final table rownames
        name_values = cbind(Variables=name, Values=est_error)
        p_table = rbind(p_table, name_values)
    }
    rownames(p_table) = 1:dim(p_table)[1]
    return(na.omit(p_table))
}

And you can use this function in the following way. Below, one example using the mtcars dataset.

ctable = summary(lm(mpg~. , data = mtcars))$coefficients
output_table = create_table(ctable)

The output is something like you posted. Then, is easy create a tex table from a R dataframe using the xtable package.

xtable::xtable(output_table)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Manually create model for single table in Entity Framework

来自分类Dev

How to manually create and load namespaces in R

来自分类Dev

Create table stats from Json

来自分类Dev

R图中的LaTeX命令

来自分类Dev

Latex,RenderTable in Shiny,R

来自分类Dev

Attempting to create anova table with unequal sizes R

来自分类Dev

R markdown 与 Latex 方程的对齐

来自分类Dev

How to plot quadratic regression in R?

来自分类Dev

How to extract value from table function in R

来自分类Dev

R - extract all strings matching pattern and create relational table

来自分类Dev

R降价等效于LaTeX \ texttt吗?

来自分类Dev

R:LaTex的t.test输出

来自分类Dev

将glht导出到R中的LaTeX

来自分类Dev

从R Markdown到LaTeX的错误转换

来自分类Dev

如何从R插入数字到LaTeX?

来自分类Dev

R:创建不含环境代码的Latex表

来自分类Dev

自动换行编织xtable(R / Latex)

来自分类Dev

R Markdown内联LaTeX公式:`$`...`$`vs`\(`...`\)`

来自分类Dev

LaTeX的R Markdown错误-显然不是$ $问题

来自分类Dev

R:创建不含环境代码的Latex表

来自分类Dev

R:将CrossTable导出到Latex

来自分类Dev

LaTeX和R的最佳设置是什么?

来自分类Dev

R块中的Eval LaTeX代码(Knitr)

来自分类Dev

在 R 中使用 LaTeX 获取图例或标题

来自分类Dev

Get LogCat From Android Device Manually

来自分类Dev

Manually parsing parameters from put request in tomcat

来自分类Dev

R,bookdown包,将LaTex强制放入R块中

来自分类Dev

Remove rows conditionally from a data.table in R

来自分类Dev

转置表格,使用Hmisc包中的latex()从R导出到Latex