tisane.Unit¶
- class tisane.Unit(name, data=None, cardinality=None, **kwargs)¶
Bases:
tisane.variable.AbstractVariable
Class for Units
A data variable that can have attributes. For example, if you have people in your dataset, and each person has an eye color, height, age, etc., then you should make the person variable a Unit.
In statistics, a Unit can represent either an observational or experimental unit.
- Parameters
name (str) – The name of the variable. If you have data, this should correspond to the column’s name. The dataset must be in long format.
data (DataVector, optional) – For internal use only.
cardinality (int, optional) – The number of unique values of the variable. cardinality is optional only if you have a data set. If specified, Tisane will check that the cardinality is correct if you include data in the design. If left unspecified, and data is available, Tisane will try to calculate the cardinality.
**kwargs (optional) – Additional keyword arguments are not currently implemented.
- cardinality¶
The number of unique values of the variable.
- Type
int
Examples
>>> import tisane as ts >>> person = ts.Unit(name="person_id")
An example with cardinality: there were 40 unique groups.
>>> group = ts.Unit(name="group_id", cardinality=40)
Methods
associates_with
(variable)Adds a correlation relationship to a data variable.
causes
(effect)Adds a causes relationship to a data variable.
moderates
(moderator, on)Adds an interaction relationship to a data variable
nominal
(name[, data, cardinality, ...])Creates a categorical data variable that is an attribute of a
Unit
numeric
(name[, data, number_of_instances])Creates a variable that takes on integer or real number values
ordinal
(name, order[, cardinality, data, ...])Creates a categorical data variable whose categories are ordered.
add_data
get_cardinality
nests_within
- nominal(name, data=None, cardinality=None, number_of_instances=1, **kwargs)¶
Creates a categorical data variable that is an attribute of a
Unit
- Parameters
name (str) – the name of the variable. If you have data, this should correspond to the column’s name.
data (DataVector, optional) – For internal use only.
cardinality (int, optional) – The number of unique values that the categorical variable can take. This should correspond to the number of “categories.” If left unspecified, Tisane will infer this from the data.
number_of_instances (int, AbstractVariable, or tisane.AtMost, default=1) – This should be the number of measurements of an attribute per unique instance of the associated
Unit
. For example, if you asked how someone felt 5 different times, the**kwargs (optional) – You can optionally specify the categories using the keyword argument
categories
. This should be a list. If left unspecified, Tisane will infer this from the data.
- Returns
The categorical data variable defined as an attribute of this
Unit
- Return type
Examples
A study asked participants how they felt 5 separate times.
>>> import tisane as ts >>> participant = ts.Unit(name="participant") >>> feelings = participant.nominal(name="feeling", ... categories=["sad", "happy", "angry", "excited"], # optional, specified here as an example ... number_of_instances=5)
The study also collected participants’ eye color.
>>> eye_color = participant.nominal(name="eye_color", categories=["blue", "brown", "green", "hazel", "gray", "black"])
- numeric(name, data=None, number_of_instances=1)¶
Creates a variable that takes on integer or real number values
- Parameters
name (str) – The name of the variable. If you have data, this should correspond to the column’s name.
data (DataVector, optional) – For internal use only.
number_of_instances (int, AbstractVariable, or tisane.AtMost, default=1) – This should be the number of measurements of an attribute per unique instance of the associated Unit. For example, if you measure the reaction time of each person in a study 10 times, then you should enter 10.
Examples
Participants in a study each had their reaction time measured 10 times.
>>> import tisane as ts >>> participant = ts.Unit(name="participant") >>> reaction_time = participant.numeric(name="reaction_time", ... number_of_instances=10)
- ordinal(name, order, cardinality=None, data=None, number_of_instances=1)¶
Creates a categorical data variable whose categories are ordered.
- Parameters
name (str) – the name of the variable. If you have data, this should correspond to the column’s name.
order (list) – a list of the categories, in the order desired
cardinality (int, optional) – The number of unique values that the variable can take. If left unspecified, Tisane will automatically infer this from the data, if any, or the order argument. cardinality is required if using Tisane without providing data.
data (DataVector, optional) – For internal use only.
number_of_instances (int, AbstractVariable, or AtMost, default=1) – This should be the number of measurements of an attribute per unique instance of the associated tisane.Unit. For example, if you measure the reaction time of a person 10 times, then you should enter 10.
- Returns
The categorical data variable with ordered categories
- Return type
Examples
Representing age ranges: <18, 18-30, 31-45, 46-64, 65+
>>> person = ts.Unit("person") >>> ageRange = person.ordinal(name="age", order=["<18", "18-30", "31-45", "46-64", "65+"])
Representing 3 different treatments of differing amounts of vitamin E:
>>> pig = ts.Unit("Pig", cardinality=72) # 72 pigs >>> vitamin_e = pig.ordinal("Evit", ... order=["Evit000", "Evit100", "Evit200"], ... number_of_instances=1)
Suppose you have 100 people, and you measure using a Likert scale how well they’re feeling 10 times, for each person.
>>> person = ts.Unit("person", cardinality=100) # 100 people >>> feeling = person.ordinal("well", ... order=[1, 2, 3, 4, 5], ... number_of_instances=10)