【问题标题】:How to obtain string of the patterned variables separated by comma in R?如何获取R中用逗号分隔的模式变量字符串?
【发布时间】:2014-12-05 17:39:46
【问题描述】:

我编写了 A增强的 Dickey-Fuller 测试代码,该代码考虑了 same (Common) 的用法自回归过程的所有滞后顺序的子样本技术信息:在计量经济学中的所有滞后选择程序中,必须使用相同的子样本来确定正确的最优最小滞后)。然而,尽管 max 参数似乎是任意的,但实际上并非如此,因为编程问题:以下代码仅适用于 max=14。因为:

我无法实现如何放置:

(对于 max=3)“x1d,x1l,x1d1l,x1d2l,x1d3l”

(对于 max=4) "x1d, x1l, x1d1l, x1d2l, x1d3l, x1d4l"

(for max=6), "x1d, x1l, x1d1l, x1d2l, x1d3l, x1d4l, x1d5l, x1d6l"等如下代码。

我想要一个特定模式的字符串;字符串的长度将由用户(通过最大值)确定。

对于那些对变量感兴趣的人:

x1d := 时间序列 x 的第一个差异

x1l := 时间序列 x 的第一个滞后

x1d1l := 时间序列 x 的第一个差异的第一个滞后(x 的第一个差异的第一个滞后!)

....

x3d5l := x 的 3 次差的 5 次滞后

x2l5d := x 的第二个滞后的第五个差

即变量命名中从左到右应用滞后和差异运算符。

adfcs <- function (t, max = 14, type = c("c")) {
x <- ts(t)
x1d = diff(x, differences=1)
x1l = lag(x, -1)
for (i in as.integer(1:max)) { # Here, I obtained all the lags that will be used in ADF regression
assign(paste(paste("x1d", i, sep=""), "l", sep=""), lag(x1d, -i))}

DLDlag = ts.intersect(x1d, x1l, x1d1l, x1d2l, x1d3l, x1d4l, x1d5l, x1d6l, x1d7l, x1d8l, x1d9l, x1d10l, x1d11l, x1d12l, x1d13l, x1d14l) # Here is the problem.
DLDlag.df = data.frame(ts.intersect(x1d, x1l, x1d1l, x1d2l, x1d3l, x1d4l, x1d5l, x1d6l, x1d7l, x1d8l, x1d9l, x1d10l, x1d11l, x1d12l, x1d13l, x1d14l), obspts =c(time(DLDlag))) # Here is the problem.

DifferenceLags = as.vector(names(DLDlag.df), mode="any")[3: (length(DLDlag.df)-1)]

lmresults = array(list())
SBCvalues = array(list())
AICvalues = array(list())
for (i in as.integer(0:max)) {
if (i == 0) { lmresults[[max+1]] = lm(as.formula(paste("x1d ~x1l")),data=DLDlag.df) 
SBCvalues[[max+1]] = BIC(lmresults[[max+1]])
AICvalues[[max+1]] = AIC(lmresults[[max+1]]) }
if (i > 0) { lmresults[[i]] = lm(as.formula(paste("x1d ~ x1l+", paste(DifferenceLags[1:i], collapse="+"))),data=DLDlag.df) 
SBCvalues[[i]] = BIC(lmresults[[i]])
AICvalues[[i]] = AIC(lmresults[[i]]) }
}
list(which.min(SBCvalues), which.min(AICvalues))
as.data.frame(cbind(SBCvalues, AICvalues)) 

if (which.min(SBCvalues)==max+1) {
scs=(max+2)-(0+1)
adfcs = unitrootTest(x[scs:length(x)], lags = 0, type = c("c"))


} else {
scs=(max+2)-(which.min(SBCvalues)+1)
adfcs = unitrootTest(x[scs:length(x)], lags =which.min(SBCvalues), type = c("c"))

 }
adfcs 
}

注意:在 Eviews(版本

1. 双击一个变量;看法;单位根检验;自动进稿器; “水平,拦截”;滞后长度:自动选择 Schwarz:最大滞后 =14"。(根据数据集变化 14 次)

2. 通过删除第一个样本来排列子样本:假设数据是 1960Q1 2009Q4 (200 obs)。然后首先从样本中删除 14+1 个 obs:样本 - 样本范围对:1963Q4 2009Q4。提供相同的子样本后,将执行 ADF。

3. 再次双击同一个变量;看法;单位根检验;自动进稿器; “水平,拦截”; Lag Length: Automatic Selection Schwarz: Maximum Lags=14"。(由于二次抽样的影响,这里可能会出现 14 以外的不同数字;删除框中的那个数字,然后输入 14) . 按确定。

任何帮助将不胜感激?

【问题讨论】:

    标签: r string replace


    【解决方案1】:

    你需要一些 R 的“计算语言”的东西来做这种事情(即,以编程方式构造对另一个函数的调用):

    试试这个:

    x <- ts(cumsum(rnorm(100)))
    max <- 5
    
    x1d <- diff(x, differences=1)
    x1l <- lag(x, -1)
    x_names <- c("x1d", "x1l", 
                 sapply(1:max, function(i) paste("x1d", i, "l", sep="")))
    for (i in as.integer(1:max)) { 
      assign(x_names[i+2], lag(x1d, -i))
    }
    DLDlag <- do.call(ts.intersect, sapply(x_names, as.symbol))
    DLDlag.df <- data.frame(DLDlag, obspts = c(time(DLDlag)))
    

    【讨论】:

    • 完美!感谢数十亿次fabians。您的回答不仅解决了问题,而且向我展示了 R 在应用 sapply、do.call 等高级技术方面的广阔视野。再次感谢您的精彩回答。上述功能将作为“causfinder”包的一部分出现,该包将很快提交给 CRAN。
    猜你喜欢
    • 2012-05-19
    • 2012-08-12
    • 2013-06-24
    • 2015-02-08
    • 1970-01-01
    • 2011-09-07
    • 2021-10-31
    • 2015-02-17
    • 1970-01-01
    相关资源
    最近更新 更多