cactussss
cactussss

Reputation: 745

Change row names of a table obtained from a lm regression using xtable function

I obtained a summary table of a regression with couple variables. The code I used was this:

    stock = dyn$lm(y1 ~ x1 +lag(x2, -1) + x2 + x3 +x4)
    print(xtable(stock))

which gave me the following output:

    % latex table generated in R 3.0.1 by xtable 1.7-1 package
    % Mon Aug 12 21:01:51 2013
    \begin{table}[ht]
    \centering
    \begin{tabular}{rrrrr}
       \hline
     & Estimate & Std. Error & t value & Pr($>$$|$t$|$) \\ 
      \hline
     (Intercept) & 0.0031 & 0.0036 & 0.85 & 0.3951 \\ 
      x1 & 0.4947 & 0.0371 & 13.33 & 0.0000 \\ 
      lag(x2, -1) & 0.3745 & 0.0347 & 10.79 & 0.0000 \\ 
      x2 & -0.1248 & 0.0368 & -3.39 & 0.0007 \\ 
      x3 & 0.7368 & 0.0424 & 17.36 & 0.0000 \\ 
      x4 & -0.0033 & 0.0039 & -0.84 & 0.3983 \\ 
         \hline
       \end{tabular}
       \end{table}

I can only change the row names (which are x1,lag(x2,-1), etc.) to greeks manually to line up with the regression in my research. However, I need to replicate the regression with many different groups of data which makes it too time-consuming to do it one by one.

Is there a more automated/robust solution that works to customize the row names with code?

Upvotes: 3

Views: 1346

Answers (1)

MichaelChirico
MichaelChirico

Reputation: 34703

I think what you want to do is convert the summary to a matrix, then use the guide here: http://www.inside-r.org/packages/cran/xtable/docs/xtable

to add row and column names to the matrix programatically.

I'm just figuring this out for myself now, but something along these lines should work:

stock = dyn$lm(y1 ~ x1 + lag(x2, -1) + x2 + x3 +x4)
stock_matrix <- matrix(summary(stock)$coef, ncol = 4L)

rownames(stock_matrix) <- c("Intercept", "$x_{1,t}$", "$x_{2,t-1}$", "$x_2$", "$x_3$", "$x_4$")

print(xtable(stock_matrix), sanitize.text.function = identity)

By default, print.xtable escapes all LaTeX-recognized characters, such as $ and % to be sure your output compiles. We override this by providing a custom text sanitizer that leaves this alone by doing nothing.

Upvotes: 2

Related Questions