다음 스크립트는 단위근 테스트 결과가있는 테이블을 인쇄하는 데 사용됩니다. 코드를 직접 사용해 볼 수 있습니다.테이블에 정확한 두 자리 숫자를 인쇄 하시겠습니까?
library(vars)
Canada = data.frame(Canada)
library(urca)
library(stargazer)
library(dplyr)
table_adf = function(DF, ...){
var_list = as.list(unlist(list(...)))
if(length(var_list) == 0){
subset = DF
var_list = as.list(names(DF))
}else{
subset = select_(DF, .dots = var_list)
}
tests = lapply(subset, function(x, y){
out1 = ur.df(x, type = "drift", selectlags = "BIC")
out2 = ur.df(x, type = "trend", selectlags = "BIC")
out3 = ur.pp(x, type = "Z-tau", model = "const", lags = "short")
out4 = ur.pp(x, type = "Z-tau", model = "trend", lags = "short")
out5 = ur.kpss(x, type = "mu", lags = "short")
out6 = ur.kpss(x, type = "tau", lags = "short")
return(list(out1, out2, out3, out4, out5, out6))
})
est_df = lapply(tests, function(x){
data.frame(ADF_constant = x[[1]]@teststat[1],
ADF_trend = x[[2]]@teststat[1],
PP_const = x[[3]]@teststat[1],
PP_trend = x[[4]]@teststat[1],
KPSS_const = x[[5]]@teststat[1],
KPSS_trend = x[[6]]@teststat[1])
})
est_table = do.call(rbind, est_df)
est_table = round(est_table, 2)
critical_vals = data.frame(tests[[1]][[1]]@cval[1,],
tests[[1]][[2]]@cval[1,],
tests[[1]][[3]]@cval[1,],
tests[[1]][[4]]@cval[1,],
tests[[1]][[5]]@cval[1,-1],
tests[[1]][[6]]@cval[1,-1])
est_table[, 1:4] = Map(function(x, y){
ifelse(x < y[1], paste(x, "0.01"),
ifelse(x < y[2], paste(x, "0.05"),
ifelse(x < y[3], paste(x, "0.10"), paste(x, ""))))
}, est_table[, 1:4], critical_vals[, 1:4]) %>% data.frame(row.names = unlist(var_list))
est_table[, 5:6] = Map(function(x, y){
ifelse(x > y[3], paste(x, "0.01"),
ifelse(x > y[2], paste(x, "0.02"),
ifelse(x > y[1], paste(x, "0.05"), paste(x, ""))))
}, est_table[, 5:6], critical_vals[, 5:6]) %>% data.frame(row.names = unlist(var_list))
stargazer(est_table, type = "text", summary = FALSE)
}
table_adf(Canada, "e", "prod", "rw", "U")
출력 테이블 : 당신이 예에서 볼 수 있듯이
=====================================================================
ADF_constant ADF_trend PP_const PP_trend KPSS_const KPSS_trend
---------------------------------------------------------------------
e -0.3 -2.73 0.15 -1.62 2.05 0.01 0.17 0.05
prod -0.11 -2.02 0.26 -1.97 1.66 0.01 0.27 0.01
rw -4.37 0.01 -2.82 -5.62 0.01 -2.81 2.12 0.01 0.43 0.01
U -2.22 -2.47 -1.72 -1.96 0.23 0.14
---------------------------------------------------------------------
, 첫 번째 값은 -0.3
대신 -0.30
로 표시됩니다. 스크립트를 조정하여 각 테스트 결과가 소수 둘째 숫자로 인쇄되도록하려면 어떻게해야합니까?
: https://stackoverflow.com/questions/46634643/r -functional-print-table-with-stationarity-test-results/46637222 # 46637222. –