[R] Exporting summary plm results to latex

arun smartpink111 at yahoo.com
Thu Oct 11 21:59:07 CEST 2012


HI Sebastian,

Sorry, I found an error in my solution (the values and coefficients got mixed up in sorting).
Try this:

library(reshape)
extract.plm <- function(model) {
if (!class(model)[1] == "plm") {
stop("Internal error: Incorrect model type! Should be a plm object!")
}
zz1<-summary(model)$coef[,c(1,2,4)]
zz2<-as.data.frame(apply(zz1,2,function(x) sprintf("%.3f",x)))
zz2[]<-sapply(zz2,function(x) as.numeric(as.character(x)))
zz3<-data.frame(Coefficient=row.names(zz1),zz2)
zz3<-melt(zz3,by=Coefficient)
zz4<-within(zz3,{Coefficient<-as.character(Coefficient);variable<-as.character(variable)})
zz5<-ddply(zz4,.(Coefficient),function(x) x)
zz5$value[zz5$variable=="Estimate"]<-ifelse(zz5$value[zz5$variable=="Pr...t.."]<0.05 & zz5$value[zz5$variable=="Pr...t.."]>=0.01,gsub("(.*)","\\1*",zz5$value[zz5$variable=="Estimate"]),ifelse(zz5$value[zz5$variable=="Pr...t.."]<0.01,gsub("(.*)","\\1**",zz5$value[zz5$variable=="Estimate"]),zz5$value[zz5$variable=="Estimate"]))
zz5$value[zz5$variable=="Std..Error"]<-gsub("(.*)","(\\1)",zz5$value[zz5$variable=="Std..Error"])
zz6<-zz5[!zz5$variable=="Pr...t..",]
rownames(zz6)<-1:nrow(zz6)
 res<-zz6[,c(1,3)]
res
}
library(plm)
data("Produc", package = "plm")
zz <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp, data = Produc, index = c("state","year"))

extract.plm(zz)
#Using Coefficient as id variables
#  Coefficient    value
#1    log(emp)  0.768**
#2    log(emp)   (0.03)
#3     log(pc)  0.292**
#4     log(pc)  (0.025)
#5   log(pcap)   -0.026
#6   log(pcap)  (0.029)
#7       unemp -0.005**
#8       unemp  (0.001)
 summary(zz)$coef
#              Estimate   Std. Error    t-value      Pr(>|t|)
#log(pcap) -0.026149654 0.0290015755 -0.9016632  3.675200e-01
#log(pc)    0.292006925 0.0251196728 11.6246309  7.075069e-29
#log(emp)   0.768159473 0.0300917394 25.5272539 2.021455e-104
#unemp     -0.005297741 0.0009887257 -5.3581508  1.113946e-07


library(xtable)
xtable(extract.plm(zz))
Using Coefficient as id variables
% latex table generated in R 2.15.0 by xtable 1.7-0 package
% Thu Oct 11 15:28:12 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rll}
  \hline
 & Coefficient & value \\ 
  \hline
1 & log(emp) & 0.768** \\ 
  2 & log(emp) & (0.03) \\ 
  3 & log(pc) & 0.292** \\ 
  4 & log(pc) & (0.025) \\ 
  5 & log(pcap) & -0.026 \\ 
  6 & log(pcap) & (0.029) \\ 
  7 & unemp & -0.005** \\ 
  8 & unemp & (0.001) \\ 
   \hline
\end{tabular}
\end{center}
\end{table}


I used this example because your example is a bit restricted in the sense that there was only one independent variable.  In that case, some adjustments need to be made in the function:

#With your example dataset 

x <- rnorm(270)
y <- rnorm(270)
t <- rep(1:3,30)
i <- rep(1:90, each=3)
data <- data.frame(i,t,x,y)
fe <- plm(y~x,data=data,model="within")

extract.plm <- function(model) {

if (!class(model)[1] == "plm") {
stop("Internal error: Incorrect model type! Should be a plm object!")
}
tab1 <- summary(model)$coef[,1:2]
tab1[1]<-ifelse(summary(model)$coef[,4]<0.05& summary(model)$coef[,4]>=0.01, gsub("(.*)","\\1*",tab1[1]),ifelse(summary(model)$coef[,4]<0.01,gsub("(.*)","\\1**",tab1[1]),tab1[1]))
tab2<-melt(tab1)
row.names(tab2)[2]<-""
tab2<-within(tab2,{value=as.character(value)})
tab2[2,1]<-gsub("(.*)","(\\1)",sprintf("%.3f",as.numeric(as.character(tab2[2,1]))))
tab2
}
extract.plm(fe)
xtable(extract.plm(fe))

% latex table generated in R 2.15.0 by xtable 1.7-0 package
% Thu Oct 11 15:56:20 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rl}
  \hline
 & value \\ 
  \hline
Estimate & -0.154513026282509* \\ 
   & (0.074) \\ 
   \hline
\end{tabular}
\end{center}
\end{table}


I hope this helps.
A.K.




----- Original Message -----
From: Sebastian Barfort <sb3730 at nyu.edu>
To: Duncan Mackay <mackay at northnet.com.au>
Cc: r-help-r-project.org <r-help at r-project.org>
Sent: Wednesday, October 10, 2012 7:45 PM
Subject: Re: [R] Exporting summary plm results to latex

I am also interested in the standard errors, but beneath not next to the point estimates which is standard in the xtable package. 
If you by any chance remember the name of the package or how to do it that would be much appreciated!

Cheers,
Sebastian


On Oct 10, 2012, at 7:10 PM, Duncan Mackay <mackay at northnet.com.au> wrote:

> Hi
> 
> If you just want the coefficients.
> 
> xtable(summary(fe)$coef)
> % latex table generated in R 2.15.1 by xtable 1.7-0 package
> % Thu Oct 11 09:04:59 2012
> \begin{table}[ht]
> \begin{center}
> \begin{tabular}{rrrrr}
>  \hline
> & Estimate & Std. Error & t-value & Pr($>$$|$t$|$) \\
>  \hline
> x & 0.12 & 0.07 & 1.78 & 0.08 \\
>   \hline
> \end{tabular}
> \end{center}
> \end{table}
> 
> There is another package whose name eludes me which may help for tables which have different outputs to the output of lm etc
> 
> HTH
> 
> Duncan
> 
> Duncan Mackay
> Department of Agronomy and Soil Science
> University of New England
> Armidale NSW 2351
> Email: home: mackay at northnet.com.au
> 
> 
> 
> At 05:09 11/10/2012, you wrote:
>> HI,
>> 
>> May be you can use library(texreg):
>> 
>> library(plm)
>> 
>> #generating some data
>> x <- rnorm(270)
>> y <- rnorm(270)
>> t <- rep(1:3,30)
>> i <- rep(1:90, each=3)
>> 
>> data <- data.frame(i,t,x,y)
>> 
>> fe <- plm(y~x,data=data,model="within")
>> summary(fe)
>> library(texreg)
>> fe1<-extract.plm(fe) #extract the plm object
>> 
>> library(xtable)
>> 
>> xtable(do.call(rbind,lapply(fe1,function(x) data.frame(x))))
>> % latex table generated in R 2.15.0 by xtable 1.7-0 package
>> % Wed Oct 10 14:59:10 2012
>> \begin{table}[ht]
>> \begin{center}
>> \begin{tabular}{rr}
>>  \hline
>> & x \\
>>  \hline
>> Estimate & -0.03 \\
>>  Std. Error & 0.08 \\
>>  Pr($>$$|$t$|$) & 0.68 \\
>>  R\$\verb|^|2\$ & 0.00 \\
>>  Adj. R\$\verb|^|2\$ & 0.00 \\
>>  Num. obs. & 270.00 \\
>>   \hline
>> \end{tabular}
>> \end{center}
>> \end{table}
>> #Another example.  In this case, you can create two tables from the zz1 list
>> data("Produc", package = "plm")
>>    zz <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp, data = Produc, index = c("state","year"))
>> zz1<-extract.plm(zz)
>> 
>> 
>> lapply(lapply(zz1,function(x) data.frame(x)),xtable)
>> [[1]]
>> % latex table generated in R 2.15.0 by xtable 1.7-0 package
>> % Wed Oct 10 15:08:02 2012
>> \begin{table}[ht]
>> \begin{center}
>> \begin{tabular}{rrrr}
>>  \hline
>> & Estimate & Std..Error & Pr...t.. \\
>>  \hline
>> log(pcap) & -0.03 & 0.03 & 0.37 \\
>>  log(pc) & 0.29 & 0.03 & 0.00 \\
>>  log(emp) & 0.77 & 0.03 & 0.00 \\
>>  unemp & -0.01 & 0.00 & 0.00 \\
>>   \hline
>> \end{tabular}
>> \end{center}
>> \end{table}
>> 
>> [[2]]
>> % latex table generated in R 2.15.0 by xtable 1.7-0 package
>> % Wed Oct 10 15:08:02 2012
>> \begin{table}[ht]
>> \begin{center}
>> \begin{tabular}{rr}
>>  \hline
>> & x \\
>>  \hline
>> R\$\verb|^|2\$ & 0.94 \\
>>  Adj. R\$\verb|^|2\$ & 0.88 \\
>>  Num. obs. & 816.00 \\
>>   \hline
>> \end{tabular}
>> \end{center}
>> \end{table}
>> 
>> 
>> Hope it helps.
>> 
>> A.K.
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> ----- Original Message -----
>> From: Sebastian Barfort <sb3730 at nyu.edu>
>> To: r-help at r-project.org
>> Cc:
>> Sent: Wednesday, October 10, 2012 1:07 PM
>> Subject: [R] Exporting summary plm results to latex
>> 
>> Dear all,
>> 
>> I am trying to export my fixed effect results to Latex. I am using the plm package with the summary function. However, it does not look like apsrtable, stargazer, or any other package can accompany using the plm package.
>> 
>> I am interested in a classic table with the coefficient in one row followed by the standard error in paranthesis in the next row and stars by the coefficient to show relevant coefficient level.
>> 
>> coefficient 1     xxx**
>>            (xxx)
>> 
>> Here is a reproducible example:
>> 
>> library(plm)
>> 
>> #generating some data
>> x <- rnorm(270)
>> y <- rnorm(270)
>> t <- rep(1:3,30)
>> i <- rep(1:90, each=3)
>> 
>> data <- data.frame(i,t,x,y)
>> 
>> fe <- plm(y~x,data=data,model="within")
>> summary(fe)
>> 
>> If there is an alternative to using the plm package that works with any of the export to latex packages, I would be very interested to know. Otherwise, any ideas of how to solve this problem are very welcome. I almost exclusively use fixed effect panel models, and the problem of exporting results to Latex is one of the things preventing me from switching entirely from Stata to R.
>> 
>> 
>> Kind regards,
>> Sebastian
>> 
>> 
>>    [[alternative HTML version deleted]]
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>> 
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.





More information about the R-help mailing list