Skip to content

why doesn't ifelse work ?

8 messages · eric, Andrew Robinson, David Winsemius +2 more

#
I have the following lines of code:

ind <- rollapply(GSPC, 200, mean)
signal <- ifelse(diff(ind, 5) > 0 , 1 , -1)
signal[is.na(signal)] <- 0

I never get a value of -1 for signal even though I know diff(ind , 5) is
less than zero frequently. It looks like when diff(ind , 5) is less than
zero, signal gets set to 0 instead of - 1. Any ideas why ?  Here's some
information on ind and diff(ind, 5) :
[1] "logical"
[1] "zoo"
?zoo? series from 1990-05-31 to 2010-12-02
  Data: logi [1:5171, 1] FALSE FALSE FALSE FALSE FALSE FALSE ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr "GSPC.Adjusted"
  Index:  Date[1:5171], format: "1990-05-31" "1990-06-01" "1990-06-04"
"1990-06-05" "1990-06-06" "1990-06-07" "1990-06-08" "1990-06-11" ...
[1] "zoo"
[1] "numeric"
?zoo? series from 1990-05-23 to 2010-12-02
  Data: num [1:5176, 1] 339 339 338 338 338 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr "GSPC.Adjusted"
  Index:  Date[1:5176], format: "1990-05-23" "1990-05-24" "1990-05-25"
"1990-05-29" "1990-05-30" "1990-05-31" "1990-06-01" "1990-06-04" 

--
View this message in context: http://r.789695.n4.nabble.com/why-doesn-t-ifelse-work-tp3482680p3482680.html
Sent from the R help mailing list archive at Nabble.com.
#
Hi Eric,

tough to say.  Please try to provide commented, minimal,
self-contained, reproducible code.

Cheers

Andrew
On Thu, Apr 28, 2011 at 06:46:16PM -0700, eric wrote:

  
    
#
equire(quantmod)
require(PerformanceAnalytics)
rm(list=ls())
getSymbols("^GSPC", src="yahoo", from="1990-01-01", to=Sys.Date())
GSPC <-na.omit(Ad(GSPC))
ind <- rollapply(GSPC, 200, mean)
signal <- ifelse(diff(ind, 5) > 0 , 1 , -1)
signal[is.na(signal)] <- 0

--
View this message in context: http://r.789695.n4.nabble.com/why-doesn-t-ifelse-work-tp3482680p3482737.html
Sent from the R help mailing list archive at Nabble.com.
#
Hi:

It seems to work for me...here's a reproducible example.

set.seed(2053)
date <- seq(as.Date('1990-01-01'), by = 'days', length = 5000)
range(date)
# [1] "1990-01-01" "2003-09-09"
date <- sort(sample(date, 2000))
tdata <- data.frame(date = date, GSPC = rpois(2000, 1000))

library(zoo)
tdata2 <- tdata           # copy data frame just to be on the safe side
# Index GSPC by date
tdata2$GSPC <- with(tdata2, zoo(GSPC, date))
ind <- with(tdata2, rollmean(GSPC, 200, align =  'right'))
signal <- ifelse(diff(ind, 5) > 0, 1, -1)
signal
 -1   1
903 893

Perhaps you can explain your problem in this context.

Dennis
On Thu, Apr 28, 2011 at 6:46 PM, eric <ericstrom at aol.com> wrote:
#
from the console ...
signal
   0    1 
1286 3885 

note there is no -1 value.

This is consistent with what I see if if plot(signal). When I issue that
statement from the console, I see signal vary between 0 and 1.0 but it never
goes to - 1



--
View this message in context: http://r.789695.n4.nabble.com/why-doesn-t-ifelse-work-tp3482680p3482792.html
Sent from the R help mailing list archive at Nabble.com.
#
On Apr 28, 2011, at 10:28 PM, eric wrote:

            
> table(coredata(diff(ind,5)) >0)

FALSE  TRUE
  1286  3886
David Winsemius, MD
West Hartford, CT
#
Hi:
On Thu, Apr 28, 2011 at 7:28 PM, eric <ericstrom at aol.com> wrote:
# Could you please not do this in the middle of a code chunk?
# Anyone who copies/pastes this into his/her session will lose everything
# (s)he had been doing.
ind returns what is effectively a matrix with six columns. If you do
the same with the differences,
?zoo? series from 1990-05-31 to 2010-12-03
  Data: num [1:5172, 1:6] -1.303 -1.248 -1.13 -1.003 -0.946 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:6] "GSPC.Open" "GSPC.High" "GSPC.Low" "GSPC.Close" ...
  Index: Class 'Date'  num [1:5172] 7455 7456 7459 7460 7461 ...
GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume GSPC.Adjusted
1990-05-31  -1.30300  -1.24530 -1.26890   -1.24725      295150      -1.24725
1990-06-01  -1.24750  -1.16410 -1.18735   -1.12970      313100      -1.12970
1990-06-04  -1.12995  -1.04920 -1.05475   -1.00325      334950      -1.00325
1990-06-05  -1.00345  -0.96625 -0.97210   -0.94540      134750      -0.94540
1990-06-06  -0.94560  -0.94210 -0.91795   -0.92290     -448450      -0.92290
1990-06-07  -0.92295  -0.91270 -0.89700   -0.90335     -544200      -0.90335

This informs you why the ifelse() statement doesn't make sense.
Perhaps this is what you had in mind:
$GSPC.Open

  -1    1
1285 3887

$GSPC.High

  -1    1
1283 3889

$GSPC.Low

  -1    0    1
1284    1 3887

$GSPC.Close

  -1    1
1286 3886

$GSPC.Volume

  -1    0    1
1349    1 3822

$GSPC.Adjusted

  -1    1
1286 3886

## with
GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume GSPC.Adjusted
1990-05-31        -1        -1       -1         -1           1            -1
1990-06-01        -1        -1       -1         -1           1            -1
1990-06-04        -1        -1       -1         -1           1            -1
1990-06-05        -1        -1       -1         -1           1            -1
1990-06-06        -1        -1       -1         -1          -1            -1
1990-06-07        -1        -1       -1         -1          -1            -1
[1] 0
[1] 2

Thank you for providing a reproducible example.

HTH,
Dennis
#
Hi Eric,
On Thu, Apr 28, 2011 at 6:46 PM, eric <ericstrom at aol.com> wrote:
If you had looked at signal here, you would see that it is logical.
but this is not doing what you almost certainly expect it is.  Consider:

ind[is.na(ind)]
ind[!is.na(ind)]

nothing is selected.  Gabor or Achim would know for sure, but I
believe this is because zoos have methods for extraction (i.e.,
`[.zoo`), which does not work with matrices, and is.na(ind) returns a
matrix.  So all you are doing with:

signal[is.na(signal)] <- 0

is converting the class from logical to numeric

which is why you get 0s and 1s instead of TRUEs and FALSEs.
This should have set off red flags.  You are using a core, nongeneric
function, ifelse, with a very special class of object.  In fact, even
your test is not a regular old logical object, it is a zoo.  You can
get lucky and everything still work as expected, and often times
authors have written methods that accompany their classes so
everything is handled automagically in the background without you ever
needing to know about it.  But in this case, ifelse is not generic, so
there are not any special methods written for it (side note, there is
an ifelse.zoo, but it also does not quite do what I think you want).

If you look at the code for ifelse, you will see that it returns the
results of the test, AFTER updating any values that meet (or do not
meet) the test AND are not NA.  But wait, we already saw earlier what
the results of:  ind[!is.na(ind)] were, and you can tell from the
class of your test (class(diff(ind, 5) > 0) that it is a zoo itself.
This all seems to end up with ifelse() not seeing any values that
match the criteria, so it simple returns the results of the the test
itself (i.e., diff(ind, 5) > 0).  Then you try to replace any NA
values with 0, which converts the logical test results to numeric and
because of how logical data is stored (FALSE = 0, TRUE = 1), you get a
bunch of 0s and 1s.

David already alluded to this solution, but you can use coredata (see
?zoo for details) to get what I suspect are the results you are after:

signal <- ifelse(diff(coredata(ind), 5) > 0, 1, -1)

mind you "signal" will not be a zoo anymore.

Cheers,

Josh