2

I have two return series in R. I want to compare the each observation in these series. The problem is that if I compare two observations with negative signs, It gives the results that minimum value is greater than maximum. For example if I have -0.5 and -1.2 as returns for a particular month. It will term -0.5 as greater value. Mathematically this is right. But in financial terms if a stock delivers -0.5% and another -1.2%, stock with -1.2% produce greater losses, that stock should be selected. Suggest how this problem can be solved. I have try to replicate the code. Please check below code

library(Lock5Data)
library(lubridate)
library(tbl2xts)
options(scipen = 999)
SandP500$Date<-mdy(SandP500$Date)
SandP500<-tbl_xts(SandP500)
SandP500<-SandP500[,4]
SandP500$logret_1<-diff(log(SandP500),lag=1)
SandP500_data<-SandP500[,1]
SandP500_log36ret<-diff(log(SandP500_data),lag=36)
SandP500_fulldata<-cbind(SandP500,SandP500_log36ret)
names(SandP500_fulldata)<-c("close","log1","lag36")
SandP500_fulldata<-SandP500_fulldata[-1:-36,]

ifelse(SandP500_fulldata$lag36>SandP500_fulldata$log1,"normal","abnormal")["2014-08-12"]
## Check observation on 
SandP500_fulldata["2014-08-12"]
simar
  • 605
  • 4
  • 12

1 Answers1

1

You could try using abs(). For example,

ifelse(SandP500_fulldata$lag36<0 & SandP500_fulldata$log1<0,
               (ifelse((abs(SandP500_fulldata$lag36))>(abs(SandP500_fulldata$log1)),"normal","abnormal")),
               (ifelse((SandP500_fulldata$lag36)>(SandP500_fulldata$log1),"normal","abnormal")))

gives,

           lag36   
2014-08-12 "normal"

Here is a very clear explanation of the nested ifelse: https://stackoverflow.com/a/18016872/13249862.

bttomio
  • 2,206
  • 1
  • 6
  • 17
  • 2
    but with this I will not be able to compare two stock one with positive another with negative return – simar Jul 16 '20 at 09:32
  • Could you please check if the updated code (nested ```ifelse```) solves your problem? – bttomio Jul 16 '20 at 09:44