import ezrsurvey as ezGetting started with ezrsurvey
ezrsurvey turns the things consumer-survey analysts do over and over into single-line helpers. This article walks through a typical analysis of the bundled podracing_survey dataset: from raw answers to percentages, models, diagnostics and a chart you could drop on a slide.
The data
podracing_survey is 1,000 simulated respondents with demographics, a 0 to 10 NPS question, worded feature ratings, multi-select motivations, brand questions and open-text comments.
ez.podracing_survey[["respondent_id", "demo_gender", "demo_age", "nps_value"]]| respondent_id | demo_gender | demo_age | nps_value | |
|---|---|---|---|---|
| 0 | R00001 | Male | 35 | 10 |
| 1 | R00002 | Male | 29 | 6 |
| 2 | R00003 | Male | 16 | 10 |
| 3 | R00004 | Male | 16 | 10 |
| 4 | R00005 | Male | 16 | 9 |
| ... | ... | ... | ... | ... |
| 995 | R00996 | Female | 16 | 7 |
| 996 | R00997 | Male | 31 | 7 |
| 997 | R00998 | Female | 27 | 10 |
| 998 | R00999 | Male | 30 | 7 |
| 999 | R01000 | Female | 22 | 7 |
1000 rows × 4 columns
Percentages without the groupby, count and pivot dance
calc_percentage() is the workhorse: counts, percentages and ordering in one call. Columns are named as strings.
ez.calc_percentage(ez.podracing_survey, "demo_gender", sort="desc")| demo_gender | n | pct | |
|---|---|---|---|
| 0 | Male | 525 | 55.0 |
| 1 | Female | 399 | 42.0 |
| 2 | Non-binary | 27 | 3.0 |
Group with by, and pivot to a wide cross-tab with wide=True:
ez.calc_percentage(ez.podracing_survey, "satis_return", by="region", wide=True)| region | Likely | Not sure | Unlikely | Very likely | Very unlikely | |
|---|---|---|---|---|---|---|
| 0 | Asia | 24.0 | 25.0 | 12.0 | 34.0 | 5.0 |
| 1 | Europe | 28.0 | 25.0 | 16.0 | 25.0 | 6.0 |
| 2 | Latin America | 30.0 | 21.0 | 11.0 | 28.0 | 10.0 |
| 3 | North America | 26.0 | 24.0 | 17.0 | 24.0 | 8.0 |
| 4 | Oceania | 31.0 | 12.0 | 24.0 | 24.0 | 10.0 |
Check-all-that-apply questions live in a block of columns sharing a prefix; calc_percentage_multi() handles the per-respondent denominator for you:
ez.calc_percentage_multi(ez.podracing_survey, "motivations_", id="respondent_id", sort="desc")| option | n | pct | |
|---|---|---|---|
| 0 | speed | 772 | 79.0 |
| 1 | drivers | 533 | 54.0 |
| 2 | social | 437 | 45.0 |
| 3 | tradition | 349 | 36.0 |
| 4 | betting | 295 | 30.0 |
Spreadsheet exports, Google Forms among them, instead pack every answer a respondent ticked into a single cell. The same function recognises that shape and separates the answers, so you do not have to reshape anything first:
import pandas as pd
packed = pd.DataFrame({
"respondent": [1, 2, 3, 4],
"motivations": ["Speed; Drivers", "Speed", "", "Betting; Speed"],
})
ez.calc_percentage_multi(packed, "motivations", id="respondent", sort="desc")| option | n | pct | |
|---|---|---|---|
| 0 | Speed | 3 | 100.0 |
| 1 | Betting | 1 | 33.0 |
| 2 | Drivers | 1 | 33.0 |
Note the denominator: three respondents ticked something, so “Speed” is 3 of 3. When you need those answers as columns rather than as a table, for a crosstab or a filter, split_multi() widens them:
ez.split_multi(packed, "motivations")| respondent | motivations | motivations_Speed | motivations_Betting | motivations_Drivers | |
|---|---|---|---|---|---|
| 0 | 1 | Speed; Drivers | Speed | Drivers | |
| 1 | 2 | Speed | Speed | ||
| 2 | 3 | ||||
| 3 | 4 | Betting; Speed | Speed | Betting |
Reusable level orders
Rather than retyping levels=[...], register an order once and link it to a variable; calc_percentage() then applies it automatically.
ez.register_order(
"education",
levels=[
"Primary or less",
"Lower secondary",
"Upper secondary",
"Short-cycle tertiary",
"Bachelor or equivalent",
"Master or equivalent",
"Doctoral or equivalent",
],
vars="demo_edu",
)
ez.calc_percentage(ez.podracing_survey, "demo_edu")| demo_edu | n | pct | |
|---|---|---|---|
| 0 | Primary or less | 42 | 4.0 |
| 1 | Lower secondary | 111 | 11.0 |
| 2 | Upper secondary | 295 | 30.0 |
| 3 | Short-cycle tertiary | 123 | 13.0 |
| 4 | Bachelor or equivalent | 244 | 25.0 |
| 5 | Master or equivalent | 122 | 13.0 |
| 6 | Doctoral or equivalent | 35 | 4.0 |
Net Promoter Score
ez.calc_nps(ez.podracing_survey, "nps_value")| n | nps | pct_detractors | pct_passives | pct_promoters | detractors | passives | promoters | |
|---|---|---|---|---|---|---|---|---|
| 0 | 1000 | 7.0 | 25.0 | 43.0 | 32.0 | 252 | 426 | 322 |
ez.plot_nps(ez.podracing_survey, "nps_value")
Importance / performance
ipm_model() combines driver importance (by default, relative weights against the NPS score) with feature performance; plot_ipm() draws the matrix. Pass method="forest" for a random-forest cross-check, which sees non-linear effects that relative weights cannot. The forest needs the drivers extra (pip install "ezrsurvey[drivers]").
model = ez.ipm_model(ez.podracing_survey, "nps_value", "ratings_")
model| feature | importance | performance | perf_class | |
|---|---|---|---|---|
| 0 | value | 24.858365 | 2.856 | 2 |
| 1 | atmosphere | 23.831093 | 3.571 | 3 |
| 2 | speed | 17.999829 | 3.480 | 3 |
| 3 | safety | 14.091294 | 2.876 | 2 |
| 4 | venue | 12.229257 | 3.079 | 3 |
| 5 | commentary | 6.990161 | 2.578 | 2 |
Comparing two events
For event surveys you often compare this wave with the last. compare_values() computes the differences and plot_diff() draws the change chart.
previous = model.copy()
previous["performance"] = model["performance"] - [0.3, -0.1, 0.2, 0, 0.1, 0.3]
ez.plot_diff(ez.compare_values(model, previous))
How precise is it?
diagnose() reports the standard error, relative standard error and margin of error for any column: numeric (a point estimate) or categorical (a proportion).
ez.diagnose(ez.podracing_survey, "demo_gender", "nps_value")| variable | type | unit | n | estimate | se | rse | moe | precision | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | demo_gender | prop | ppt | 951 | 55.21 | 1.61 | 2.92 | 3.16 | high precision |
| 1 | nps_value | mean | points | 1000 | 7.56 | 0.06 | 0.74 | 0.11 | high precision |
For a one-shot, plain-language read on the whole study, precision_summary() picks the point-scale and proportion variables for you and writes the bullet points you would otherwise hand-craft for a report appendix:
ez.precision_summary(ez.podracing_survey)Survey precision summary
- Based on 1,000 total responses (per-question base of 776 to 1,000).
- Point ratings carry a sampling margin of about +/-0.40 points (up to +/-0.69) at 95% confidence.
- Percentages carry a sampling margin of about +/-3.0 percentage points (worst case +/-3.1 at a 50/50 split).
- Overall relative standard error of about 4.2% -- high precision.
Why relative standard error?
A standard error on its own is hard to judge: is plus or minus 0.05 good? It depends on the scale. The relative standard error (RSE) divides the standard error by the estimate, so precision becomes comparable across a 1 to 5 rating and a percentage alike. Rating estimates by their RSE is the approach national statistical agencies use to decide whether a number is solid enough to publish. The Australian Bureau of Statistics, for example, treats an RSE of 25% or more as not reliable for most purposes, marks estimates between 25% and 50% with an asterisk as ones to use with caution, and suppresses anything above 50%. ezrsurvey keeps that 25% boundary and adds tighter bands below it, because in consumer-survey work the question is usually which of several adequately precise numbers to lead with:
| RSE | Rating |
|---|---|
| under 5% | high precision |
| under 10% | precise |
| under 15% | satisfactory |
| under 25% | use with caution |
| 25% or more | likely reliability issues |
For proportions, the margin of error is widest at a 50/50 split (sqrt(0.5 * 0.5 / n)), so a single worst-case percentage-point figure at your achieved sample size bounds every percentage in the study, which is why precision_summary() reports it.
Saving and exporting
Every table and chart has a one-line save. A bare file name lands in an ezrsurvey-outputs/ folder, created on demand, so a script’s results collect in one place instead of scattering through the working directory; pass a path with a directory in it ("./gender.xlsx", "tables/gender.xlsx", anything absolute) to put a file exactly where you want it. The first save of a session says which folder it used, so nothing goes missing. Excel files need the excel extra.
ez.save_data(ez.calc_percentage(ez.podracing_survey, "demo_gender"), "gender.xlsx")
ez.save_plot(ez.plot_nps(ez.podracing_survey, "nps_value"), "nps.png")
# several tables, one tab each
ez.export_xlsx(
gender=ez.calc_percentage(ez.podracing_survey, "demo_gender"),
job=ez.calc_percentage(ez.podracing_survey, "demo_job"),
path="summary.xlsx",
)Every helper also takes the data frame first, so a pandas method chain works with .pipe():
ez.podracing_survey.pipe(ez.calc_percentage, "demo_job", sort="desc")| demo_job | n | pct | |
|---|---|---|---|
| 0 | Employed full-time | 447 | 46.0 |
| 1 | Student | 218 | 23.0 |
| 2 | Employed part-time | 129 | 13.0 |
| 3 | Not in labour force | 113 | 12.0 |
| 4 | Unemployed | 59 | 6.0 |
Where to next
- Set persistent defaults with
ezrsurvey_options()and a profile (use_ezrsurvey_profile()). - Build a deck with
report_deck()or scaffold a Quarto report withscaffold_report(), which drops the.qmdbeside where its render will land.