Skip to main content

US Nested Genpop

Updated over a week ago

You can insert this screener into your survey using

s.standard_screener("US", "NestedGenPop")

Or if you want to edit it, just copy and paste the code below:


age = s.numeric_question(
question="How old are you?",
min_max=(18, 65),
recodes={
"18-24": "18-24",
"25-34": "25-34",
"35-44": "35-44",
"45-54": "45-54",
"55-64": "55-64",
"65+": "65+",
},
)

gender = s.select_question("What is your gender?", ["Male", "Female", "Non-Binary", "Prefer not to say"])

ethnicity = s.select_question(
"What is your ethnicity?",
[
"White",
"Black",
"Other",
],
)

hispanic = s.select_question("Would you describe yourself as Hispanic?", ["Yes", "No"])

income = s.numeric_question(
"What is your annual income?",
min_max=(0, 500000),
recodes={
"0-20000": "Less than $20,000",
"20000-34999": "$20,000 to $34,999",
"35000-49999": "$35,000 to $49,999",
"50000-74999": "$50,000 to $74,999",
"75000-99999": "$75,000 to $99,999",
"100000-149999": "$100,000 to $149,999",
"150000+": "$150,000 or more",
},
)

s.select_question(
"What is your highest level of education?",
[
"Less than high school degree",
"High school graduate",
"Some college",
"Bachelor's degree",
"Master's degree",
"Post-graduate degree",
],
)

region = s.select_question(
"Which region of the country do you come from?", options=["Northeast", "Midwest", "South", "West"]
)

# Age quotas
s.set_quota(
name="Age",
quotas=[
s.quota("18-24", criteria=(18 <= age <= 24), quota=0.12),
s.quota("25-34", criteria=(25 <= age <= 34), quota=0.18),
s.quota("35-44", criteria=(35 <= age <= 44), quota=0.17),
s.quota("45-54", criteria=(45 <= age <= 54), quota=0.16),
s.quota("55-64", criteria=(55 <= age <= 64), quota=0.17),
s.quota("65-99", criteria=(65 <= age <= 99), quota=0.2),
],
)

# Ethnicity x Hispanic quotas
s.set_quota(
name="Ethnicity_Hispanic",
quotas=[
s.quota("White_Hispanic_Yes", criteria=(ethnicity == "White") & (hispanic == "Yes"), quota=0.18),
s.quota("White_Hispanic_No", criteria=(ethnicity == "White") & (hispanic == "No"), quota=0.58),
s.quota("Black_Hispanic_Yes", criteria=(ethnicity == "Black") & (hispanic == "Yes"), quota=0.01),
s.quota("Black_Hispanic_No", criteria=(ethnicity == "Black") & (hispanic == "No"), quota=0.13),
s.quota("Other_Hispanic_No", criteria=(ethnicity == "Other") & (hispanic == "No"), quota=0.10),
],
)

# Hispanic quotas (flat)
s.set_quota(
name="Hispanic",
quotas=[
s.quota("Yes", criteria=(hispanic == "Yes"), quota=0.19),
s.quota("No", criteria=(hispanic == "No"), quota=0.81),
],
)

# Gender x Age quotas

s.set_quota(
name="Age and Gender",
quotas=[
s.quota(
f"{gender_val}_{min_age}-{max_age}",
criteria=(gender == gender_val) & (age >= min_age) & (age <= max_age),
quota=quota_val,
)
for gender_val, age_quotas in {
"Male": {
(18, 24): 0.06,
(25, 34): 0.09,
(35, 44): 0.085,
(45, 54): 0.08,
(55, 64): 0.085,
(65, 99): 0.09,
},
"Female": {
(18, 24): 0.06,
(25, 34): 0.09,
(35, 44): 0.085,
(45, 54): 0.08,
(55, 64): 0.085,
(65, 99): 0.11,
},
}.items()
for (min_age, max_age), quota_val in age_quotas.items()
],
)

# Income x Gender quotas
s.set_quota(
name="Income_Gender",
quotas=[
s.quota(
f"{income_range}_{gender_val}",
criteria=gender_quotas["criteria"] and (gender == gender_val), # type: ignore
quota=gender_quotas[gender_val],
)
for gender_val in ["Male", "Female"]
for income_range, gender_quotas in {
"0-24999": {"Male": 0.08, "Female": 0.09, "criteria": (income < 25000)},
"25000-49999": {"Male": 0.09, "Female": 0.10, "criteria": (income >= 25000) & (income < 50000)},
"50000-99999": {"Male": 0.14, "Female": 0.14, "criteria": (income >= 50000) & (income < 100000)},
"100000-149999": {"Male": 0.08, "Female": 0.08, "criteria": (income >= 100000) & (income < 150000)},
"150000-500000": {"Male": 0.10, "Female": 0.10, "criteria": (income >= 150000)},
}.items()
],
)

# Region quotas
s.set_quota(
name="Region",
quotas=[
s.quota("Northeast", criteria=(region == "Northeast"), quota=0.18),
s.quota("Midwest", criteria=(region == "Midwest"), quota=0.23),
s.quota("South", criteria=(region == "South"), quota=0.37),
s.quota("West", criteria=(region == "West"), quota=0.22),
],
)
Did this answer your question?