0

I work with neuralnet package to predict values of stocks (diploma thesis). The example data are below

predict<-runif(23,min=0,max=1)
day<-c(369:391)
ChoosedN<-c(2,5,5,5,5,5,4,3,5,5,5,2,1,1,5,5,4,3,2,3,4,3,2)
Profit<-runif(23,min=-2,max=5)
df<-data.frame(predict,day,ChoosedN,Profit)
colnames(df)<-c('predict','day','ChoosedN','Profit')

But I haven't always same period for investments (ChoodedN). For backtest the neural site I have to skip the days when I am still in position even if the neural site says 'buy it' (i.e.predict > 0.5). The frame looks like this

        predict day ChoosedN        Profit
1  0.6762981061 369        2 -1.6288823350
2  0.0195611224 370        5  1.5682195597
3  0.2442795106 371        5  0.6195915225
4  0.9587601107 372        5 -1.9701975542
5  0.7415729680 373        5  3.7826137026
6  0.4814927997 374        5  4.1228808255
7  0.1340754859 375        4  3.7818792837
8  0.6316874851 376        3  0.7670884461
9  0.1107241728 377        5 -1.3367400097
10 0.5850426450 378        5  2.2848396166
11 0.2809308425 379        5  2.5234691438
12 0.2835292015 380        2 -0.3291319925
13 0.3328713216 381        1  4.7425349397
14 0.4766904986 382        1 -0.4062103292
15 0.5005860797 383        5  4.8612083721
16 0.2734292494 384        5 -0.2320077328
17 0.1488479455 385        4  2.6195679584
18 0.9446908936 386        3  0.4889716264
19 0.8222738281 387        2  0.7362413658
20 0.7570014759 388        3  4.6661250258
21 0.9988698252 389        4  2.6340743946
22 0.8384663551 390        3  1.0428046484
23 0.1938821415 391        2  0.8855748393

And I need to create new data.frame this way.For example:If predict (in first row) > 0.5,delete second and third row (because ChoosedN in first row is 2 so next two after first row has to be delete, because there we were still in position). And continue on fourth the same way (if predict (fourth row) > 0.5, delete next five rows and so. And of course, if predict <=0.5 delete this row too. Any straightforward way how to do it with some loop? Thanks

1 Answer 1

2

I would create a new dataframe, then bind the rows you want using rbind inside of a for loop

newDF <- data.frame()                # New, Empty Dataframe
i = 1                                # Loop index Variable

while (i < nrow(df)) {
  if (df$predict[i] > 0.5) {         # If predict > 0.5,
    newDF <- rbind(newDF, df[i,])    # Bind the row
    i = i + df$ChoosedN[i]           # Adjust for ChoosedN rows
  }
  i = i + 1                          # Move to the next row
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.