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.
Thanks!
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);

Comment