用 R 中每次循环迭代的结果填充一个对象
Fill an object with the results of every loop iteration in R
第一次在这里提问。我最近计划与 R 合作,我希望我能在某个问题上得到一些帮助。这个问题可能很容易解决,但我自己找不到答案,我的研究也没有成功。
基本上我需要根据循环的输入创建一个对象。我有 7 个模拟资产 returns,这些对象包含模拟 I 运行 的结果。我想匹配每个对象的列并形成一个组合的列(即每个列 1 形成一个对象),它将用于某些计算。
最后,每次迭代的结果应该存储在一个对象上,该对象必须在循环外可用以供进一步分析。
我创建了以下循环,问题是只有最后一次迭代的结果被写入最终对象。
# Initial xts object definition
iteration_returns_combined <- iteration_returns_draft_1
for (i in 2:10){
# Compose object by extracting the i element of every simulation serie
matrix_daily_return_iteration <- cbind(xts_simulated_return_asset_1[,i],
xts_simulated_return_asset_2[,i],
xts_simulated_return_asset_3[,i],
xts_simulated_return_asset_4[,i],
xts_simulated_return_asset_5[,i],
xts_simulated_return_asset_6[,i],
xts_simulated_return_asset_7[,i])
# Transform the matrix to an xts object
daily_return_iteration_xts <- as.xts(matrix_daily_return_iteration,
order.by = index(optimization_returns))
# Calculate the daily portfolio returns using the iteration return object
iteration_returns <- Return.portfolio(daily_return_iteration_xts,
extractWeights(portfolio_optimization))
# Create a combined object for each iteration of portfolio return
# This is the object that is needed in the end
iteration_returns_combined <<- cbind(iteration_returns_draft_combined,
iteration_returns_draft)
}
iteration_returns_combined_after_loop_view
有人可以帮我解决这个问题吗,如果有人能提供任何信息,我将不胜感激。
谢谢,
R-新秀
通过查看代码,我推测错误出在你的 for 循环的最后一行。
iteration_returns_draft_combined
从未定义,因此假定为 NULL。本质上,您只需将每次迭代的结果列绑定到 NULL 对象。因此,最后一个循环的输出也按列绑定到 NULL 对象,这就是您观察到的。尝试以下操作:
iteration_returns_combined <- cbind(iteration_returns_combined,
iteration_returns)
这应该有用,希望如此!
考虑 sapply
并避免在循环中扩展对象:
iteration_returns_combined <- sapply(2:10, function(i) {
# Compose object by extracting the i element of every simulation serie
matrix_daily_return_iteration <- cbind(xts_simulated_return_asset_1[,i],
xts_simulated_return_asset_2[,i],
xts_simulated_return_asset_3[,i],
xts_simulated_return_asset_4[,i],
xts_simulated_return_asset_5[,i],
xts_simulated_return_asset_6[,i],
xts_simulated_return_asset_7[,i])
# Transform the matrix to an xts object
daily_return_iteration_xts <- as.xts(matrix_daily_return_iteration,
order.by = index(optimization_returns))
# Calculate the daily portfolio returns using the iteration return object
iteration_returns <- Return.portfolio(daily_return_iteration_xts,
extractWeights(portfolio_optimization))
})
如果需要先绑定列 vector/matrix,然后再绑定:
# CBIND INITIAL RUN
iteration_returns_combined <- cbind(iteration_returns_draft_1, iteration_returns_combined)
第一次在这里提问。我最近计划与 R 合作,我希望我能在某个问题上得到一些帮助。这个问题可能很容易解决,但我自己找不到答案,我的研究也没有成功。
基本上我需要根据循环的输入创建一个对象。我有 7 个模拟资产 returns,这些对象包含模拟 I 运行 的结果。我想匹配每个对象的列并形成一个组合的列(即每个列 1 形成一个对象),它将用于某些计算。 最后,每次迭代的结果应该存储在一个对象上,该对象必须在循环外可用以供进一步分析。
我创建了以下循环,问题是只有最后一次迭代的结果被写入最终对象。
# Initial xts object definition
iteration_returns_combined <- iteration_returns_draft_1
for (i in 2:10){
# Compose object by extracting the i element of every simulation serie
matrix_daily_return_iteration <- cbind(xts_simulated_return_asset_1[,i],
xts_simulated_return_asset_2[,i],
xts_simulated_return_asset_3[,i],
xts_simulated_return_asset_4[,i],
xts_simulated_return_asset_5[,i],
xts_simulated_return_asset_6[,i],
xts_simulated_return_asset_7[,i])
# Transform the matrix to an xts object
daily_return_iteration_xts <- as.xts(matrix_daily_return_iteration,
order.by = index(optimization_returns))
# Calculate the daily portfolio returns using the iteration return object
iteration_returns <- Return.portfolio(daily_return_iteration_xts,
extractWeights(portfolio_optimization))
# Create a combined object for each iteration of portfolio return
# This is the object that is needed in the end
iteration_returns_combined <<- cbind(iteration_returns_draft_combined,
iteration_returns_draft)
}
iteration_returns_combined_after_loop_view
有人可以帮我解决这个问题吗,如果有人能提供任何信息,我将不胜感激。
谢谢, R-新秀
通过查看代码,我推测错误出在你的 for 循环的最后一行。
iteration_returns_draft_combined
从未定义,因此假定为 NULL。本质上,您只需将每次迭代的结果列绑定到 NULL 对象。因此,最后一个循环的输出也按列绑定到 NULL 对象,这就是您观察到的。尝试以下操作:
iteration_returns_combined <- cbind(iteration_returns_combined,
iteration_returns)
这应该有用,希望如此!
考虑 sapply
并避免在循环中扩展对象:
iteration_returns_combined <- sapply(2:10, function(i) {
# Compose object by extracting the i element of every simulation serie
matrix_daily_return_iteration <- cbind(xts_simulated_return_asset_1[,i],
xts_simulated_return_asset_2[,i],
xts_simulated_return_asset_3[,i],
xts_simulated_return_asset_4[,i],
xts_simulated_return_asset_5[,i],
xts_simulated_return_asset_6[,i],
xts_simulated_return_asset_7[,i])
# Transform the matrix to an xts object
daily_return_iteration_xts <- as.xts(matrix_daily_return_iteration,
order.by = index(optimization_returns))
# Calculate the daily portfolio returns using the iteration return object
iteration_returns <- Return.portfolio(daily_return_iteration_xts,
extractWeights(portfolio_optimization))
})
如果需要先绑定列 vector/matrix,然后再绑定:
# CBIND INITIAL RUN
iteration_returns_combined <- cbind(iteration_returns_draft_1, iteration_returns_combined)