Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Re: st: Making a Matrix from Three Variables


From   "Glenn Goldsmith" <[email protected]>
To   <[email protected]>
Subject   Re: Re: st: Making a Matrix from Three Variables
Date   Sun, 10 May 2009 11:45:01 +0100

<>

Good call on the missing values front.

The other thing to potentially be wary of is that missing values in your
rows or cols variables will cause the -sparse()- code to do odd things. A
possible fix is below, but it may be easiest just to ensure there aren't any
missing values in your initial x matrix.

*********************************************************
mata:
real matrix sparse(real matrix x)
 {
  real matrix y
  real scalar k

  y = J(colmax(x[,1]),colmax(x[,2]),.)
  for (k=1; k<=rows(x); k++) {
    if (x[k,1]!=. & x[k,2]!=.) y[x[k,1],x[k,2]] = x[k,3]
  }

  return(y)
 }
end
**********************************************************

Tirthankar Chakravarty <[email protected]> wrote:

<>
<>
Many apologies. Read Glenn for Allan.

T

On Sat, May 9, 2009 at 10:38 PM, Tirthankar Chakravarty
<[email protected]> wrote:
> <>
> <>
> 1) Amadou's solution can be streamlined considerably by replacing the
> first part as in the code included below.
>
> 2) The line:
> tomata x y z, missing
> does not work because -missing- is not a valid option for -tomata- (Gould,
SSC).
>
> 3) Allan's solution is efficient, but you might want to watch out for
> missing values in your original data. So, a slight modification is
> included below.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index