R:向表输出添加脚注

时间:2017-09-07 20:00:09

标签: r function dataframe

我,一个R初学者,创建了以下可重现的示例代码,用于输出各种变量的ADF测试的测试统计信息:

library(urca)
data(nporg)
df <- nporg[complete.cases(nporg), ]

table_adf = function(x) {

  for (i in 1:x) { #something is wrong here
     out1 <- ur.df(x, type = "drift", selectlags = "AIC")@teststat[1]
     out2 <- ur.df(x, type = "trend", selectlags = "AIC")@teststat[1]
     out <- c(out1,out2)
     return(data.frame(matrix(out, nrow=1, dimnames=list(NULL, names(out)))))
  }

  footnote <- rep("", 3)
  footnote[1] <- c("Lag length determined with the AIC information criteria.")
  footnote[2] <- c(paste("ADF critical values drift:", out1@cval[[1]], out1@cval[[2]], out1@cval[[3]]))
  footnote[3] <- c(paste("ADF critical values trend:", out2@cval[[2]], out2@cval[[2]], out2@cval[[3]]))
  print(footnote) # something is wrong here, too
}

rbind(
  table_adf(df$emp),
  table_adf(df$cpi),
  table_adf(df$sp)
)

我的问题: 如何打印表格下方的脚注,如果可能,还要将每个变量名称添加到行中?

如果您在我的代码中发现任何其他错误或效率低下,请给我一个提示。谢谢。

更新:所需输出:

==============================================
                     drift          trend     
----------------------------------------------
emp                 -0.259          -4.841 ***    
cpi                  0.488          -1.326    
sp                   0.232          -3.831 **    
...                  ...            ...
----------------------------------------------
ADF critical values drift: -3.51 -2.89 -2.58  
ADF critical values trend: -4.04 -3.45 -3.15 

我更改了趋势中的数字,以便您可以看到重要性。

1 个答案:

答案 0 :(得分:3)

每个变量一个表,具有自己的临界值

stargazer()具有出色的报告功能。您可以使用notes= library(urca) library(stargazer) library(dplyr) data(nporg) df <- nporg[complete.cases(nporg), ] table_adf = function(DF, ...){ var_list = list(...) select_(DF, .dots = var_list) %>% {invisible(Map(function(x, y){ out1 = ur.df(x, type = "drift", selectlags = "AIC") out2 = ur.df(x, type = "trend", selectlags = "AIC") est_df = data.frame(drift = out1@teststat[1], trend = out2@teststat[1], row.names = y) footnote1 = paste("ADF critical values drift:", out1@cval[1,1], out1@cval[1,2], out1@cval[1,3]) footnote2 = paste("ADF critical values trend:", out2@cval[1,1], out2@cval[1,2], out2@cval[1,3]) stargazer(est_df, type = "text", summary = FALSE, notes = c(footnote1, footnote2)) },. , var_list))} } table_adf(df, "emp", "cpi", "sp") 参数作为脚注来执行以下操作:

...

使用select_参数和.dots + select代替out1@cval以编程方式提供变量名称

请注意,我更改了您的out1@cval[[1]]索引,以正确引用&#34; tau&#34;关键价值观。虽然我不熟悉ADF测试,但是使用> out1@cval 1pct 5pct 10pct tau2 -3.51 -2.89 -2.58 phi1 6.70 4.71 3.86 索引2x3矩阵的方式对我来说似乎很奇怪。所以我怀疑你真的想要那张桌子的第一行。

==============================================
                     drift          trend     
----------------------------------------------
emp                 -0.259          -3.041    
----------------------------------------------
ADF critical values drift: -3.51 -2.89 -2.58  
ADF critical values trend: -4.04 -3.45 -3.15  

==============================================
                     drift          trend     
----------------------------------------------
cpi                  0.488          -1.326    
----------------------------------------------
ADF critical values drift: -3.51 -2.89 -2.58  
ADF critical values trend: -4.04 -3.45 -3.15  

==============================================
                     drift          trend     
----------------------------------------------
sp                   0.232          -1.343    
----------------------------------------------
ADF critical values drift: -3.51 -2.89 -2.58  
ADF critical values trend: -4.04 -3.45 -3.15  

结果:

@teststate

具有所有变量估计显着性指标的单表

现在,这需要更多参与,因为你必须......:

  1. 从每个ur.df对象
  2. 中提取估算值(@cval
  3. %>%
  4. 相比,查看每项估算值是否显着
  5. 将所有估算值放在同一张表中
  6. 注意我在此函数中使用了最少数量的管道运算符...,因为几乎每个步骤都被使用了不止一次。例如,我不能仅将lapply参数传递到tests row.names,因为我还需要变量列表来设置est_table步骤中的var_list = as.list(unlist(list(...)))

    c("emp", "cpi", "sp")似乎不必要地复杂,但它允许用户输入变量名称"emp", "cpi", "sp",单个变量名称c("emp", "cpi"), "sp"或两者ifelse的向量。这为用户创造了额外的便利。最开始的table_adf = function(DF, ...){ # Stores ... arguments as list var_list = as.list(unlist(list(...))) # Subset DF based on var_list if(length(var_list) == 0){ subset = DF var_list = as.list(names(DF)) }else{ subset = select_(DF, .dots = var_list) } # Run model for each variable, and stored as list of lists, each element of the # outer list contains a list of the variable's drift and trend estimates tests = lapply(subset, function(x, y){ out1 = ur.df(x, type = "drift", selectlags = "AIC") out2 = ur.df(x, type = "trend", selectlags = "AIC") return(list(out1, out2)) }) # Store each model estimates in to a dataframe with corresponding variable names est_df = lapply(tests, function(x){ data.frame(drift = x[[1]]@teststat[1], trend = x[[2]]@teststat[1]) }) # Combine the list into a single dataframe est_table = do.call(rbind, est_df) critical_vals = data.frame(tests[[1]][[1]]@cval[1,], tests[[1]][[2]]@cval[1,]) # Check for significance level and add *'s accordingly est_table = Map(function(x, y){ ifelse(x < y[1], paste(x, "***"), ifelse(x < y[2], paste(x, "**"), ifelse(x < y[3], paste(x, "*"), x))) }, est_table, critical_vals) %>% data.frame(row.names = unlist(var_list)) # Set footnotes footnote1 = paste("ADF critical values drift:", paste(critical_vals[[1]], collapse = " ")) footnote2 = paste("ADF critical values trend:", paste(critical_vals[[2]], collapse = " ")) # Create nice looking table with critical values stargazer(est_table, type = "text", summary = FALSE, notes = c(footnote1, footnote2)) } 步骤检查变量名称是否有零参数,如果是这种情况则使用整个数据帧。

    > table_adf(df, "emp", "cpi", "sp")
    
    ==============================================
                         drift          trend     
    ----------------------------------------------
    emp                 -0.259          -3.041    
    cpi                  0.488          -1.326    
    sp                   0.232          -1.343    
    ----------------------------------------------
    ADF critical values drift: -3.51 -2.89 -2.58  
    ADF critical values trend: -4.04 -3.45 -3.15  
    
    > table_adf(df, names(df)[-1])
    
    ==============================================
                         drift           trend    
    ----------------------------------------------
    gnp.r          1.22473721812295      -1.308   
    gnp.n           4.2258196600637      2.195    
    gnp.pc        -0.153914521574277     -2.628   
    ip             1.49350460242673      -0.924   
    emp           -0.258523412908067     -3.041   
    ur            -2.75061777744267 *    -2.777   
    gnp.p          1.06909915298064      -0.800   
    cpi            0.488243115715459     -1.326   
    wg.n           2.81042091873464      0.399    
    wg.r           0.399440467221402     -2.445   
    M              1.45354783780152      -0.454   
    vel            -2.50304919304012     -2.537   
    bnd            1.21565390985417      1.837    
    sp             0.231913401411228     -1.343   
    ----------------------------------------------
    ADF critical values drift: -3.51 -2.89 -2.58  
    ADF critical values trend: -4.04 -3.45 -3.15  
    
    > table_adf(df)
    
    ==============================================
                         drift           trend    
    ----------------------------------------------
    year           -1.73205080756904     -1.732   
    gnp.r          1.22473721812295      -1.308   
    gnp.n           4.2258196600637      2.195    
    gnp.pc        -0.153914521574277     -2.628   
    ip             1.49350460242673      -0.924   
    emp           -0.258523412908067     -3.041   
    ur            -2.75061777744267 *    -2.777   
    gnp.p          1.06909915298064      -0.800   
    cpi            0.488243115715459     -1.326   
    wg.n           2.81042091873464      0.399    
    wg.r           0.399440467221402     -2.445   
    M              1.45354783780152      -0.454   
    vel            -2.50304919304012     -2.537   
    bnd            1.21565390985417      1.837    
    sp             0.231913401411228     -1.343   
    ----------------------------------------------
    ADF critical values drift: -3.51 -2.89 -2.58  
    ADF critical values trend: -4.04 -3.45 -3.15 
    

    下面演示了用户输入变量名称以获得所需估计值的方法:

    {{1}}