Announcement

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

  • Access members of scalar struct interactively but not vector struct

    Hi everyone,
    I am running code to create a series of predicted state variables from initial distribution and transition parameters. I have been storing the state space for each period as an orgtype scalar structure, and the series of structures as orgtype vector. However, I am confused that when just using a scalar structure (for one draw) I can interactively access the members, but for the vector structure this is no longer true. I saw this previous post on the topic (https://www.statalist.org/forums/for...anding-structs), but that seemed to indicate that the members should always be inaccessible from interactive mode. I am just wondering if this is something that changed since then or if I have a coding issue making them sometimes accessible and not others. Below is a simplified example.

    Code:
    . version 17
    . set matastrict on
    .
    . //Define structures used
    . mata:
    ------------------------------------------------- mata (type end to exit) --------------------------------------------------
    :         //Structure to hold state space values
    :                 struct state_space {
    >                         real scalar s1 //Aggregate state
    >                 }
    
    : end
    ----------------------------------------------------------------------------------------------------------------------------
    
    .        
    . /*-------------------------------------------------------------------*/
    . **#Simulate initial state draw
    . /*------------------------------------------------------------------            
    >         Inputs
    >         ----------
    >                 Omega : Vector of state space variable mean and standard deviations
    >                       real matrix (1,2)
    >         Outputs
    >         -------
    >                 eta : Initial state space draw from distributions
    >                       struct state_space scalar
    > --------------------------------------------------------------------*/
    . mata:
    ------------------------------------------------- mata (type end to exit) --------------------------------------------------
    : struct state_space scalar draw_eta(real matrix Omega) {
    >         //Define variables
    >                 struct state_space scalar eta
    >         //Get draw
    >                 eta.s1 = rnormal(1, 1, Omega[1,1], Omega[1,2])
    >         //Return structure
    >                 return(eta)
    >         }
    
    : end
    ----------------------------------------------------------------------------------------------------------------------------
    
    .        
    . /*-------------------------------------------------------------------*/
    . **#Create series of state space draws
    . /*------------------------------------------------------------------
    >         Inputs
    >         ----------
    >                T : Number of iterations to run
    >                       real scalar
    >                Omega : Vector of state space variable mean and standard deviations
    >                       real matrix (1,2)
    >                                
    >         Outputs
    >         -------
    >                 eta : Series of state space draws
    >                      struct state_space vector
    >
    > --------------------------------------------------------------------*/
    . mata:
    ------------------------------------------------- mata (type end to exit) --------------------------------------------------
    : struct state_space vector eta_series(real scalar T, transmorphic Omega) {
    >         //Define variables
    >                 struct state_space vector eta
    >                 real scalar t  
    >         //Repeat draws of state variables
    >                 //Vector to store state variables in
    >                         eta = J(T,1, state_space()) //Create Tx1 matrix of type struct state_space that stores state space
    >  structures for each iteration
    >                 //Loop over iterations
    >                         for (t=1;t<=T;t++) {
    >                                 eta[t] = draw_eta(Omega)
    >                         }
    >         //Return state series
    >                 display("First draw read during non-interactive:")
    >                         eta[1].s1
    >                 return(eta)
    >         }
    
    : end
    ----------------------------------------------------------------------------------------------------------------------------
    
    .
    . mata:
    ------------------------------------------------- mata (type end to exit) --------------------------------------------------
    : //Set initial distribution
    : Omega = (0,1)
    
    : //Draw one state
    : eta0 = draw_eta(Omega)
    
    : //Show value
    : eta0.s1
      -.2393540124
    
    : //Draw multiple states
    : eta = eta_series(5, Omega)
    First draw read during non-interactive:
      .5165489944
    
    : //Show first state value
    : eta[1].s1
    type mismatch:  exp.exp:  transmorphic found where struct expected
    (0 lines skipped)
    ----------------------------------------------------------------------------------------------------------------------------
    r(3000);
    Thanks!

  • #2
    Puzzling. I cannot say what is going on. I can replicate your example. More specifically, this code

    Code:
    // set to your version
    
    mata:
    
    mata set matastrict on
    
    struct state_space {
        real scalar s1 //Aggregate state
    }
    
    struct state_space scalar draw_eta(real matrix Omega) {
        struct state_space scalar eta
        eta.s1 = rnormal(1, 1, Omega[1,1], Omega[1,2])
    return(eta)
    }
    
    struct state_space vector eta_series(real scalar T, transmorphic Omega) {
    
                     struct state_space vector eta
                     real scalar t  
                             eta = J(T,1, state_space()) //Create Tx1 matrix of type struct state_space that 
                             for (t=1;t<=T;t++) {
                                     eta[t] = draw_eta(Omega)
                             }
                     display("First draw read during non-interactive:")
                             eta[1].s1
                     return(eta)
    }
    
    
    end
    
    mata :
    
    Omega = (0,1)
        
    eta0 = draw_eta(Omega)
    
    eltype(eta0)
    
    liststruct(eta0)
    
    eta0.s1
    
    eta = eta_series(1, Omega)
    
    eltype(eta)
    
    liststruct(eta)
    
    eta[1].s1
    
    end
    executed in Stata 11 or Stata 12 will exit with error at

    Code:
    eta0.s1
    That is, in Stata 11 and Stata 12 I cannot access the struct scalar eta0 interactively. This is what I expect.

    However, the very same code executed in Stata 16 or higher will access the struct scalar eta0 in interactive mode; moreover, liststruct(eta) will list the contents of struct vector eta. Yet, the elements of struct vector eta cannot be accessed.

    If you are on Stata 18, I would ask tech support for an explanation the observed behavior.

    Comment


    • #3
      Thanks for looking into this. Being able to access some of the members interactively is quite convenient and useful, so I really have no complaints- but I appreciate the validation that it isn't a unique quirk on my end.
      -Lena

      Comment

      Working...
      X