loops - How to find variance and count periods until condition is met after event in R -
--sorry bad title, suggestions make more clear?--
i have following data frame:
df <- data.frame( day = c(1,2,3,4,5,6,7,8,9,10,11), score = c(67,51,52,57,66,63,63,68,64,57,77), attempt = c(0,1,0,1,0,0,0,1,0,0,0)) i want calculate how many days takes on threshold of >10% in scores each time attempt occurs. threshold should calculated scores occurred after attempt.
the threshold calculated percentage difference attempts until next score >10%. first value in table below 57/51-1=0.12
attempt = 1
as result, table shows me actual percentage deviation on score @ time attempt number of days took.
day score attempt threshold periods 1 67 0 2 51 1 12% 1 3 52 0 4 57 1 16% 0 5 66 0 6 63 0 7 63 0 8 68 1 13% 2 9 64 0 10 57 0 11 77 0
if don't have threshold yet, can calculate follows. i'll assume have starting point start_score:
start_score <- 45 later_scores <- df$score[df$attempt == 1] target <- c(start_score, later_scores) # 45 51 57 68 these want calculate percentage increase: # -length(target) remove last value of target denominator pct_increase <- (diff(target) / target[-length(target)]) * 100 df$threshold[df$attempt == 1] <- pct_increase with threshold column in place, can proceed: find rows in df$threshold > 10, precede indices 0 , calculate number of rows (i.e. periods) between rows df$threshold > 10:
inds <- c(0, which(df$threshold > 10)) df$periods <- rep(na, 11) df$periods[inds] <- diff(inds)-1 # day score attempt threshold periods # 1 67 0 na na # 2 51 1 13.33333 1 # 3 52 0 na na # 4 57 1 11.76471 1 # 5 66 0 na na # 6 63 0 na na # 7 63 0 na na # 8 68 1 19.29825 3 # 9 64 0 na na #10 57 0 na na #11 77 0 na na edit find sell point 'attempt': first value in 'df$score' troublesome illustration, because if buy @ point, you'll able sell @ +10% in last period. however, if have bought, should've sold in first period immediately. therefore deleted value data frame:
df <- data.frame(day = c(2,3,4,5,6,7,8,9,10,11), score = c(51,52,57,66,63,63,68,64,57,77), attempt = c(1, rep(na, 9))) if understand correctly, sell after score reached 10% higher score in period in bought. parallel this, buy again in period sell, right? wait selling newly bought shares (?) until scores have again risen 10%:
sell_time1 <- 1 repeat{ sell_thres <- df$score[sell_time1] * 1.1 sell_time2 <- min( which( (df$score > sell_thres) & (df$day > df$day[sell_time1]) )) ifelse(sell_time2 == sell_time1, break, sell_time1 <- sell_time2) df$attempt[sell_time1] <- 1 } this produce warning, because @ point sell_time2 in second line of repeat{} try take minimum value of empty vector. in application, nothing worry about. result in:
# day score attempt # 2 51 1 # 3 52 na # 4 57 1 # 5 66 1 # 6 63 na # 7 63 na # 8 68 na # 9 64 na # 10 57 na # 11 77 1
Comments
Post a Comment