mardi 28 mars 2017

R 3.3.3 "Error in if (corl > corr) { : argument is of length zero"

Sequential similarity of similar scale

    library(quantmod)

    #input symbol, start and end dates:

    my_symbol<-readline("What symbol would you like analyzed?")
    start_date <- readline("When do you want the series to start?")
    end_date <- readline("When do you want the series to end?")

    #Scaled series begins here        
    start_date2 <- readline("When do you want the scaled series to start?")
    end_date2 <- readline("When do you want the scaled series to end?")
    s_min<-readline("What is the minimum scaling factor you would like the use?")
    s_max<-readline("what is the maximum scaling factor you would like to use?") 

    data1 <- as.data.frame(getSymbols(my_symbol, from = start_date, to = end_date, env = NULL)) 
    data2<- getSymbols(my_symbol, from = start_date2, to = end_date2, env = NULL)
    data3<- as.ts(data2[,4])

    section<-length(data3)/3
    #divide subset into 3 sections

    subdata1<-data3[1:section]
    subdata2<-data3[section:(section*2)]
    subdata3<-data3[(section*2):(section*3)]

    #Dataframe function goes here!
    data_frames<- function(subdata, df){      
                section2<-length(subdata)
                testdata1<-data1[,4]
                testdata2<-testdata1[1:section2]
                co<-readline("what is the minimum correlation accepteable for the current part?")
                corr<-as.numeric(co)
                steps<-readline("what is the minimum step desired per iteration?")
                steps_numeric<-as.numeric(steps)
                #Iterate through testdata
                len2<-length(subdata)
                x<-as.numeric(s_min)
                while (x < as.numeric(s_max)){
                  resize<-approx(seq_along(subdata), subdata, n = len2*x)$y
                  len<-length(resize)
                  for (i in seq(1, length(testdata1), by = steps_numeric)){
                    testdata2<-testdata1[(1+i):(len+i)]
                    corl<-cor(testdata2, resize)
                    corl<-na.omit(corl)
                    if(corl > corr) {
                      maxes<-c(corl, (len/len2), i)
                      write.table(maxes,"a.txt", append = TRUE, quote = FALSE, row.names = FALSE, col.names = FALSE)

                    }
                  }
                  x<-(x+0.01)
                }
                df<- read.table("a.txt")
                file.remove("a.txt")
                df<-as.matrix(df)
                df<-matrix(df, ncol = 3, byrow = TRUE)
                df<-as.data.frame(df)

              }

    df<-data_frames(subdata1,df)
    df<-df[ order(-df[,1], df[,1]), ]
    df2<-data_frames(subdata2, df2)
    df2<-df2[ order(-df2[,1], df2[,1]), ]
    df3<-data_frames(subdata3, df3)
    df3<-df3[ order(-df3[,1], df3[,1]), ]
    colnames(df)<-c("Cor", "Scale", "Step")
    colnames(df2)<-c("Cor", "Scale", "Step")
    colnames(df3)<-c("Cor", "Scale", "Step")

I get an error when running the if statement that says $Error in if (corl[i] > corr) { : missing value where TRUE/FALSE needed

How can this be resolved?

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire