适用于sarlm类 [英] apsrtable for sarlm class

查看:39
本文介绍了适用于sarlm类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢 apsrtable(),并且发现扩展到其他类有点简单(特别是,我已经将其改编为 mlogit 对象.但是出于某些原因), apsrtableSummary.sarlm()函数不能像我编写的其他hack一样工作.

通常,我们需要重新定义系数矩阵,以便 apsrtable()知道在哪里可以找到它.的代码是

 "apsrtableSummary.sarlm"<-函数(x){s<-摘要(x)s $系数<-s $ Coef退货} 

我们还需要为新类重新定义 modelInfo ,如下所示:

  setMethod("modelInfo","summary.sarlm",function(x){env<-sys.parent()数字<-evalq(数字,envir = env)model.info<-列表("$ \\ rho $" = formatC(x $ rho,format ="f",digits = digits),"$ p(\\ rho)$" = formatC(x $ LR1 $ p.value,format ="f",digits = digits),"$ N $" =长度(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"返回(model.info)}) 

但是,在定义了这两个函数之后,对 apsrtable()的调用不会显示系数(MWE使用 spdep lagsarlm 中的示例>包装).

 库(spdep)库(可导入)数据(oldcol)COL.lag.eig<-lagsarlm(CRIME〜INC + HOVAL,数据= COL.OLD,nb2listw(COL.nb, style="W"), method="eigen")摘要(COL.lag.eig)#上面的加载函数apsrtable(COL.lag.eig)## 输出 ##\ begin {table} [!ht]\标题{}\标签{}\ begin {tabular} {l D {.} {.} {2}}\ hline&\ multicolumn {1} {c} {模型1} \\ \ hline%&型号1$ \ rho $ .rho&0.43 \\$ p(\ rho)$.0.00 \\$ N $&49 \\工商局374.78 \\\ mathcal {L}&-182.39 \\ \ hline\ multicolumn {2} {l} {\ footnotesize {括号中的标准错误}} \\\ multicolumn {2} {l} {\ footnotesize {$ ^ * $表示在$ p<0.05 $}}\ end {表格}\茶几} 

如您所见,除了系数和标准误差不存在以外,其他所有方法都非常有效.显然,摘要的重新定义是可行的,因为

  apsrtableSummary(COL.lag.eig)$系数估计标准误差z值Pr(> | z |)(截取)45.0792505 7.17734654 6.280768 3.369041e-10INC -1.0316157 0.30514297 -3.380762 7.228517e-04豪瓦-0.2659263 0.08849862 -3.004863 2.657002e-03 

我已经拔掉头发好几天了,试图找到解决方法.有提示吗?

解决方案

好吧,我想我可能是地球上唯一一个同时使用这两个软件包的人,但我想出了一种解决此问题的方法./p>

事实证明,错误源在 summary.sarlm 类对象的 coef 方法中.通常,此方法返回带有系数表的矩阵,但是对于此类,它仅返回系数.以下代码解决了该问题.

  setMethod("coef","apsrtableSummary.sarlm",函数(对象)object $ coefficients) 

我还发现将 rho 项作为模型系数很有用(方法不一致).

  apsrtableSummary.sarlm<-函数(x){s<-摘要(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)行名(s $ Coef)[1]<-"$ \\ rho $"s $系数<-s $ Coef退货} 

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?

解决方案

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)
}

这篇关于适用于sarlm类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆