R功能可打印带有平稳性测试结果的表格 [英] R function to print table with stationarity test results

查看:88
本文介绍了R功能可打印带有平稳性测试结果的表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create an elegant solution that enables to select a set of data and then prints the Augmented Dickey-Fuller test results, as well as their critical values, into a table.

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

I generated following example code to get the needed data:

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'。

" Error in as.matrix(y) : object 'df.i' not found ".

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

Any idea how to write the function correctly, or even improve it? If possible, I would directly like to add corresponding results for the ur.pp test. dplyr solutions welcome.

推荐答案

您的功能离什么不太远你要。我做了一些更改,我相信这就是您想要的:

Your function is not too far from what you want. I made some changes and I believe this is what you wanted:

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"))

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

Probably there is more elegant solution but this is what I came up with.

这篇关于R功能可打印带有平稳性测试结果的表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆