简介
fencode provides frequency-based encoding of string and numeric variables. Unlike encode which assigns codes alphabetically, fencode assigns numeric codes ordered by frequency (descending or ascending). The command accepts both string variables and labeled numeric variables with non-sequential codes, standardizing them to sequential order. This functionality is particularly valuable for creating tables and graphs where frequency ordering improves interpretability, and for regression analysis where the most frequent category serves as a more meaningful base category.
命名
fencode=f+encode=frequency-based encoding [1]
安装
. ssc install fencode
场景
• Survey data analysis: Response categories with arbitrary codes (0=Never, 1=Sometimes, 7=Always) can be standardized to sequential codes ordered by prevalence
• Publication-ready tables: Frequency tables automatically display the most important categories first without the need for manual sorting
• Panel data visualization: Entities (firms, countries, individuals) appear in graphs ordered by their frequency in the dataset, improving interpretability
• Likert scale presentation: Response distributions in graphs and tables follow logical frequency ordering rather than alphabetical ordering, particularly useful when using graphing commands that lack straightforward sorting options
示例
Basic usage demonstrating default behavior and options:
. webuse hbp2, clear . fencode sex // Creates sex_fencode with default name . fencode sex, gen(gender) // Same result with explicit name . fencode sex, gen(gender_asc) ascending // Ascending order
Working with city temperature data:
. sysuse citytemp, clear . fencode region . graph bar (count), over(region_fencode) title("Cities by Region (automatically ordered by frequency)")
Standardizing arbitrary category codes:
. sysuse voter, clear . fencode candidat // Converts codes 2,3,4 to clean 1,2,3
Using frequency ordering for regression base categories:
. webuse hbp2, clear . fencode age_grp . fencode sex . logit hbp i.age_grp_fencode i.sex_fencode // Most frequent age group as base
作者
Kabira Namit
World Bank
Email: knamit@worldbank.org
同类
Nick Cox的myaxis [2]命令也可以实现,且更为灵活。
. webuse hbp2, clear . fencode sex . myaxis sex_myaxis = sex, sort(count) descending . tab1 sex_fencode sex_myaxis