如何在R Studio中的for循环中包含计数过程?
我想计算10年内每个月有多少正回报和负回报,并将其包含在如何在R Studio中的for循环中包含计数过程?,r,loops,for-loop,if-statement,while-loop,R,Loops,For Loop,If Statement,While Loop,我想计算10年内每个月有多少正回报和负回报,并将其包含在dtf中。这是因为我想计算得到正回报和负回报的概率。我不知道如何在for循环中包括计数过程 例如:10年内7月的平均月回报率为2.18%,而正回报率为80%,因此获得负回报的概率为20%。由于7月份的10年平均月回报率为正,为2.18%,我希望在dtf中显示正回报率(80%)的概率,而不是负回报率(20%) 另一个例子:同样的事情发生在五月。由于5月份10年内的平均月回报率为负(-1.23%),10年内获得正回报的概率为60%(10年中有6
dtf
中。这是因为我想计算得到正回报和负回报的概率。我不知道如何在for循环中包括计数过程
例如:10年内7月的平均月回报率为2.18%,而正回报率为80%,因此获得负回报的概率为20%。由于7月份的10年平均月回报率为正,为2.18%,我希望在dtf
中显示正回报率(80%)的概率,而不是负回报率(20%)
另一个例子:同样的事情发生在五月。由于5月份10年内的平均月回报率为负(-1.23%),10年内获得正回报的概率为60%(10年中有6个),而负回报的概率为40%(10年中有4个),因此我希望在dtf
中显示负回报率(40%),而不是正回报率60%
每个月都会有同样的事情发生,因此,dtf
中会有第三列显示获得正/负回报的概率
我试图在我的for loop
中包含if loop
,但它不起作用。我在下面的代码中附加了dtf
,只有两列(月和平均月)
库(quantmod)
#历史股价
价格根据我对您问题的理解,这里有一个建议的解决方案。代码中给出了注释
主要思想是根据一些if/else
测试的结果,在循环中添加一些递增的计数器变量。它们存储负收入或正收入的概率。然后通过一个额外的测试,你确定你想要保持正或负收入的概率
有很多方法可以写得更简洁,但是这个较长的版本显示了所有的细节和想法。如果你知道收入从不为零,你可以只计算一个计数器,因为你知道一个概率,例如正,总是100-其他概率。在我的版本中,零收入是可能的,因此你可能有正概率+负概率小于100
library(quantmod)
#obtian the historical stock price
prices <- getSymbols("^GSPC", src = 'yahoo', from = "2009-07-01", to = "2019-08-01",
periodicity = "monthly", auto.assign = FALSE, warnings = FALSE)[,4]
#calculate the log return and convert back to simple return
return <- diff(log(prices))
r <- na.omit(exp(return)-1)
monthlyRet <- as.numeric(r[,1])
#loop through all the months in 10 years
AverageMonthlyRet <- c()
#Added: Array to store the probability for each month
Probability <- c()
for (j in 1:12){
Group <- c()
#Added: Counter, for each month, of positive or negative income
connt_pos=0
count_neg=0
for (i in seq(j,length(monthlyRet),12)){
Group[i] <- monthlyRet[i]
#Added: Increment the counters based on the sign of monthlyRet[i]
if(monthlyRet[i]>0){
connt_pos <- connt_pos+1
}
else if(monthlyRet[i]<0){
count_neg <- count_neg+1
}
}
AverageMonthlyRet[j] <- mean(Group, na.rm=TRUE)
#Added: Depending if the average monthly retrn is positive or negative
#compute the probability of positive or negative income (in %)
prob=0
if(AverageMonthlyRet[j]>0)
{
prob=connt_pos/(length(monthlyRet)/12)*100
}
else if (AverageMonthlyRet[j]<0){
prob=count_neg/(length(monthlyRet)/12)*100
}
#Added: Store the result
Probability[j] <- prob
}
AverageMonthlyRet <- round(AverageMonthlyRet,4)
#create a data frame to store the result
Month <- c("Aug","Sep","Oct","Nov","Dec","Jan","Feb","Mar","Apr","May","Jun","Jul")
#Added: Add the new probability column to the final data frame
dtf <- data.frame(Month, AverageMonthlyRet,Probability)
库(quantmod)
#历史股价
价格使用基数R,我们可以计算每个月符号的比例,通过将与dtf
合并,并根据AverageMonthRnet
的符号
选择合适的值
tmp <- as.data.frame(do.call(rbind, tapply(sign(r), format(index(r), "%b"),
function(x) c(table(x)))))
tmp$Month <- rownames(tmp)
df1 <- merge(dtf, tmp)
df1$selected <- ifelse(sign(df1$AverageMonthlyRet) == 1, df1$`1`, df1$`-1`)
df1
# Month AverageMonthlyRet -1 1 selected
#1 Apr 0.0122 1 9 9
#2 Aug -0.0078 5 5 5
#3 Dec 0.0061 3 7 7
#4 Feb 0.0234 2 8 8
#5 Jan 0.0115 4 6 6
#6 Jul 0.0218 2 8 8
#7 Jun 0.0030 4 6 6
#8 Mar 0.0171 4 6 6
#9 May -0.0123 4 6 4
#10 Nov 0.0162 2 8 8
#11 Oct 0.0189 4 6 6
#12 Sep 0.0086 4 6 6
tmp每个月的正回报率(7月8/10,5月6/10)信息存储在哪里?另外,您的预期输出是什么样子的?嗨,Ronak,谢谢您的回复。这就是我的问题所在,我不知道如何将计数部分包括到for循环中。我在7月和5月通过手动计数获得了8/10和6/10,它们存储在“r”中。我希望我可以在“dtf”中获得额外的第3列,这表明获得负或正回报的概率取决于我上面提到的10年平均回报是正还是负。谢谢!:)请注意,在运行“r”之后,输出是一个表,其中有一列名为GSPC.Close。这实际上是退货而不是收盘价,我忘了更改列名。对此表示抱歉。请使用dput()
包含输入和预期输出,谢谢Chelmy提供的解决方案!这正是我要找的!代码简单易懂。感谢您的帮助:)感谢Ronak提供更简短的解决方案!它对我也很有用,但是你的代码太先进了,我无法理解,因为我还是R的初学者,但无论如何,非常感谢!)
tmp <- as.data.frame(do.call(rbind, tapply(sign(r), format(index(r), "%b"),
function(x) c(table(x)))))
tmp$Month <- rownames(tmp)
df1 <- merge(dtf, tmp)
df1$selected <- ifelse(sign(df1$AverageMonthlyRet) == 1, df1$`1`, df1$`-1`)
df1
# Month AverageMonthlyRet -1 1 selected
#1 Apr 0.0122 1 9 9
#2 Aug -0.0078 5 5 5
#3 Dec 0.0061 3 7 7
#4 Feb 0.0234 2 8 8
#5 Jan 0.0115 4 6 6
#6 Jul 0.0218 2 8 8
#7 Jun 0.0030 4 6 6
#8 Mar 0.0171 4 6 6
#9 May -0.0123 4 6 4
#10 Nov 0.0162 2 8 8
#11 Oct 0.0189 4 6 6
#12 Sep 0.0086 4 6 6