2017-10-08 2 views
1

データのセットを選択して、拡張Dickey-Fullerテスト結果とその重要な値をテーブルに出力する洗練されたソリューションを作成しようとしています。Rは定常性テスト結果でテーブルを印刷する機能

私は必要なデータを取得するコード例を以下に生成

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([email protected][1], 
       [email protected][1,1], 
       [email protected][1,2], 
       [email protected][1,3], 
       [email protected][1], 
       [email protected][1,1], 
       [email protected][1,2], 
       [email protected][1,3]) 
    print(est_df) 
} 
} 

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

、それは動作しませんでした。

"as.matrix(y)のエラー:オブジェクト 'df.i'が見つかりません"。

どのように正しく機能を書いたり、改善することができますか?可能であれば、私はur.ppテストの対応する結果を直接追加したいと思います。 dplyrソリューション歓迎。

答えて

0

あなたの機能はあなたが望むものからあまり離れていません。私はいくつかの変更を行って、私はこれがあなたが望んでいると信じて:

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([email protected][1], 
         [email protected][1,1], 
         [email protected][1,2], 
         [email protected][1,3], 
         [email protected][1], 
         [email protected][1,1], 
         [email protected][1,2], 
         [email protected][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")) 

は、おそらくよりエレガントな解決策はありますが、これは私が思い付いたものです。