首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >lag.xts问题: lag.xts(x,n,na.pad = na.pad)中的错误: abs(k)必须小于nrow(x)

lag.xts问题: lag.xts(x,n,na.pad = na.pad)中的错误: abs(k)必须小于nrow(x)
EN

Stack Overflow用户
提问于 2020-10-04 02:36:55
回答 1查看 565关注 0票数 0

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

在下面的# Get权重函数中

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

示例代码:

代码语言:javascript
复制
#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)
EN

回答 1

Stack Overflow用户

发布于 2020-11-02 16:24:12

在我看来,这个错误很明显。你不能比你的xts落后更多的时间。见一个例子:

代码语言:javascript
复制
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)  
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64190685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档