Find rows of matrix containing na values

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Jason, EViews Moderator

tho_mi
Posts: 61
Joined: Sat May 12, 2012 2:41 am

Find rows of matrix containing na values

Postby tho_mi » Sat Mar 24, 2018 11:01 am

Hey,

I have a matrix in which one column might contain na values. I'd like to get rid of the corresponding rows. What is the easiest way to do that?

Thanks in advance!

EViews Matt
EViews Developer
Posts: 238
Joined: Thu Apr 25, 2013 7:48 pm

Re: Find rows of matrix containing na values

Postby EViews Matt » Mon Mar 26, 2018 9:46 am

Hello,

Building on this thread for removing NAs from a vector, the following returns a matrix with the rows containing NAs removed (replacing "m" with the name of your matrix).

Code: Select all

m.@droprow(@emult(@eneq(m * @ones(@rows(m)), m * @ones(@rows(m))), @ranks(@ones(@rows(m)), "a", "i")))

or

Code: Select all

m.@droprow(@emult(@nan(m * @zeros(@rows(m)), 1), @ranks(@ones(@rows(m)), "a", "i")))

tho_mi
Posts: 61
Joined: Sat May 12, 2012 2:41 am

Re: Find rows of matrix containing na values

Postby tho_mi » Tue Mar 27, 2018 6:52 am

Unfortunately this doesn't work.

"@DROPROW is not a member or procedure of M in ..."

Additional question:
What do "@nan" and "@zeros" do? Both are missing in the documentation.

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 11733
Joined: Tue Sep 16, 2008 5:38 pm

Re: Find rows of matrix containing na values

Postby EViews Gareth » Tue Mar 27, 2018 7:22 am

Version of EViews?
Follow us on Twitter @IHSEViews

tho_mi
Posts: 61
Joined: Sat May 12, 2012 2:41 am

Re: Find rows of matrix containing na values

Postby tho_mi » Tue Mar 27, 2018 7:54 am

9.5

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 11733
Joined: Tue Sep 16, 2008 5:38 pm

Re: Find rows of matrix containing na values

Postby EViews Gareth » Tue Mar 27, 2018 7:59 am

@droprow is 10 only (there's a reason we ask you to state your version of EViews in every post!).

@zeros creates a matrix of zeros.

@nan changes NAs to some other value.
Follow us on Twitter @IHSEViews

tho_mi
Posts: 61
Joined: Sat May 12, 2012 2:41 am

Re: Find rows of matrix containing na values

Postby tho_mi » Tue Mar 27, 2018 8:14 am

@droprow:
I assumed that such a basic operation would've been available "earlier".

@zeros:
Ok, so that means the second argument is the number of columns.

@nan:
So I guess the first argument is the "source" matrix/vector and the second argument the value the NAs are changed to?

Since @droprow isn't available that means the question is still open. I assume the easiest way would be to sort the matrix with respect to the column with the NAs and then extracting the submatrix?

Btw, it would be quite helpful if the documentation would have the information on the version number (or at least a note if the procedure or whatever is only available in the newest version).

edit:
I really appreciate your help here (and the fact that you usually reply pretty fast), but the documentation is sometimes annoying because from my perspective important information is missing (like the version number or some procedures, e.g. @nan, which are simply not included).

tho_mi
Posts: 61
Joined: Sat May 12, 2012 2:41 am

Re: Find rows of matrix containing na values

Postby tho_mi » Tue Mar 27, 2018 8:44 am

If the NAs are only present in one column (column 3 here) the following does the job:

Code: Select all

vector m_ranks = @ranks(@columnextract(m, 3), "a", "i")
m = @capplyranks(m, m_ranks)

vector column_with_nas = @columnextract(m, 3)
!current_value = column_with_nas(1)
!i = 1

while @isna(!current_value)
   !i = !i + 1
   !current_value = column_with_nas(!i)
wend

m = @subextract(m, !i, 1)


m is the matrix to be "cleaned". It has to be sorted ascending first, so that the NAs are at the beginning.

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 11733
Joined: Tue Sep 16, 2008 5:38 pm

Re: Find rows of matrix containing na values

Postby EViews Gareth » Tue Mar 27, 2018 8:46 am

Follow us on Twitter @IHSEViews

EViews Matt
EViews Developer
Posts: 238
Joined: Thu Apr 25, 2013 7:48 pm

Re: Find rows of matrix containing na values

Postby EViews Matt » Tue Mar 27, 2018 9:22 am

Here's a more general, EViews 9.5-friendly solution that (1) doesn't require all the NAs to be in the same column, and (2) preserves the order of the non-NA-containing rows. This solution constructs a new matrix as the result rather than modifying the original matrix.

Code: Select all

matrix(1,m.@cols) m2
for !i = 1 to @rows(m)
   if not @isna(@inner(@rowextract(m, !i))) then
      m2 = @vcat(m2, @transpose(@rowextract(m, !i)))
   endif
next
m2 = @subextract(m2, 2, 1)

tho_mi
Posts: 61
Joined: Sat May 12, 2012 2:41 am

Re: Find rows of matrix containing na values

Postby tho_mi » Tue Mar 27, 2018 9:29 am

Thanks! In my case the matrix has to be sorted anyway (and the NAs are only in one column), so it doesn't have to be that general (but I'm sure it's helpful for other people) :)


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 8 guests