panelmodel (plm) 对象上的 texreg;额外的政府信息

Eri*_*ail 6 latex r texreg plm

如何在调用中自定义拟合优度 (

我有一些想要显示的模型,但我只得到"Num. obs.", "Adj. R^2",, "R^2"(参见下面的工作示例)。我会像howver所有显示器小nTF-statistic,和p-value所有的东西,我在默认获得summary()的呼叫。

我得到的一个例子。首先是一些数据和需要的包,

# install.packages(c("wooldridge", "plm", "texreg"), dependencies = TRUE)
library(wooldridge) 
data(wagepan)
library(plm) 
Run Code Online (Sandbox Code Playgroud)

二、部分机型,

POLS <- plm(lwage ~ educ + black + hisp + exper+I(exper^2)+ married + union +
            factor(year), data = wagepan, index=c("nr","year") , model="pooling")

RE <- plm(lwage ~ educ + black + hisp + exper + I(exper^2) + married + union +
          factor(year), data = wagepan, index = c("nr","year") , model = "random") 

FE <- plm(lwage ~ I(exper^2) + married + union + factor(year), 
          data = wagepan, index = c("nr","year"), model="within")
Run Code Online (Sandbox Code Playgroud)

第三,我当前的 调用及其输出,

# library(texreg)                      
texreg::screenreg(list(POLS, RE, FE), custom.coef.map = list('married' = 'Marrtied', 'union' = 'Union'))
#> ================================================
#>            Model 1      Model 2      Model 3    
#> ------------------------------------------------
#> Marrtied      0.11 ***     0.06 ***     0.05 *  
#>              (0.02)       (0.02)       (0.02)   
#> Union         0.18 ***     0.11 ***     0.08 ***
#>              (0.02)       (0.02)       (0.02)   
#> ------------------------------------------------
#> R^2           0.19         0.18         0.18    
#> Adj. R^2      0.19         0.18         0.06    
#> Num. obs.  4360         4360         4360       
#> ================================================
#> *** p < 0.001, ** p < 0.01, * p < 0.05
Run Code Online (Sandbox Code Playgroud)

我确实尝试添加, include.fstatistic = TRUE,但似乎我无法那样做。因为我需要一些额外的定制。

我的目标是这样的,

#> ------------------------------------------------
#> Obs.  (N)   4360         4360         4360       
#> Indiv.(n)    545          545          545       
#> Time  (T)      8            8            8       
#> R^2           0.19         0.18         0.18    
#> Adj. R^2      0.19         0.18         0.06    
#> F-stat       72.458       68.4124      83.8515  
#> P-value        (2.22e-16)   (2.22e-16)   (2.22e-16) 
#> ================================================
#> *** p < 0.001, ** p < 0.01, * p < 0.05
Run Code Online (Sandbox Code Playgroud)

jay*_*.sf 2

你可以使用 破解它texreg::extract()

为了获得“小n”,我们首先需要一个小函数。

getIndex <- function(fit){
  # extracts number of factor levels of index variables
  # from raw data used in models
  index.names <- as.character(as.list(summary(fit)$call)$index)[-1]
  if (length(index.names == 1)){
    df.name <- as.character(as.list(summary(fit)$call)$data)
    index.df <- get(df.name)[, index.names]
    length(unique(index.df))
  }
  if (length(index.names == 2)){
    df.name <- as.character(as.list(summary(fit)$call)$data)
    index.df <- get(df.name)[, index.names]
    cbind(length(unique(index.df[, 1])), 
          length(unique(index.df[, 2]))) 
  } else {
    stop("no index variables specified in model")
  }

}
Run Code Online (Sandbox Code Playgroud)

然后继续提取。

fv.1 <- summary(POLS)$fstatistic$statistic  # get F statistic
pv.1 <- summary(POLS)$fstatistic$p.value  # get p value
ns.1 <- getIndex(POLS)[1]  # get small n
tm.1 <- getIndex(POLS)[2]  # get times

library(texreg)
ex.1 <- extract(POLS)  # extract coefficients and GOF measures
ex.1@gof.names <- c(ex.1@gof.names[1:3],"Indiv.(n)", "Time (T)", 
                    "F-stat", "P-value")  # the GOF names
ex.1@gof <- c(ex.1@gof[1:3], ns.1, tm.1, fv.1, pv.1)  # the GOF values
ex.1@gof.decimal <- c(ex.1@gof.decimal[1:3], FALSE, FALSE, TRUE, TRUE)  # numeric or integer

fv.2 <- summary(RE)$fstatistic$statistic
pv.2 <- summary(RE)$fstatistic$p.value
ns.2 <- getIndex(RE)[1]
tm.2 <- getIndex(RE)[2]

ex.2 <- extract(RE)
ex.2@gof.names <- c(ex.2@gof.names[1:3],"Indiv.(n)", "Time (T)", 
                    "F-stat", "P-value")
ex.2@gof <- c(ex.2@gof[1:3], ns.2, tm.2, fv.2, pv.2)
ex.2@gof.decimal <- c(ex.2@gof.decimal[1:3], FALSE, FALSE, TRUE, TRUE)

fv.3 <- summary(FE)$fstatistic$statistic
pv.3 <- summary(FE)$fstatistic$p.value
ns.3 <- getIndex(FE)[1]
tm.3 <- getIndex(FE)[2]

ex.3 <- extract(FE)
ex.3@gof.names <- c(ex.3@gof.names[1:3],"Indiv.(n)", "Time (T)", 
                    "F-stat", "P-value")
ex.3@gof <- c(ex.3@gof[1:3], ns.3, tm.3, fv.3, pv.3)
ex.3@gof.decimal <- c(ex.3@gof.decimal[1:3], FALSE, FALSE, TRUE, TRUE)
Run Code Online (Sandbox Code Playgroud)

屈服

> screenreg(list(ex.1, ex.2, ex.3))

=======================================================
                  Model 1      Model 2      Model 3    
-------------------------------------------------------
[TRUNCATED...]
-------------------------------------------------------
R^2                  0.19         0.18         0.18    
Adj. R^2             0.19         0.18         0.06    
Num. obs.         4360         4360         4360       
Indiv.(n)          545          545          545       
Time (T)             8            8            8       
F-stat              72.46        68.41        83.85    
P-value              0.00         0.00         0.00    
=======================================================
*** p < 0.001, ** p < 0.01, * p < 0.05
Run Code Online (Sandbox Code Playgroud)

查看例如str(extract(FE))将此应用于其他 GOF。

要将其包装到函数中,请查看 @Neal Fultz 的答案中的代码。