texreg表格对空间滞后模型的影响

时间:2017-08-31 00:09:27

标签: r latex texreg spdep spatial-regression

我使用spdep使用Durbin滞后模型运行空间回归。这种类型的模型返回每个回归系数及其显着性水平的直接,间接和总效应。

是否有像texreg这样的R库以一种很好的方式组织Durbin滞后模型的输出,其中包含有关直接,间接和总效果的信息?

可重复的例子:

library(spdep)

example(columbus)
listw <- nb2listw(col.gal.nb)

# spatial regression - Durbin Model
  mobj <- lagsarlm(CRIME ~ INC + HOVAL, columbus, listw, type="mixed")
  summary(mobj)

# Calculate direct and indirect impacts
  W <- as(listw, "CsparseMatrix")
  trMatc <- trW(W, type="mult")
  trMC <- trW(W, type="MC")
  imp <- impacts(mobj, tr=trMC, R=100) 
  sums <- summary(imp, zstats=T)

# Return Effects
  data.frame(sums$res)

# Return p-values
  data.frame(sums$pzmat)

1 个答案:

答案 0 :(得分:2)

我不确定是否有现有的函数可以为这种类型的模型对象创建漂亮的表格,但是(通过一些努力)你可以自己动手。

下面是一个rmarkdown文档,其中包含您的代码以及另外三个代码块。第一个结合了系数和p值数据。接下来的两个为latex表生成两个不同的选项。

我使用sums$ressums$pzmat表示值,tidyverse函数用于组合系数估算值和p值并编辑列名称,以及kablekableExtra包用于生成乳胶输出。

rmarkdown文件

---
title: "Coefficient Table for Durbin Lag Model"
author: "eipi10"
date: "8/30/2017"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message=FALSE, warning=FALSE)

library(spdep)
library(texreg)

example(columbus)
listw <- nb2listw(col.gal.nb)
```

```{r}
# spatial regression - Durbin Model
  mobj <- lagsarlm(CRIME ~ INC + HOVAL, columbus, listw, type="mixed")
  #summary(mobj)

# Calculate direct and indirect impacts
  W <- as(listw, "CsparseMatrix")
  trMatc <- trW(W, type="mult")
  trMC <- trW(W, type="MC")
  imp <- impacts(mobj, tr=trMC, R=100) 
  sums <- summary(imp, zstats=T)

# Return Effects
#  data.frame(sums$res)

# Return p-values
#  data.frame(sums$pzmat)
```

```{r extractTableData}
library(knitr)
library(kableExtra)
library(dplyr)
library(tidyr)
library(stringr)

# Extract coefficients and p-values
tab = bind_rows(sums$res) %>% t %>% as.data.frame %>% 
  setNames(., names(sums$res[[1]])) %>%
  mutate(Coef_Type=str_to_title(rownames(.)),
         Value_Type="Estimate") %>% 
  bind_rows(sums$pzmat %>% t %>% as.data.frame %>% 
              mutate(Coef_Type=rownames(.),
                     Value_Type="p-value")) %>% 
  gather(key, value, INC, HOVAL) 
```

```{r table1}
# Reshape table into desired output format
tab1 = tab %>% 
  unite(coef, key, Value_Type) %>% 
  spread(coef, value) %>% 
  mutate_if(is.numeric, round, 3)

# Change column names to what we want to see in the output table
names(tab1) = c("", gsub(".*_(.*)", "\\1", names(tab1)[-1]))

# Output latex table, including extra header row to mark coefficient names
kable(tab1, booktabs=TRUE, format="latex") %>% 
  add_header_above(setNames(c("", 2, 2), c("", sort(rownames(sums$pzmat))))) %>% 
  kable_styling(position="center")
```

\vspace{1cm}

```{r table2}
# Reshape table into desired output format
tab2 = tab %>% 
  unite(coef, Coef_Type, Value_Type) %>% 
  spread(coef, value) %>% 
  mutate_if(is.numeric, round, 3)

# Change column names to what we want to see in the output table
names(tab2) = c("Coefficient", gsub(".*_(.*)", "\\1", names(tab2)[-1]))

# Output latex table, including extra header row to mark coefficient names
kable(tab2, booktabs=TRUE, format="latex") %>% 
  add_header_above(setNames(c(" ", rep(2, 3)), c("", colnames(sums$pzmat)))) %>% 
  kable_styling(position="center")
```

PDF输出文件

enter image description here