# Queries for a Pair of Tables¶

class larch.core.QuerySetTwoTable

This subclass of core.QuerySet is used when the data consists of one idco Format format table and one idca Format format table.

idco_query

This attribute defines a SQL query that evaluates to an larch_idco table. The table should have the following features:

• Column 1: caseid (integer) a key for every case observed in the sample
• Column 2+ can contain any explanatory data, typically numeric data, although non-numeric data is allowable.
idca_query

This attribute defines a SQL query that evaluates to an larch_idca table. The table should have the following features:

• Column 1: caseid (integer) a key for every case observed in the sample
• Column 2: altid (integer) a key for each alternative available in this case
• Column 3+ can contain any explanatory data, typically numeric data, although non-numeric data is allowable.
alts_query

This attribute defines a SQL query that evaluates to an larch_alternatives table. The table should have the following features:

• Column 1: id (integer) a key for every alternative observed in the sample
• Column 2: name (text) a name for each alternative
choice

This attribute defines the choices. It has two styles

• When set to a string, the string gives an expression evaluated on one of the two main tables that identifies the choice for each case. If the expression is evaluated on the idco table, it should result in integer values corresponding to the alternative codes. If the expression is evaluated on the idca table, it should evaluate to 1 if the alternative for the particular row was chosen, and 0 otherwise. (For certain specialized models, values other than 0 or 1 may be appropriate.) If the expression is part of a valid query on both main tables, this attribute cannot be set directly, and must instead be set using either set_choice_ca() or set_choice_co().
• When set to a dict, the dict should contain {integer:string} key/value pairs, where each key is an integer value corresponding to an alternative code, and each value is a string identifying an expression evaluated on the idco table; the result should contain a value indicating whether the alternative was chosen. Usually this will be a binary dummy variable, although it need not be. For certain specialized models, values other than 0 or 1 may be appropriate.

The choice of style is a matter of convenience; the same data can be expressed with either style as long as the choices are binary, or if the first style names an expression in the idca table.

set_choice_ca(expr)

Set the choice expression that will evaluate on the idca table.

Parameters: expr (str) – The expression to be evaluated. It should evaluate to 1 if the alternative for the particular row was chosen, and 0 otherwise. (For certain specialized models, values other than 0 or 1 may be appropriate.)
set_choice_co(expr)

Set the choice expression that will evaluate on the idco table.

Parameters: expr (str) – The expression to be evaluated. It should result in integer values corresponding to the alternative codes.
avail

This attributes defines the availability of alternatives for each case. If set to True, then all alternatives are available to all cases. Otherwise, this attribute should be either:

• a dict that contains {integer:string} key/value pairs, where each key is an integer value corresponding to an alternative code, and each value is a string identifying an expression evaluated on the idco table; that expression should evaluate to a value indicating whether the keyed alternative is available. This must be a binary dummy variable.
• a string identifying an expression evaluated on the idca table; that expression should evaluate to a value indicating whether the alternative for that row is available. This must be a binary dummy variable.
• If set to 1 or None, then the string “1” is used.
weight

This attribute gives an expression that is evaluated on the idco table to defines the weight for each case. Set it to an empty string, or 1.0, to assign all cases equal weight. Te weight cannot be set based on the idca table.