# Model initialization

Source:`R/market_model.R`

, `R/diseq_basic.R`

, `R/diseq_deterministic_adjustment.R`

, and 3 more
`model_initialization.Rd`

Model initialization

## Usage

```
# S4 method for diseq_basic
initialize(
.Object,
quantity,
price,
demand,
supply,
subject,
time,
data,
correlated_shocks = TRUE,
verbose = 0
)
# S4 method for diseq_deterministic_adjustment
initialize(
.Object,
quantity,
price,
demand,
supply,
subject,
time,
data,
correlated_shocks = TRUE,
verbose = 0
)
# S4 method for diseq_directional
initialize(
.Object,
quantity,
price,
demand,
supply,
subject,
time,
data,
correlated_shocks = TRUE,
verbose = 0
)
# S4 method for diseq_stochastic_adjustment
initialize(
.Object,
quantity,
price,
demand,
supply,
price_dynamics,
subject,
time,
data,
correlated_shocks = TRUE,
verbose = 0
)
# S4 method for equilibrium_model
initialize(
.Object,
quantity,
price,
demand,
supply,
subject,
time,
data,
correlated_shocks = TRUE,
verbose = 0
)
```

## Arguments

- .Object
The object to be Constructed.

- quantity
The quantity variable of the system.

- price
The price variable of the system.

- demand
A formula representation of the right hand side of the demand equation.

- supply
A formula representation of the right hand side of the supply equation.

- subject
The subject identifier of the data set.

- time
The time identifier of the data set.

- data
The data set.

- correlated_shocks
Should the model be estimated using correlated shocks?

- verbose
Verbosity level.

- price_dynamics
A formula representation of the price equation.

## Details

The following two subsections describe the common initialization steps of all market model classes.

### Variable construction

The constructor prepares the model's variables using the passed
specifications. The specification variables are expected to be of type
`language`

. The right hand side specifications of the system are
expected to follow the syntax of `formula`

. The
construction of the model's data uses the variables extracted by these
specification. The demand variables are extracted by a
formula that uses the `quantity`

on the left hand side and the
`demand`

on the right hand side of the formula. The supply
variables are constructed by the `quantity`

and the
`supply`

inputs. In the case of the
`diseq_stochastic_adjustment`

model, the price dynamics'
variables are extracted using the `price dynamics`

input.
The `price dynamics`

for the `diseq_stochastic_adjustment`

should contain only terms other than that of excess demand. The excess demand term of
the price equation is automatically generated by the constructor.

### Data preparation

1. If the passed data set contains rows with NA values, they are dropped. If the verbosity level allows warnings, a warning is emitted reporting how many rows were dropped.

2. After dropping the rows, factor levels may be invalidated. If needed, the constructor readjusts the factor variables by removing the unobserved levels. Factor indicators and interaction terms are automatically created.

3. The primary key column is constructed by pasting the values of the columns of the subject and time variables.

4. In the cases of the `diseq_directional`

,
`diseq_deterministic_adjustment`

, and
the `diseq_stochastic_adjustment`

models, a column with lagged
prices is constructed. Since lagged prices are unavailable for the observations of
the first time point, these observations are dropped. If the verbosity level allows
the emission of information messages, the constructor prints the number of dropped
observations.

5. In the cases of the `diseq_directional`

and the `diseq_stochastic_adjustment`

models, a column with price
differences is created.

## Functions

`initialize(diseq_basic)`

: Basic disequilibrium model base constructor`initialize(diseq_deterministic_adjustment)`

: Disequilibrium model with deterministic price adjustment constructor`initialize(diseq_directional)`

: Directional disequilibrium model base constructor`initialize(diseq_stochastic_adjustment)`

: Disequilibrium model with stochastic price adjustment constructor`initialize(equilibrium_model)`

: Equilibrium model constructor

## Examples

```
simulated_data <- simulate_data(
"diseq_basic", 500, 3, # model type, observed entities, observed time points
-0.9, 8.9, c(0.3, -0.2), c(-0.03, -0.01), # demand coefficients
0.9, 6.2, c(0.03), c(-0.05, 0.02) # supply coefficients
)
# initialize the model
model <- new(
"diseq_basic", # model type
subject = id, time = date, quantity = Q, price = P,
demand = P + Xd1 + Xd2 + X1 + X2, supply = P + Xs1 + X1 + X2,
simulated_data, # data
correlated_shocks = FALSE # use independent shocks
)
show(model)
#> Basic Model for Markets in Disequilibrium:
#> Demand RHS : D_P + D_Xd1 + D_Xd2 + D_X1 + D_X2
#> Supply RHS : S_P + S_Xs1 + S_X1 + S_X2
#> Short Side Rule : Q = min(D_Q, S_Q)
#> Shocks : Independent
simulated_data <- simulate_data(
# model type, observed entities and time points
"diseq_deterministic_adjustment", 500, 3,
# demand coefficients
-0.9, 8.9, c(0.03, -0.02), c(-0.03, -0.01),
# supply coefficients
0.9, 4.2, c(0.03), c(0.05, 0.02),
# price adjustment coefficient
1.4
)
# initialize the model
model <- new(
"diseq_deterministic_adjustment", # model type
subject = id, time = date, quantity = Q, price = P,
demand = P + Xd1 + Xd2 + X1 + X2, supply = P + Xs1 + X1 + X2,
simulated_data, # data
correlated_shocks = TRUE # allow shocks to be correlated
)
show(model)
#> Deterministic Adjustment Model for Markets in Disequilibrium:
#> Demand RHS : D_P + D_Xd1 + D_Xd2 + D_X1 + D_X2
#> Supply RHS : S_P + S_Xs1 + S_X1 + S_X2
#> Short Side Rule : Q = min(D_Q, S_Q)
#> Separation Rule : P_DIFF analogous to (D_Q - S_Q)
#> Shocks : Correlated
# \donttest{
simulated_data <- simulate_data(
"diseq_directional", 500, 3, # model type, observed entities, observed time points
-0.2, 4.3, c(0.03, 0.02), c(0.03, 0.01), # demand coefficients
0.0, 4.0, c(0.03), c(0.05, 0.02) # supply coefficients
)
# in the directional model prices cannot be included in both demand and supply
model <- new(
"diseq_directional", # model type
subject = id, time = date, quantity = Q, price = P,
demand = P + Xd1 + Xd2 + X1 + X2, supply = Xs1 + X1 + X2,
simulated_data, # data
correlated_shocks = TRUE # allow shocks to be correlated
)
show(model)
#> Directional Model for Markets in Disequilibrium:
#> Demand RHS : D_P + D_Xd1 + D_Xd2 + D_X1 + D_X2
#> Supply RHS : S_Xs1 + S_X1 + S_X2
#> Short Side Rule : Q = min(D_Q, S_Q)
#> Separation Rule : P_DIFF >= 0 then D_Q >= S_Q
#> Shocks : Correlated
# }
simulated_data <- simulate_data(
# model type, observed entities and time points
"diseq_stochastic_adjustment", 500, 3,
# demand coefficients
-0.1, 9.8, c(0.3, -0.2), c(0.6, 0.1),
# supply coefficients
0.1, 7.1, c(0.9), c(-0.5, 0.2),
# price adjustment coefficient
1.4, 3.1, c(0.8)
)
# initialize the model
model <- new(
"diseq_stochastic_adjustment", # model type
subject = id, time = date, quantity = Q, price = P,
demand = P + Xd1 + Xd2 + X1 + X2, supply = P + Xs1 + X1 + X2,
price_dynamics = Xp1,
simulated_data, # data
correlated_shocks = TRUE # allow shocks to be correlated
)
show(model)
#> Stochastic Adjustment Model for Markets in Disequilibrium:
#> Demand RHS : D_P + D_Xd1 + D_Xd2 + D_X1 + D_X2
#> Supply RHS : S_P + S_Xs1 + S_X1 + S_X2
#> Price Dynamics RHS: I(D_Q - S_Q) + Xp1
#> Short Side Rule : Q = min(D_Q, S_Q)
#> Shocks : Correlated
simulated_data <- simulate_data(
"equilibrium_model", 500, 3, # model type, observed entities and time points
-0.9, 14.9, c(0.3, -0.2), c(-0.03, -0.01), # demand coefficients
0.9, 3.2, c(0.3), c(0.5, 0.02) # supply coefficients
)
# initialize the model
model <- new(
"equilibrium_model", # model type
subject = id, time = date, quantity = Q, price = P,
demand = P + Xd1 + Xd2 + X1 + X2, supply = P + Xs1 + X1 + X2,
simulated_data, # data
correlated_shocks = TRUE # allow shocks to be correlated
)
show(model)
#> Equilibrium Model for Markets in Equilibrium:
#> Demand RHS : D_P + D_Xd1 + D_Xd2 + D_X1 + D_X2
#> Supply RHS : S_P + S_Xs1 + S_X1 + S_X2
#> Market Clearing : Q = D_Q = S_Q
#> Shocks : Correlated
```