繁体   English   中英

R功能可打印带有平稳性测试结果的表格

[英]R function to print table with stationarity test results

我正在尝试创建一个优雅的解决方案,该解决方案能够选择一组数据,然后将Augmented Dickey-Fuller测试结果及其临界值打印到表中。

我生成了以下示例代码来获取所需的数据:

library(urca)
data(Canada)
Canada
data.dft <- ur.df(Canada[, "e"], lags=3, type='drift')
data.df <- ur.df(Canada[, "e"], lags=3, type='trend')
summary(data.dfc)
summary(data.dft)

所需的输出表:

T-test(drift),  1%,    5%,    10%,   T-test(trend),  1%,    5%,    10%
       0.4964   -3.51  -2.89  -2.58         -1.9664  -4.04  -3.45 -3.15

尝试过:

stationarity = function(df, x){
for (i in x){
  out1 = ur.df(df.i[,1], type = "drift", selectlags = "BIC")
  out2 = ur.df(df.i[,1], type = "trend", selectlags = "BIC")
  est_df = cbind(out1@teststat[1],
                 out1@cval[1,1],
                 out1@cval[1,2],
                 out1@cval[1,3],
                 out2@teststat[1],
                 out2@cval[1,1],
                 out2@cval[1,2],
                 out2@cval[1,3])
  print(est_df)
}
}

stationarity(Canada, c("e","prod","RW"))

但是,这不起作用:

“ as.matrix(y)中的错误:找不到对象'df.i”。

知道如何正确编写甚至改进该功能吗? 如果可能的话,我想直接为ur.pp测试添加相应的结果。 欢迎使用dplyr解决方案。

您的功能距离您想要的不太远。 我进行了一些更改,我相信这是您想要的:

library(urca)
library(vars)
# Load data
data(Canada)
# Function
stationarity <- function(df, x){
# Define empty object
  df <- NULL
# The way to write "i in ..."
  for (i in 1 : length(x)){
# We change column names as x[1] = "e" and so on
    out1 <- ur.df(Canada[,x[i]], type = "drift", selectlags = "BIC")
    out2 <- ur.df(Canada[,x[i]], type = "trend", selectlags = "BIC")
# rbind will collect rows after they are combined for each x[i]
     df <- rbind(df,
# cbind will work on inner part, when you combine 
# the 8 resulting numbers for current x[i]
                cbind(out1@teststat[1],
                      out1@cval[1,1],
                      out1@cval[1,2],
                      out1@cval[1,3],
                      out2@teststat[1],
                      out2@cval[1,1],
                      out2@cval[1,2],
                      out2@cval[1,3]))
  }
# assign column names
  colnames(df) <- c("T-test(drift)", "1%", "5%", "10%",
                    "T-test(trend)", "1%", "5%", "10%")
# assign row names
  rownames(df) <- x
# result
  print(df)

}
stationarity(Canada, c("e","prod","rw"))

也许有更优雅的解决方案,但这就是我想出的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM