turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

12-31-2013 02:00 AM

I have a specific data and now I need to know how I can produce a matrix with the following formula:

1/2+sum of multiplications of row(i)-row(n). That is to say, I have a matrix with let say 2 columns and 3 rows and I need to produce a new matrix by calculating a value for each element (value in the cell=v(row,column). In the real matrix the number of rows and columns are 106 and 133...

First the original matrix

column1 column2

row1 v(1,1) v(1,2)

row2 v(2,1) v(2,2)

row3 v(3,1) v(3,2)

And then the desired one. Read carefully, v(row, column)'s come from the upper matrix :

column1 column2

row1 1/2+v(1,1)+v(1,1)*v(2,1)+v(1,1)*v(2,1)*v(3,1) 1/2+v(1,2)+v(1,2)*v(2,2)+v(1,2)*v(2,2)*v(3,2)

row2 1/2+v(2,1)+v(2,1)*v(3,1) 1/2+v(2,2)+v(2,2)*v(3,2)

row3 1/2+v(3,1) 1/2+v(3,2)

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Milla

12-31-2013 03:44 PM

Let A be the desired matrix without the 1/2 term. Notice that formula for first column of A is

v11(1+v21*(1+v31))

v21*(1+v31))

v31

and similar for other columns. Therefore you can compute A by

computing from the bottom row to the top. The i_th row of A is the i_th row of V times (1 + i_th row of A),

as follows. (Then add the 1/2 at the end.)

proc iml;

V = {1 2,

3 4,

5 6};

N = nrow(V);

A = j(N, ncol(V), .); /* allocate */

A[N, ] = V[N, ]; /* assign last row */

do i = N-1 to 1 by -1;

A[i, ] = V[i, ]#(1 + A[i+1, ]);

end;

A = 1/2 + A;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rick_SAS

01-02-2014 01:58 AM

Thank you so much Rick! Now the problem seems to be that I have some missing values and those should be skipped. So basically if there is a missing value, then each term should be multiplied just like before but skip the missing terms. I was thinking whether I could just replace the missing values with 1 or 0 and then do the proc iml, but then the formula is carried out also for the terms that used to be missing.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Milla

01-02-2014 06:29 AM

Dealing with missing values can be compliated. Mathematically, they can/should propogate, but you are intending to replace missing values with a nonmissing, which can be dangerous because the missing values are often there for a reason. You are biasing your results if you arbitrarily replace missing values by 0 or 1. Statisticians tend to use some imputation technique instead of simple repacement.

If you insist on replacing the missing values, try some variation of this approach. Inside the loop, use these formulas:

Q = choose(V[i, ]=., 0, V[i, ]);

R = choose(V[i, ]=., 1, V[i, ]);

A[i, ] = Q + R#A[i+1, ];

I don't know what you want to do if there is a missing value in the last row. Presumably use the 'Q' formula.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rick_SAS

03-11-2014 02:08 AM

Hello again. I'm still working on my codes so there's a small change. Can you tell me what to change in the code to leave the LAST row as it is?

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Milla

03-11-2014 06:52 AM

Why not save the last row in a vector, apply the transformation, and then restore the original values for the last row?