Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Changing matrix entries to 1

    Hello,

    I am trying to edit a spatial weighting matrix created by the spmatrix create command in Stata. I would like to replace all the non-zero entries (>0) with 1, but this seems to not be working.

    I have the following code to do so, where spatial_weights is a weighting matrix created with spmatrix create:
    Code:
    spmatrix matafromsp W id = spatial_weights
    
    mata:
        for (i=1; i<=rows(W); i++) {
            for (j=1; j<=cols(W); j++) {
                if (W[i,j]:>0) W[i,j]=1
            }
        }
    end
    
    spmatrix spfrommata spatial_weights = W id, replace
    What I get as a result is a matrix of indeed same entries for all the previously non-zero entries, but they all take the value of .01065647570978. Any ideas of what am I doing wrong?

  • #2
    Maybe try something like the following?

    .ÿclearÿ*

    .ÿlocalÿline_sizeÿ`c(linesize)'

    .ÿsetÿlinesizeÿ72

    .ÿmatrixÿinputÿWÿ=ÿ(0ÿ0.1ÿ0ÿ\ÿ0ÿ0.2ÿ0.1)

    .ÿmatrixÿlistÿW

    W[2,3]
    ÿÿÿÿc1ÿÿc2ÿÿc3
    r1ÿÿÿ0ÿÿ.1ÿÿÿ0
    r2ÿÿÿ0ÿÿ.2ÿÿ.1

    .ÿmata:
    -------------------------------------------------ÿmataÿ(typeÿendÿtoÿexit
    >ÿ)ÿ--------------------------------------------------------------------
    :ÿWÿ=ÿst_matrix("W")

    :ÿW
    ÿÿÿÿÿÿÿÿ1ÿÿÿÿ2ÿÿÿÿ3
    ÿÿÿÿ+----------------+
    ÿÿ1ÿ|ÿÿÿ0ÿÿÿ.1ÿÿÿÿ0ÿÿ|
    ÿÿ2ÿ|ÿÿÿ0ÿÿÿ.2ÿÿÿ.1ÿÿ|
    ÿÿÿÿ+----------------+

    :ÿWÿ=ÿW:>0

    :ÿW
    ÿÿÿÿÿÿÿ1ÿÿÿ2ÿÿÿ3
    ÿÿÿÿ+-------------+
    ÿÿ1ÿ|ÿÿ0ÿÿÿ1ÿÿÿ0ÿÿ|
    ÿÿ2ÿ|ÿÿ0ÿÿÿ1ÿÿÿ1ÿÿ|
    ÿÿÿÿ+-------------+

    :ÿst_matrix("W",ÿW)

    :ÿend
    ------------------------------------------------------------------------

    .ÿmatrixÿlistÿW

    W[2,3]
    ÿÿÿÿc1ÿÿc2ÿÿc3
    r1ÿÿÿ0ÿÿÿ1ÿÿÿ0
    r2ÿÿÿ0ÿÿÿ1ÿÿÿ1

    .ÿsetÿlinesizeÿ`line_size'

    .

    Comment


    • #3
      Be default, the spmatrix command applies a spectral normalization of the matrix. Use the option normalize(none) if you do not want any normalization (which would be unusual) or normalize(row) if you want a row standardization (which might have undesired effects on the implied network structure and will also change your entries to different numbers than one).
      https://twitter.com/Kripfganz

      Comment


      • #4
        Originally posted by Sebastian Kripfganz View Post
        Be default, the spmatrix command applies a spectral normalization of the matrix. Use the option normalize(none) if you do not want any normalization (which would be unusual) or normalize(row) if you want a row standardization (which might have undesired effects on the implied network structure and will also change your entries to different numbers than one).
        Sebastian Kripfganz , thank you, you are spot on. I thought for some reason that normalization is only applied with spmatrix create and not spmatrix spfrommata, and that I need to manually normalize it with spmatrix normalize after my transformation.That's why I didn't expect my matrix to already be normalized.

        Comment

        Working...
        X