Scripts/Missing Values

From J Wiki
Jump to: navigation, search

This script has cover functions to handle missing values where __ (negative infinity) is used to represent the missing value. This assumes that the value does not arise in normal calculations.

Note, there are various ways NA may be treated in calculations, e.g.

  • ignore it
  • treat as 0 or 1
  • carry it forward

In practice, different cover functions are needed for each of these.

Download the script: File:Na.ijs

NA=: __
isNA=: =&NA : (+. & $:)
notNA=: -. @ isNA
setNA=: NA * isNA
zeroNA=: * notNA
oneNA=: isNA + zeroNA

NB. following carry forward NA:
mplus=: setNA + + & zeroNA
mminus=: setNA + - & zeroNA
mtimes=: setNA + * & zeroNA
mdivide=: setNA + notNA * % & oneNA

NB. following ignore NA in totals, and leave NA in scans:
mlen=: +/ @ notNA
msum=: +/ @ (* notNA)
msumscan=: setNA + +/\ @ (* notNA)
msumscanr=: setNA + +/\. @ (* notNA)

NB. following works on lists:
msumscaninv=: 3 : 0
msk=. notNA y
NA (I.-.msk) } msk expand +/\^:_1 msk#y
)

NB. following is matrix product on matrices:
mmp=: msum @: mtimes "1 _

For example:

   A=: 2 3,NA,7 11
   B=: 0 1,NA,NA,4

   A mplus B
2 4 __ __ 15

   A mdivide B
_ 3 __ __ 2.75

   msumscan A
2 5 __ 12 23

  (2 3$A) mmp 3 4$B
12  2  3 0
44 15 11 2

Contributed by Chris Burke