Announcement

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

  • st_view() function ... does it only work inside another mata function?

    Hi -- I might have an old manual, but this is in regards to st_view(). Does st_view() not work when using mata interactively? Only when using it inside of a user-written function? Thank you! -- P

  • #2
    Originally posted by Paul Rathouz View Post
    Does st_view() not work when using mata interactively?
    Seems to work interactively as well (Stata Release 18).

    .ÿsysuseÿauto,ÿclear
    (1978ÿautomobileÿdata)

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

    .ÿsetÿlinesizeÿ80

    .ÿmata:
    -------------------------------------------------ÿmataÿ(typeÿendÿtoÿexit)ÿ------
    :ÿst_sview(A=(.),ÿ.,ÿ"make")

    :ÿA[1..2,ÿ1]
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ1
    ÿÿÿÿ+---------------+
    ÿÿ1ÿ|ÿÿAMCÿConcordÿÿ|
    ÿÿ2ÿ|ÿÿÿÿAMCÿPacerÿÿ|
    ÿÿÿÿ+---------------+

    :ÿst_view(A=(.),ÿ.,ÿ"price")

    :ÿA[1..2,ÿ1]
    ÿÿÿÿÿÿÿÿÿÿ1
    ÿÿÿÿ+--------+
    ÿÿ1ÿ|ÿÿ4099ÿÿ|
    ÿÿ2ÿ|ÿÿ4749ÿÿ|
    ÿÿÿÿ+--------+

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

    .ÿsetÿlinesizeÿ`line_size'

    .

    Comment


    • #3
      Ahh. .. I think I need to put -A=(.)- in the first argument. I was just putting -A- in there. Alternatively, should one declare -A- before -st_view()- ? It seems that -st_view()- is setting up -A- as a sort of pointer (maybe not in the technical mata sense), so a declaration seemed like the wrong way to go here. Thx! -- P

      Comment


      • #4
        Originally posted by Paul Rathouz View Post
        Alternatively, should one declare -A- before -st_view()- ?
        Yes, you can do that as an alternative to inline declaration in the argument list. See below.

        .ÿquietlyÿsysuseÿauto

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

        .ÿsetÿlinesizeÿ80

        .ÿmata:
        -------------------------------------------------ÿmataÿ(typeÿendÿtoÿexit)ÿ------
        :ÿAÿ=ÿ(.)

        :ÿeltype(A)
        ÿÿreal

        :ÿorgtype(A)
        ÿÿscalar

        :ÿst_sview(A,ÿ.,ÿ"make")

        :ÿA[1..2,ÿ1]
        ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ1
        ÿÿÿÿ+---------------+
        ÿÿ1ÿ|ÿÿAMCÿConcordÿÿ|
        ÿÿ2ÿ|ÿÿÿÿAMCÿPacerÿÿ|
        ÿÿÿÿ+---------------+

        :ÿA[1,ÿ1]ÿ=ÿ"Hello,ÿWorld!"

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

        .ÿsetÿlinesizeÿ`line_size'

        .ÿlistÿmakeÿinÿ1/2,ÿnoobs

        ÿÿ+---------------+
        ÿÿ|ÿmakeÿÿÿÿÿÿÿÿÿÿ|
        ÿÿ|---------------|
        ÿÿ|ÿHello,ÿWorld!ÿ|
        ÿÿ|ÿAMCÿPacerÿÿÿÿÿ|
        ÿÿ+---------------+

        .ÿmata:ÿeltype(A)
        ÿÿstring

        .ÿmata:ÿorgtype(A)
        ÿÿcolvector

        .


        It seems that -st_view()- is setting up -A- as a sort of pointer (maybe not in the technical mata sense), so a declaration seemed like the wrong way to go here.
        It seems to me that it's definitely setting up pointers behind the scenes—see the way that changing the Mata colvector's element's content changes the corresponding observation in the Stata dataset—nevertheless, you can do the declaration either beforehand as above or inline. As the documentation states, the function overwrites whatever you've declared.

        Comment


        • #5
          Thank you! I seem to recall now that it does not what A is declared _as_ ... just that is it something. I note above that you declared it as a real scalar, but then it points to a nx2 matrix. I have to go back and check, but it seems like inside of a -mata- function, it is not necessary to do this declaration. I pass the character vector variable name (e.g., -"make"- in your example, and then issue, in the function
          : st_view(y,.,"make")
          without declaring y in any way and it seems to work fine. If I have any insight on that, I will pass along when I get back to it. -- P

          Comment


          • #6
            So, returning to this issue, I am struggling to understand the logic here. Below, we see that inside of a mata function, we can reference a mata matrix inside -st_view()- without declaring it, but not in interactive mode. Why is this?

            . clear all

            . set obs 6
            Number of observations (_N) was 0, now 6.

            . gen myVar=_n+5

            . list myVar

            +-------+
            | myVar |
            |-------|
            1. | 6 |
            2. | 7 |
            3. | 8 |
            4. | 9 |
            5. | 10 |
            |-------|
            6. | 11 |
            +-------+

            .
            .
            . mata
            ------------------------------------------------- mata (type end to exit) -----
            : mata clear

            :
            : st_view(V,.,"myVar")
            <istmt>: 3499 V not found
            r(3499);

            : st_view(V=(5),.,"myVar")

            : V'
            1 2 3 4 5 6
            +-------------------------------+
            1 | 6 7 8 9 10 11 |
            +-------------------------------+

            : mata drop V

            :
            : void function tryView(string scalar getVar)
            > {
            > st_view(V,.,getVar)
            > V'
            > }
            note: variable V may be used before set.

            : tryView("myVar")
            1 2 3 4 5 6
            +-------------------------------+
            1 | 6 7 8 9 10 11 |
            +-------------------------------+

            : mata describe

            # bytes type name and extent
            -------------------------------------------------------------------------------
            128 void tryView()
            -------------------------------------------------------------------------------

            :
            : end
            -------------------------------------------------------------------------------

            .
            . exit

            Comment


            • #7
              Originally posted by Paul Rathouz View Post
              . . . inside of a mata function, we can reference a mata matrix inside -st_view()- without declaring it, but not in interactive mode. Why is this?
              Well, from the help file for Mata's interactive mode:
              The reasons are technical, but . . . Mata [in interactive mode] just does not understand that st_view() really does not need Z defined. The way around Mata's confusion is to define Z and then let st_view() redefine it . . .
              Just a guess but perhaps you're invoking the compiler when defining a function even in interactive mode and it can parse the tokens without declaration, as opposed to the interpreter that you're invoking in strictly line-by-line input.

              Comment


              • #8
                Thx. I am going to send this Q to stata tech support. It is not addressed in the -st_view()- manual entry. thx!

                Comment


                • #9
                  So I contacted stata tech support. It does not appear to be a st_view() issue so much as an issue of -set matastrict off/on-. I had it -off-. So, inside a function, did not need to declare. Something I do not totally understand is that when using mata in interactive mode, you do not declare things. for some reason in -st_view()-, mata wants the first argument declared, but for the foregoing reason, you have to declare inside the function.

                  Comment

                  Working...
                  X