Here is the image showing what i need to calculate. Sorry total_r in the image is actually test_gain in the code and gamma in the pic refers to alpha in the code (Sorry). Also in the image i stop calculating at t=3 but actually i want to calculate it until the last value which is 0.6.
I am fairly new to using the apply family of functions. I normally use loops but i have heard using the apply function instead of nested for loops is a lot quicker. I tried a a couple tutorials but still failed to replace my nested for loop with the apply function. Any help will be greatly appreciated and below is the code i'm trying to change.
Basically this is what i'm trying to do: first row of the data: take value from a column + alpha * nextvalue of this column (row 2) + alpha ^ 2 * nextvalue of column (row3) + alpha ^ 3 * next value of column (row4) and so on untill the last row. Each time i am increasing the power to Alpha.
All this calculation was for the first row. Now for the 2nd row i will ignore the first value from the column but will take all the subsequent values the same way. Below is my code which works fine but it just take too long to execute.
#value of alpha
alpha <- 0.85
# test_gain is a vector containing values from a column belonging to a data frame
test_gain <- testdata$Total_rew
# initialise the variables
s = 0
d = rep(0,nrow(testdata))
for (i in 1:nrow(testdata[1:4999,])){
d[i] = test_gain[i]
for (s in (i+1):nrow(testdata)){
d[i] = d[i] + alpha^(s-i) * test_gain[s]
if (alpha^(s-i) < (10^-5)) {next()}
}
}
snoti; you should probably usebreakinstead? Anyways, you would want a better solution.