Consider the following example
inds <- c('var1','','var2','')
model1 <- c(10.2,0.00,0.02,0.3)
model2 <- c(11.2,0.01,0.02,0.023)
df = df=data.frame(inds,model1,model2)
df
inds model1 model2
var1 10.20 11.200
0.00 0.010
var2 0.02 0.020
0.30 0.023
Here you have the output of a custom regression model with coefficients and P-values (I actually can show any other statistics if I need to, say, the standard errors of the coefficients).
There are two variables, var1
and var2
.
For instance, in model1, var1
comes with a coefficient of 10.2
and a P-value of 0.00
while var2
has a coefficient of 0.02
and a P-value of 0.30
.
Is there a package that handle these (custom) tables automatically and can create a neat Latex table with stars for significance?
Thanks!
Here is a solution using
texreg
.Note that
texreg
>= 1.36.18 is required.The information you are providing in the data frame (coefs and p-values) could be arranged in arbitrary ways in a data frame. Therefore we need to write code that selects these data from the appropriate places in the data frame and uses them to create a
texreg
object. As you are requesting a generic (and presumably re-usable) solution, we should wrap the code in a re-usable function. I'll call this functionextractFromDataFrame
. So here is the function, which extracts the information from the data frame and creates a list oftexreg
objects for the different models:In this function, we first define in which rows of the data frame the coefficients are stored and in which rows the p-values are stored. Then we created an empty list in which we stored the
texreg
objects. We iterate through all columns but the first as the first one contains only the labels. In each of these model columns, we save the coefficients, their names, and the p-values, and then we hand them over to thecreateTexreg
constructor, which is a function that creates atexreg
object for us based on the data. We add thetexreg
object to the list. In the end, we return the list oftexreg
objects.We can now apply the function to any data frame that looks like the one provided in the question, with arbitrary numbers of columns (> 1). In this case, after applying the function to the
df
object, we may want to print the contents of the list if we want to make sure that we did everything right:And indeed, the results contain the relevant data:
Now we can simply hand the list of
texreg
objects over toscreenreg
, e.g.,screenreg(tr)
, with the following result:Or to
htmlreg
for creating an HTML table. Or, as requested in the original question, totexreg
for creating a LaTeX table. The output oftexreg(tr, single.row = TRUE)
looks like this:This solution can be modified to accommodate standard errors, confidence intervals, or goodness-of-fit statistics.
Various
texreg
arguments can be used to customize the output, including the use of thebooktabs
package or decimal alignment viadcolumn
, for example.Please note that you should not call your data frame
df
because that object name is already defined in thestats
package.