【问题标题】:lag.xts issue: Error in lag.xts(x, n, na.pad = na.pad) : abs(k) must be less than nrow(x)lag.xts 问题:lag.xts(x, n, na.pad = na.pad) 中的错误:abs(k) 必须小于 nrow(x)
【发布时间】:2021-01-19 06:15:01
【问题描述】:

说明 运行 RMD 脚本时遇到错误:Error in lag.xts(x, n, na.pad = na.pad) : abs(k) must be less than nrow(x)

#获取权重函数下面

预期行为 脚本正在计算权重。下面是当前的代码块和回溯。

示例代码:

#subsetprices <- na.locf(prices['2010::20200410'])
subsetprices <- na.locf(prices['2010::'])
etf_returns <- na.omit(Return.calculate(subsetprices, method='discrete')) 

**# Get weights**
unl_etf_weights <- get_unlevered_target_weights(etf_returns, rebal = 22, vol_lookback = 90, cor_lookback = 120, cor_shrinkage = 1, adjust_momo = TRUE, momo_multiplier = 0.1)

# Restrict IEMB exposures to 15%, due to concerns over potential credit risk negative skews which won't appear in the data
EMB_weight_cap <- 0.15

# Subset unl_etf_weights data to pick those which we need to modify
unl_etf_weights_EMB_more_then_cap <- unl_etf_weights[unl_etf_weights$IEMB.LSE > EMB_weight_cap]

# Modify EMB weights
EMB_modded_weights <- replace(unl_etf_weights$IEMB.LSE, unl_etf_weights$IEMB.LSE > EMB_weight_cap, EMB_weight_cap)

# Rescale weights back to 1 on subset
weights_ex_EMB <- subset(unl_etf_weights_EMB_more_then_cap, select = -IEMB.LSE)
weights_ex_EMB <- (1-EMB_weight_cap) * weights_ex_EMB / rowSums(weights_ex_EMB)
unl_etf_weights_EMB_more_then_cap <- cbind(weights_ex_EMB, EMB_modded_weights)

# Ensure we have a right order before we bind back our modified subset
unl_etf_weights_EMB_more_then_cap <- unl_etf_weights_EMB_more_then_cap[,symbols_RP]
unl_etf_weights <- unl_etf_weights[,symbols_RP]

# combine two subset into one
unl_etf_weights <- rbind(unl_etf_weights_EMB_more_then_cap, unl_etf_weights[unl_etf_weights$IEMB.LSE <= EMB_weight_cap])
unl_etf_weights <- unl_etf_weights[c(!duplicated(time(unl_etf_weights))[-1], TRUE)]

# Ensure returns matrix in xts object is in the same order as in the backtest
etf_backtest_returns <- etf_returns[,symbols_RP]
etf_backtest_weights <- unl_etf_weights[,symbols_RP]

# Subset so we're only reporting on the live trading record
etf_backtest_returns <- etf_backtest_returns['2019-9::']
etf_backtest_weights <- etf_backtest_weights['2019-9::']

# Generate performance charts
riskperformance(etf_backtest_returns, weights=etf_backtest_weights)


Error in lag.xts(x, n, na.pad = na.pad) : abs(k) must be less than nrow(x)
6.
lag.xts(x, n, na.pad = na.pad)
5.
TTR::ROC(synthetic_prices, n = formation_period, type = "discrete")
4.
xts::lag.xts(TTR::ROC(synthetic_prices, n = formation_period, type = "discrete"), 1)
3.
na.omit(xts::lag.xts(TTR::ROC(synthetic_prices, n = formation_period, type = "discrete"), 1))
2.
get_momo_adjustments(ret, formation_period = 12 * 22, vol_weights = volw)
1.
get_unlevered_target_weights(etf_returns, rebal = 22, vol_lookback = 90, cor_lookback = 120, cor_shrinkage = 1, adjust_momo = TRUE, momo_multiplier = 0.1)

【问题讨论】:

    标签: r xts


    【解决方案1】:

    这个错误对我来说很清楚。你不能比你的 xts 延迟更多的天数。看一个例子:

    library(xts)
    testxts <- xts(1:100, order.by = seq.Date(from = Sys.Date()-100,
                                              length.out = 100,
                                              by = 'days'))
    works <- xts::lag.xts(testxts, k = 1, na.pad = T)
    head(works)
               [,1]
    2020-07-25   NA
    2020-07-26    1
    2020-07-27    2
    2020-07-28    3
    2020-07-29    4
    2020-07-30    5
                   
    doesnt_work <- xts::lag.xts(testxts, k =200, na.pad = T)
    Error in xts::lag.xts(testxts, k = 200, na.pad = T) : 
      abs(k) must be less than nrow(x)  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 2018-09-27
      • 2021-08-02
      • 1970-01-01
      • 2018-05-26
      相关资源
      最近更新 更多