我正在进行大量 GMM 回归,需要报告所有测试。我为此使用 plm 和 stargazer 包。
但是stargazer不直接提供这个测试,所以我写了很长的代码来添加这个测试。
我想向一个函数提供方程的名称,该函数进行所有这些测试并将它们直接添加到stargazer中。有人可以帮我构建这个功能吗?我相信它会帮助很多遇到同样问题的人。 :-)
library(plm)
library(stargazer)
library(stringr)
data("EmplUK")
#these are my two GMM equations, "z1" and "z2" - from PLM manual
## Arellano Bond 91 table 4b
z1 <- pgmm(dynformula(log(emp)~log(wage)+log(capital)+log(output),list(2,1,0,1)),
data=EmplUK, effect="twoways", model="twosteps",
gmm.inst=~log(emp),lag.gmm=list(c(2,99)))
## Blundell and Bond tab4 (cf DPD for OX p.12 col.4)
z2 <- pgmm(dynformula(log(emp)~log(wage)+log(capital),list(1,1,1)),
data=EmplUK, effect="twoways", model="onestep",
gmm.inst=~log(emp)+log(wage)+log(capital),lag.gmm=c(2,99),
transformation="ld")
###########################################
#saving results of equation z1 and z2 - i would like to make a function on this part
#########################################
Z1_summary<-summary(z1,robust=TRUE)
Z2_summary<-summary(z2,robust=TRUE)
#saving instruments
Z1_inst <- "~log(emp),lag.gmm=list(c(2,99))"
Z2_Inst <- "~log(emp)+log(wage)+log(capital),lag.gmm=c(2,99)"
Instruments <- c("Instr",Z1_inst,Z2_Inst)
#saving sargan results
sargan_est <- c("Sargan",
round(Z1_summary$sargan$statistic,4),
round(Z2_summary$sargan$statistic, 4))
sargan_p <- c("Sargan (p-value)",
round(Z1_summary$sargan$p.value, 4),
round(Z2_summary$sargan$p.value, 4))
good_sargan<-c("Conclusion about Sargan",
ifelse(round(Z1_summary$sargan$p.value, 4)>0.05, "Good", "Bad"),
ifelse(round(Z2_summary$sargan$p.value, 4)>0.05, "Good", "Bad"))
#saving ar1 results
AR1_est <- c("AR(1)",
round(Z1_summary$m1$statistic, 2),
round(Z2_summary$m1$statistic, 2))
AR1_pvalue <- c("AR(1) p-value",
round(Z1_summary$m1$p.value, 4),
round(Z2_summary$m1$p.value, 4))
#saving ar2 results
AR2_est <- c("AR(2)",
round(Z1_summary$m2$statistic, 2),
round(Z2_summary$m2$statistic, 2))
AR2_pvalue <- c("AR(2) p-value",
round(Z1_summary$m2$p.value, 4),
round(Z2_summary$m2$p.value, 4))
#saving wald results
Wald_Coef_est <- c("Wald Coef (df)",
str_c(round(Z1_summary$wald.coef$statistic, 2), " (", Z1_summary$wald.coef$parameter, ")"),
str_c(round(Z2_summary$wald.coef$statistic, 2), " (", Z2_summary$wald.coef$parameter, ")")
)
Wald_Coef_pvalue <- c("Wald Coef p-value",
round(Z1_summary$wald.coef$p.value, 4),
round(Z2_summary$wald.coef$p.value, 4))
Wald_Tim_est <- c("Wald Time (df)",
str_c(round(Z1_summary$wald.td$statistic, 2), " (", Z1_summary$wald.td$parameter, ")"),
str_c(round(Z2_summary$wald.td$statistic, 2), " (", Z2_summary$wald.td$parameter, ")")
)
Wald_Tim_pvalue <- c("Wald Time p-valor",
round(Z1_summary$wald.td$p.value, 4),
round(Z2_summary$wald.td$p.value, 4))
###########################
#showing results with stargazer
stargazer(z1,z2,
column.labels = c("Eq z1", "Eq z2"),
title= "2 GMM equations",
align = TRUE,
type = "text",
digits = 4,
no.space = TRUE,
add.lines = list(Instruments,
sargan_est,
sargan_p,
good_sargan,
AR1_est,
AR1_pvalue,
AR2_est,
AR2_pvalue,
Wald_Coef_est,
Wald_Coef_pvalue,
Wald_Tim_est,
Wald_Tim_pvalue),
notes="Sargan is considered good (acceptable) if its p-value is above 5%")
我认为编写函数不是一个好主意。下面是可以针对函数进行适当修改的代码。
# writing code to make a function to add more information to the stargazer GMM table
# will only involve wrapping this around a function and naming the function appropriately
# and some changes to get the final stargazer output
# name here refers to the common name of the models which in the above example is z
# this can be one argument of the function you write
name <- "m"
# I assume that there are 5 different models
# so the value can be modified accordingly
k <- 5
sargan_est <- NULL
sargan_p <- NULL
AR1_est <- NULL
AR1_pvalue <- NULL
AR2_est <- NULL
AR2_pvalue <- NULL
Wald_Coef_est <- NULL
Wald_Coef_pvalue <- NULL
sargan_est[1] <- c("Sargan")
sargan_p[1] <- c("Sargan (p-value)")
AR1_est[1] <- c("AR(1)")
AR1_pvalue[1] <- c("AR(1) p-value")
AR2_est[1] <- c("AR(2)")
AR2_pvalue[1] <- c("AR(2) p-value")
Wald_Coef_est[1] <- c("Wald Coef (df)")
Wald_Coef_pvalue[1] <- c("Wald Coef p-value")
for(i in 1:k){
assign(paste0(name,i,"_summary"),summary(get(paste0(name,i)),robust=TRUE))
sargan_est[i+1] <- c(round(get(paste0(name,i,"_summary"))$sargan$statistic,4))
sargan_p[i+1] <- c(round(get(paste0(name,i,"_summary"))$sargan$p.value, 4))
AR1_est[i+1] <- c(round(get(paste0(name,i,"_summary"))$m1$statistic, 2))
AR1_pvalue[i+1] <- c(round(get(paste0(name,i,"_summary"))$m1$p.value, 4))
AR2_est[i+1] <- c(round(get(paste0(name,i,"_summary"))$m2$statistic, 2))
AR2_pvalue[i+1] <- c(round(get(paste0(name,i,"_summary"))$m2$p.value, 4))
Wald_Coef_est[i+1] <- paste0(round(get(paste0(name,i,"_summary"))$wald.coef$statistic, 2)," (", get(paste0(name,i,"_summary"))$wald.coef$parameter, ")")
Wald_Coef_pvalue[i+1] <- c(round(get(paste0(name,i,"_summary"))$wald.coef$p.value, 4))
}
# you must change the following code to correctly indicate the number of models that you are considering
# I have not included some of the information that was put by the OP
# but it is straightforward to suitably mmodify the model to do the same
stargazer(m1,m2,m3,m4,m5,
column.labels = c("(1)", "(2)","(3)","(4)","(5)"),
title= "GMM Panel Regression Models",
model.numbers = FALSE,
header = FALSE,
align = TRUE,
type = "text",
digits = 3,
no.space = TRUE,
add.lines = list(sargan_est,
sargan_p,
AR1_est,
AR1_pvalue,
AR2_est,
AR2_pvalue,
Wald_Coef_est,
Wald_Coef_pvalue))
请注意,我修改了OP的代码,并且没有包含一些信息。但是,修改此代码以满足任何需要都是很简单的。