Question

I love apsrtable(), and have found it somewhat simple to extend to other classes (in particular, I have adapted it for mlogit objects. But for some reason, the apsrtableSummary.sarlm() function doesn't work quite like other hacks I have written.

Typically, we need to redefine the coefficients matrix so that apsrtable() knows where to find it. The code for this is

"apsrtableSummary.sarlm" <- function (x){
  s <- summary(x)
  s$coefficients <- s$Coef
  return(s)
}

We also need to redefine the modelInfo for the new class, like this:

setMethod("modelInfo", "summary.sarlm", function(x){
  env <- sys.parent()
  digits <- evalq(digits, envir=env)
  model.info <- list(
    "$\\rho$" = formatC(x$rho, format="f", digits=digits),
    "$p(\\rho)$" = formatC(x$LR1$p.value, format="f", digits=digits),
    "$N$" = length(x$fitted.values),
    "AIC" = formatC(AIC(x), format="f", digits=digits),
    "\\mathcal{L}" = formatC(x$LL, format="f", digits=digits)
  )
  class(model.info) <- "model.info"
  return(model.info)
})

After defining these two functions however, a call to apsrtable() doesn't print the coefficients (MWE using example from lagsarlm in spdep package).

library(spdep)
library(apsrtable)
data(oldcol)
COL.lag.eig <- lagsarlm(CRIME ~ INC + HOVAL, data=COL.OLD,
                         nb2listw(COL.nb, style="W"), method="eigen")
summary(COL.lag.eig)
# Load functions above
apsrtable(COL.lag.eig)

## OUTPUT ##
\begin{table}[!ht]
\caption{}
\label{} 
\begin{tabular}{ l D{.}{.}{2} } 
\hline 
  & \multicolumn{ 1 }{ c }{ Model 1 } \\ \hline
 %                           & Model 1 \\
 $\rho$.rho                 & 0.43   \\ 
$p(\rho)$.Likelihood ratio & 0.00   \\ 
$N$                         & 49     \\ 
AIC                         & 374.78 \\ 
\mathcal{L}                & -182.39 \\ \hline
 \multicolumn{2}{l}{\footnotesize{Standard errors in parentheses}}\\
\multicolumn{2}{l}{\footnotesize{$^*$ indicates significance at $p< 0.05 $}} 
\end{tabular} 
 \end{table}

As you can see, everything works out great except for that the coefficients and standard errors are not there. It's clear that the summary redefinition works, because

apsrtableSummary(COL.lag.eig)$coefficients
              Estimate Std. Error   z value     Pr(>|z|)
(Intercept) 45.0792505 7.17734654  6.280768 3.369041e-10
INC         -1.0316157 0.30514297 -3.380762 7.228517e-04
HOVAL       -0.2659263 0.08849862 -3.004863 2.657002e-03

I've pulled my hair out for several days trying to find a way out of this. Any tips?

Was it helpful?

Solution

Well, I think I may be the only person on earth who uses both of these packages together, but I figured out a way to work through this problem.

It turns out that the source of the error is in the coef method for summary.sarlm class objects. Typically this method returns a matrix with the coefficients table, but for this class it just returns the coefficients. The following code fixes that problem.

setMethod("coef", "apsrtableSummary.sarlm", function(object) object$coefficients)

I also found it useful to include the rho term as a model coefficient (the methods are not consistent on this).

apsrtableSummary.sarlm <- function (x){
  s <- summary(x)
  s$rholine<- c(unname(s$rho), s$rho.se, unname(s$rho/s$rho.se),
                unname(2 * (1 - pnorm(abs(s$rho/s$rho.se)))))
  s$Coef <- rbind(s$rholine, s$Coef)
  rownames(s$Coef)[1] <- "$\\rho$"
  s$coefficients <- s$Coef
  return(s)
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top