The Synthetic Data Generator Tab

The Synthetic Data Generator tab builds a dataset from a data generating process (DGP) in which every column declares a distribution. It lets you prepare regression, grouped, time-series, panel, and survival data without an external file. Because the true parameters used for generation are known, the data is useful for checking how an analysis method behaves and for practicing operations.

The coefficients used for generation do not always match what the fitted model estimates. Fitting a GLM to binary data that has random effects estimates population-averaged coefficients, whose absolute values are smaller than the group-specific conditional coefficients. Logistic regression coefficients are non-collapsible, so omitting a covariate that affects y changes the coefficient of x even when that covariate is independent of x. The relation between a coefficient placed in the Scale of a Weibull and the Cox regression coefficient is shown in Survival Times with Censoring.

The same generation can also run through the Agent API's datasets.generateSynthetic. See Agent API for the JSON format of the spec, the options, and the error codes.

Generating Data

Choosing Data > Generate Synthetic Data... in the menu bar opens the tab.

In the form, enter Dataset name, Rows, and Seed, then list columns under Columns and choose a distribution and its parameters for each.

The Synthetic Data Generator form

Generate Preview validates the input, generates the data, and shows the first 10 rows.

MIDAS uses a single sequence of random numbers in the order it evaluates the columns. Even with the same Seed, adding a column or changing the declaration order therefore also changes the values of the other columns.

Reviewing the Preview and Saving

Until you press Save as Dataset, no dataset is added to the project.

The preview and the Save as Dataset button

Pressing Save as Dataset creates the dataset, opens a Data Table tab, and closes the Synthetic Data Generator tab.

Saving under the same name as an existing dataset opens a dialog offering to rename, to append a number, or to replace. Replacing is available only when the target dataset is also synthetic data, that is, created with the Synthetic Data Generator tab or the Agent API's datasets.generateSynthetic.

Columns and Distributions

Each column has one distribution. The distribution parameters are given as expressions. Expressions can reference other columns, so dependencies between columns express the generating process. See Expression Syntax for how to write them.

There are 8 distributions to choose from, and the parameter fields change with the distribution. The meaning of each parameter and the shape of each distribution are explained in Probability Distribution Fundamentals.

DistributionParametersColumn data type
NormalMean, SDfloat64
UniformMin, Maxfloat64
Bernoulli (0/1)Pint64
CategoricalLevelsenum1
PoissonLambdaint64
GammaShape, Scalefloat64
WeibullShape, Scalefloat64
DeterministicValuefloat642

Deterministic has no noise and writes the expression value directly into the column. Use it for a linear predictor column or a transformed copy of another column.

For Categorical, write the levels in Levels one per field, and write the weight of each level in the field to its right. Leaving every weight field empty selects every level with equal probability. Each weight is itself an expression, so a weight can vary by row.

The level fields accept consecutive levels from the keyboard and from the clipboard. Pressing Enter in a level field adds an empty level field after it and moves the focus there. Pasting multi-line text makes MIDAS add one level per line. Copying a column from a spreadsheet and pasting it enters many levels at once.

Gamma and Weibull are parameterized by Shape and Scale. The mean of Gamma is Shape × Scale and its variance is Shape × Scale². The mean of Weibull is Scale × Γ(1 + 1/Shape), where Γ is the gamma function.

See Data Types and Measurement Scales for how column data types map to measurement scales.

When an expression evaluates to a value outside a parameter's domain, generation fails. Examples include an SD of Normal that is 0 or below, a Lambda of Poisson that is 0 or below, and a Max of Uniform that does not exceed Min3.

Every defined column becomes a column of the output dataset. There is no setting that excludes intermediate-calculation columns from the output.

Expression Syntax

An expression is text written in a distribution parameter, and it evaluates to a number.

An expression can reference other columns in the same definition by name. As long as the references have no cycle, the order of the columns does not matter. Numeric columns are referenced by writing their name directly. A column name that does not start with a letter or _ followed by letters, digits, and _ is enclosed in backticks, as in `my col`. Categorical columns and the factor columns created by Add Factor can only be referenced through cases, described below.

The available operators are the arithmetic operators + - * /, unary minus, and the comparisons < <= > >=. Precedence decreases from multiplication and division, to addition and subtraction, to comparisons, and parentheses make the order explicit. A comparison returns 1 when true and 0 when false. Because the result is a number, a comparison can be multiplied with other terms and used as a 0/1 indicator variable.

Two comparisons cannot be written in a row as in mathematical notation, and 0 < x < 1 is a parse error. Intervals are written as a product of two comparisons, as in (0 < x) * (x < 1). = and == are not available. Equality with a single value is also written as a product of two comparisons, as in (t >= 6) * (t <= 6).

The available functions and their argument counts are as follows.

FunctionArgumentsMeaning
exp(x)1exponential
log(x)1natural logarithm
sqrt(x)1square root
logistic(x)1logistic function 1/(1+exp(−x))
pow(x, y)2power x^y
min(x, y)2smaller of two values
max(x, y)2larger of two values
abs(x)1absolute value

cases switches the expression by the value of a Categorical column or a factor column. Write the referenced column name and an expression per level, as in cases(group){A: 0, B: 1.5, default: 0}. Either list every level or include a default entry. A level name can be written as is when it starts with a letter or _ followed by letters, digits, and _. Other level names are quoted, as in cases(group){"Group A": 0, default: 1}. A level named default is also quoted, as "default". Only an unquoted default is the default entry.

The return value of cases is the per-level value itself. Added to a base expression, as in 10 + cases(group){control: 0, treat: 1.5}, it expresses a per-group shift.

cases also maps levels to quantitative values. Writing cases(dose){low: 10, mid: 20, high: 40} in the Value of a Deterministic column produces a column holding the dose as a number.

lag(column, k) returns the value k rows earlier in the same series. See Factors and Row Structure for the definition of a series. lag is available only when a Time column is declared. The offset k is an integer of at least 1 and less than the Length of the Time column. Expressions can reference multiple past values, as in 0.3 + 0.5*lag(y, 1) + 0.2*lag(y, 2). The constant 0.3 in this expression is not the mean of the series. This autoregression is stationary, and its mean is 0.3/(1 − 0.5 − 0.2) = 1. In the ARIMA tab with d + D = 0, the Intercept of Include Intercept (Mean) represents the series mean, so it corresponds to 1 for this series.

A lag that points before the start of the series returns 0. As a result, the first few rows of an autoregressive series follow a different distribution from the later rows. For an AR(1) with constant c and coefficient φ (|φ| < 1), the mean at time t, counting the start as 1, is 1 − φ^t times the stationary mean, and the variance is 1 − φ^(2t) times the stationary variance. To avoid the influence of the start, make Length long enough that φ^t becomes small, and drop the first time points. For higher-order autoregressions, how fast the influence of the start fades depends on the set of coefficients.

Expression Examples

Commonly used expressions are as follows.

PurposeWhere to writeExpression
Simple regressionMean of y1 + 2*x
InteractionMean of y1 + 0.5*x1 + 0.3*x2 + 0.2*x1*x2
Poisson regressionLambda of Poissonexp(0.5 + 0.3*x)
Per-group intercept shiftMean of y10 + cases(group){control: 0, treat: 1.5}
Spread varying with xSD of y0.5 + 0.2*abs(x)
Clamping to a rangeValue of Deterministicmin(max(z, -2), 2)

Factors and Row Structure

Declaring Factors determines the row count from the factors. The row count is the product of all factor level counts, multiplied by Rows per cell and by the Length of the Time column.

A factor created with Add Factor produces one column whose values are the declared levels. The column type follows the same rule as for Categorical columns. Write the column name in Name and the levels in Levels, one per field. Declaring multiple factors lays out every combination of levels as rows. Rows per cell sets the number of rows per combination. Because every combination of levels receives the same number of rows, this produces balanced data.

Without a Time column, when Rows per cell is 1 and a column's By includes all the declared factors, each group of that column has a single row. This arrangement cannot identify the group variance separately from the residual variance. To estimate a group variance, set Rows per cell to 2 or more, or declare a Time column so that a group has multiple rows. A column whose By also selects the time column still has single-row groups even with a Time column.

Writing a name in Time column creates a column holding the integers 1 through Length. Each row determined by the level combinations and Rows per cell expands into a run of rows from time 1 through Length. This run is called a series. Rows are laid out in time order within each series, and lag looks back within the same series. The time column is numeric, so expressions can include a term using the time, such as 0.1*t.

The factor columns and the time column also become columns of the output dataset.

When a Time column is used without declaring factors, setting Rows per cell to 2 or more produces data with Rows per cell observations at each time point. Combined with a column whose By selects the time column, this builds data in which observations at the same time point share a common value. This arrangement produces Rows per cell series, and lag looks back independently within each series. No column indicates which row belongs to which series, so when a column distinguishing the groups of observations is needed, declare a factor.

By and Per-Group Values

A column with By draws one value per group and writes the same value into every row of that group. Use it for values determined per group, such as per-group intercept shifts.

A group is determined by the combination of values of the columns selected in By. By lists the names of factors, the Time column, and Categorical columns as checkboxes. With a factor, the number of groups is as declared and the group sizes are equal. With a Categorical column, the number of rows in each group depends on the sampling outcome and is not constant. Depending on the row count, some levels may not appear at all.

A column whose By selects only some of the factors shares its values across the factors not selected. For example, with the two factors school and class, a column with only class selected in By shares the value of each class across all schools. For nested groups that draw a separate value per school, select both school and class.

Even with nested groups, no column identifies the pair of school and class. When class level names repeat across schools, specifying only class as the GLMM group puts classes with the same name from different schools into one group. When you need a column that identifies the pair, create a string column that concatenates the values of school and class with Data > Add Columns....

The expression of a column with By can reference only the columns selected in By and columns whose By is a subset of this column's By. lag is also unavailable in a column with By.

Selecting the Time column in By produces a column that draws one value per time point. The value of each time point is shared across all series.

Whether the per-group values made with By are treated as fixed effects or random effects is decided by the model you fit. See GLMM Fundamentals for the reasoning.

Category Order

The categories of Categorical columns and factor columns are ordered as written in Levels. This is because MIDAS makes these columns enum columns and assigns them an enum definition whose values follow the order of Levels. Graph axes and legends, Crosstab, and Dummy Coding order the categories in this order. A column with more than 50 levels exceeds the limit on the number of values in an enum definition, so MIDAS makes it a string column. MIDAS also makes a column a string column when one of its levels has leading or trailing spaces. The columns of a synthetic dataset saved with Save data with project enabled in an earlier version of MIDAS also remain string columns, and they become enum columns when the dataset is edited and saved. The categories of a string column are not ordered as written in Levels.

The enum definitions are added to the Manage Enums tab when the dataset is saved. If a definition with the same values in the same order as Levels already exists, MIDAS uses it. Otherwise, MIDAS creates a definition named after the column, and when a definition with that name already exists, MIDAS appends a sequence number such as _2 to the name. Whether two names are the same is decided without distinguishing uppercase and lowercase letters. When MIDAS uses an existing definition, the definition is shared with the columns of other datasets. Changing the order of the values of a shared definition in the Manage Enums tab changes the order of the categories and the default reference category of Dummy Coding in every column that references the definition.

When you edit a saved synthetic dataset, MIDAS keeps using the definition from before the edit for each column whose Levels have the same values in the same order as before the edit, provided that the set of values of that definition equals the Levels. Therefore, an order set in the Manage Enums tab remains after an edit that does not change Levels. For a column whose Levels were reordered, and for a column whose levels were added or removed, MIDAS chooses the definition again by the rules in the previous paragraph. When Replace overwrites a synthetic dataset with the same name, MIDAS chooses the definitions by the same rules, treating the replaced dataset as the dataset before the edit.

The default reference category of Dummy Coding is the first category in the category order among the categories that appear in the data. For example, when the Levels of the factor arm are written in the order placebo, drug, the default reference category is placebo, and Dummy Coding creates a dummy variable for drug. To use the control group as the reference category, write the control group first in Levels. In a Categorical column made an enum column, depending on the weights and the number of rows, the first level may not be generated in any row; in that case, the default is the level that comes earliest in Levels among the levels that appear in the data. In a factor column made an enum column, every level appears in the same number of rows, so the default is always the first level in Levels. In a column made a string column, the order of Levels does not set the default.

The per-level values written in cases correspond to the true values of the dummy-variable coefficients through differences between level values. When the Mean of y is 10 + cases(arm){placebo: 0, drug: 1.5} + 0.6*x and y is regressed on the dummy variable of arm and on x, the true value of the coefficient of the drug dummy variable is 1.5 − 0 = 1.5. In general, the true value of the coefficient of the dummy variable for level L is the value cases returns for L minus the value it returns for the reference category. For a level handled by the default entry, use the value of the default expression. This correspondence holds when the terms and the link function of the fitted model match the data generating process. When cases is added to a linear predictor column that is passed through logistic to the P of a Bernoulli column, the true coefficient equals the difference of the level values in a logistic regression with the same link function, as in A Binary Response.

Presets

Choosing a Preset replaces the factors, columns, Rows per cell, Time column, and Length with the preset's contents as a starting point for editing. Dataset name and Seed remain as they are. Choosing a Preset again discards the edits to these and the preview made so far.

Multilevel (random intercept) is 40 schools × 10 students, 400 rows. It draws a per-school intercept u with SD 0.8 and, with a per-student predictor x, builds y = 10 + 0.6x + u plus residual noise with SD 1. The intraclass correlation coefficient given x is 0.64/(0.64+1) ≈ 0.39.

Repeated measures (subject × time) is 30 subjects × 8 time points, 240 rows. It draws a per-subject intercept u with SD 1 and builds y = 20 + 0.5t + u plus measurement noise with SD 1 drawn independently at each time point. The within-subject correlation between time points is 0.5, with no serial correlation.

Correlated random intercept and slope is 40 groups × 10 rows, 400 rows. It draws a per-group intercept u0 (SD 0.9) and slope u1 (SD 0.4) with correlation 0.5 and builds y = 2 + u0 + (0.7 + u1)x plus residual noise with SD 1. The two correlated effects are composed as linear combinations of independent standard normals z0 and z1.

The GLMM in MIDAS fits a random intercept only. Fitting a random-intercept model to the data from Correlated random intercept and slope therefore mismatches the model structure with the generating process. Given the group effects u0 and u1, the variance of y is 1 and constant. Averaged over the group effects, the covariance of y between rows i and j in the same group is 0.81 + 0.18(x_i + x_j) + 0.16 x_i x_j, and the variance of row i is 1.81 + 0.36 x_i + 0.16 x_i². A random-intercept model treats the covariance between rows in the same group as a constant that does not depend on x, and does not represent the group-to-group variation of the slope. The standard error that this model reports for the fixed-effect slope therefore tends to be smaller than the standard deviation of the sampling distribution of the slope estimator. See the GLMM tab for the fitting steps.

Configuration Examples

A Binary Response

Put the linear predictor in a Deterministic column and pass it through logistic for the P of Bernoulli. The following settings produce data following a logistic regression with intercept −1 and coefficient 0.8.

ColumnDistributionParametersBy
xNormalMean: 0, SD: 1
etaDeterministicValue: -1 + 0.8*x
yBernoulliP: logistic(eta)

A Regression with Group-Specific Intercepts

Create groups with a factor and add a per-group By column to the mean. The following settings produce regression data with a per-group intercept shift u, under a factor group with the five levels A, B, C, D, and E and Rows per cell 40.

ColumnDistributionParametersBy
uNormalMean: 0, SD: 0.8group
xNormalMean: 0, SD: 1
yNormalMean: 10 + 0.6*x + u, SD: 1

A Time Series with Autoregression

Declare a Time column and put lag(y, 1) in the Mean of y. The following settings produce a series with autoregressive coefficient 0.7, with Time column t and Length 200.

ColumnDistributionParametersBy
yNormalMean: 0.7 * lag(y, 1), SD: 1

Because lag returns 0 in the first row, the variance in the first few time points of the series is smaller than in the later time points. With these settings, the variance at time t, counting the start as 1, is 1 − 0.7^(2t) times the stationary variance 1/(1 − 0.7²), and at t = 10 it differs from the stationary variance by about 0.08% in relative terms. These settings generate a single series in a single column, so it can be passed to the ARIMA tab as is.

A Panel across Individuals

Declare individuals with a factor and add a per-individual By column to the mean. The following settings produce individual × time panel data, under a factor id whose Levels list 20 individual identifiers P01 through P20 and a Time column t with Length 10.

ColumnDistributionParametersBy
uNormalMean: 0, SD: 1id
yNormalMean: 5 + 0.3*t + u, SD: 1

Survival Times with Censoring

Draw an event time and a censoring time separately, then build the observed-time column with min and the event-variable column with a comparison. The following settings produce survival data in which a predictor x affects the event time through a Scale of exp(0.5 + 0.3*x).

ColumnDistributionParametersBy
xNormalMean: 0, SD: 1
T_eventWeibullShape: 1.5, Scale: exp(0.5 + 0.3*x)
T_censUniformMin: 0.5, Max: 3
timeDeterministicValue: min(T_event, T_cens)
eventDeterministicValue: T_event <= T_cens

Because the top level of its expression is a comparison, event becomes a 0/1 int64 column and can be used directly as the event variable in the Kaplan-Meier tab. Use a distribution that takes no negative values for the censoring time.

For a Weibull with Scale λ and Shape k, the hazard is h(t) = (k/λ)(t/λ)^(k−1), and here λ = exp(0.5 + 0.3x). Because Shape does not depend on x, the hazards are proportional. The log hazard ratio for a 1-unit increase in x is −k × 0.3 = −0.45. The true value of the x coefficient that the Cox regression tab estimates is −0.45, not the 0.3 placed in Scale. If Shape is made an expression in x, the hazards are not proportional, and the Cox regression coefficient has no single true value. This true value assumes that censoring is independent of the event time. T_cens in these settings is a Uniform that references neither T_event nor x, so the assumption holds.

Editing a Saved Dataset

The definition of a saved synthetic dataset can be reopened through the dataset's Edit Operation. See the Project Overview tab and the Project Lineage tab for how to open it.

In edit mode the dataset name cannot be changed, and Update Dataset overwrites the same dataset. Columns whose names are unchanged keep their column IDs across the overwrite. Models, derived datasets, and reports that use this dataset therefore keep their column references.

If the transformation is changed from another path such as another edit tab or the Agent API between the time this tab opens and the time you click Update Dataset, MIDAS stops the save and shows a dialog. Choose Overwrite with This Tab's Edits to save the edits in this tab over the version changed elsewhere. Choose Keep Editing to return to the edit tab without saving; the edits stay in place, and saving again shows the same dialog.

By default, a saved synthetic dataset holds the generation definition and the Seed rather than the data itself, and MIDAS regenerates the data from the definition every time the project opens. The regenerated values depend on the implementations of the random number generator and the math functions, so opening the project in a different browser, or updating MIDAS or the browser, can change the values even with the same definition and Seed. To keep the exact values, turn on Save data with project in the menu of the Data Table tab or the Project Overview tab. With it on, MIDAS includes the already evaluated data in the MDS file at save time and loads that data when the project opens. See Saving the Row Data of Derived Datasets for details. Exporting to CSV and importing it back also keeps the values. See Exporting Data for the export steps.

Appendix: Expression Grammar and Semantics

Grammar

The grammar in EBNF is as follows.

expression = additive [ comparison-op additive ] ;
comparison-op = "<" | "<=" | ">" | ">=" ;
additive   = term { ("+" | "-") term } ;
term       = primary { ("*" | "/") primary } ;
primary    = number | column-ref | function-call | cases-expr | lag-expr
           | "(" expression ")" | ("-" | "+") primary ;
function-call = function-name "(" expression { "," expression } ")" ;
function-name = "exp" | "log" | "sqrt" | "logistic"
              | "pow" | "min" | "max" | "abs" ;
cases-expr = "cases" "(" identifier ")" "{" branch { "," branch } [ "," ] "}" ;
branch     = label ":" expression ;
label      = identifier | string | number | "default" ;
lag-expr   = "lag" "(" identifier "," number ")" ;
column-ref = identifier ;
identifier = (letter | "_") { letter | digit | "_" }
           | "`" { character other than "`" | "``" } "`" ;
number     = ( digits [ "." { digit } ] | "." digits ) [ exponent ] ;
exponent   = ("e" | "E") [ "+" | "-" ] digits ;
string     = '"' { character other than '"' | '""' } '"'
           | "'" { character other than "'" | "''" } "'" ;

Whitespace between tokens (spaces, tabs, and newlines) is ignored.

Two quote characters in a row stand for one quote character. Inside a string, "" and '' become one " and one ', and inside a backtick-quoted identifier, `` becomes one `.

Writing two comparisons in a row, as in a < b < c, is a parse error. Parentheses, function arguments, and cases branches are each independent expressions, so comparisons can be written inside them.

There is no negative number literal. -1 is read as the unary minus applied to the number 1.

Among labels, only an unquoted default is the default branch. The string "default" and the backtick-quoted `default` refer to a level named default.

cases makes a duplicate label and a second default a parse error. A numeric label is matched against the levels as written, so 01 matches level "01", and 1.0 does not match level "1".

cases and lag are treated as syntax only when followed by (. Otherwise they are ordinary column references, so columns named cases or lag can also be referenced. A backtick-quoted identifier is a column reference whether or not ( follows it.

Semantics

Expressions are evaluated per row, and the values are IEEE 754 double-precision floating-point numbers. A column reference reads the value in the same row. When used as a distribution parameter, the parameter expression is evaluated per row and then one value is sampled with it. For a column with By, evaluation and sampling happen once, at the first row of the group.

A comparison is 1 when true and 0 when false. When either of the compared values is NaN, it returns NaN.

Functions are double-precision mathematical functions. log and sqrt return NaN for negative arguments.

The value of cases is the value of the branch matching the row's level. When the level matches no branch and a default exists, it is the value of the default expression.

The value of lag is the value k rows earlier in the same series. Before the start of the series it is 0, and lag never reads across a series boundary into the previous series.

NaN and an infinity produced when an operation on finite values overflows (such as exp(800)) are passed on to the next operation within an expression. For example, 1/(1 + exp(800)) is 0. At the point where the value of an expression is used as a distribution parameter, MIDAS checks finiteness and fails the generation on a non-finite value.

Footnotes

  1. Some columns, such as a column with more than 50 levels, become string columns. See Category Order for details. ↩

  2. A Deterministic column becomes a 0/1 int64 column only when the top level of its expression is a comparison. The event-variable column in Survival Times with Censoring is an example. ↩

  3. To map a linear predictor into a domain, use logistic for the range 0 to 1 and exp for positive values. ↩