# Variable Types

Table 65 shows the different type of script variables that are available in the verbatim sections of PoPy scripts:-

Table 65 PoPy Variable Types
Type | Description | Defined | Used
--- | --- | --- | ---
`c[X]` (fit script) | covariates (fit) | data file/PREPROCESS | main sections
`c[X]` (gen script) | covariates (gen) | EFFECTS | main sections
`c[X]` | observations | PREDICTIONS (fit) | PREDICTIONS (gen)
`f[X]` | fixed effects | EFFECTS | MODEL_PARAMS
`r[X]` | random effects | EFFECTS | MODEL_PARAMS
`m[X]` | Model parameters | MODEL_PARAMS | main sections
`w[X]` | Workspace Variables | MODEL_PARAMS | MODEL_PARAMS
`x[NEWIND]` | First row for individual | N/A | MODEL_PARAMS
`d[X]` | Derivatives wrt time | DERIVATIVES | N/A
`s[X]` | States | DERIVATIVES/STATES | DERIVATIVES/PREDICTIONS
`x[TIME]` | Continuous time | N/A | DERIVATIVES
`p[X]` | Predictions | PREDICTIONS | PREDICTIONS

In Table 65, the ‘Defined’ column shows where a variable of particular type is first declared, typically on the left hand side of an ‘=’ or ‘~’ operator. The ‘Used’ column shows where a variable may be used, typically on the right hand side of an ‘=’ or ‘~’ operator.

Note the entry ‘main sections’ in table Table 65 above means the following sections - MODEL_PARAMS/STATES/DERIVATIVES/PREDICTIONS.

## Parameterized Variables

There are circumstances where many compartments in the model follow the same basic structure. When using delay compartments, for example, there is typically only a flow in from the previous compartment and a flow out to the next one:

    DERIVATIVES: |
        d[DEPOT]   = @bolus{amt:c[AMT]} - m[K]*s[DEPOT]
        d[DELAY1]  = m[K]*s[DEPOT]      - m[K]*s[DELAY1]
        d[DELAY2]  = m[K]*s[DELAY1]     - m[K]*s[DELAY2]
        d[DELAY3]  = m[K]*s[DELAY2]     - m[K]*s[DELAY3]
        d[DELAY4]  = m[K]*s[DELAY3]     - m[K]*s[DELAY4]
        d[CENTRAL] = m[K]*s[DELAY4]     - m[KE]*s[CENTRAL]

Expressing these models can be time-consuming and error-prone, so PoPy provides a means to define multiple compartments with parameters in the definitions.

To define a sequence of numbered compartments (DELAY1, DELAY2, DELAY3, and so on) use braces on the left hand side to define the index variable (e.g., “i”) and the range of numbers it should take on (e.g., “1..4” to denote 1, 2, 3 and 4). This line will be replicated for every value of the index, i, in the specified range, and the braces (along with their contents) will be replaced with the corresponding value of i.

On the right hand side, you may use expressions with simple functions of the index variable (also within braces) to refer to a parameterised compartment. When i=2, for example, c[DELAY{i-1}] refers to c[DELAY1] and so on. The model above then becomes:

    DERIVATIVES: |
        d[DEPOT]         = @bolus{amt:c[AMT]} - m[K]*s[DEPOT]
        d[DELAY1]        = m[K]*s[DEPOT]      - m[K]*s[DELAY1]

        # this line...
        d[DELAY{i:2..4}] = m[K]*s[DELAY{i-1}] - m[K]*s[DELAY{i}]

        # ...expands to, and is therefore equivalent to, these lines:
        # d[DELAY2]      = m[K]*s[DELAY1]     - m[K]*s[DELAY2]  # i = 2
        # d[DELAY3]      = m[K]*s[DELAY2]     - m[K]*s[DELAY3]  # i = 3
        # d[DELAY4]      = m[K]*s[DELAY3]     - m[K]*s[DELAY4]  # i = 4

        d[CENTRAL]       = m[K]*s[DELAY4]     - m[KE]*s[CENTRAL]

It is also possible to use parameterized variables in the same way within a sum() function for the sake of simplicity:

    PREDICTIONS: |
        p[TOTAL_CONC] = sum(s[CMT{i:1..4}])
        ...