【问题标题】:Recoding Categorical Variable (Stata)重新编码分类变量(Stata)
【发布时间】:2021-02-09 20:28:28
【问题描述】:

我正在尝试将income_change 分类变量从 5 组更改为 3 组。

当前变量看起来:

tab income_change                  frequency
Decreased by more than 25% |        333
        Decreased by 1-25% |        331
           Stayed the same |        222
        Increased by 1-25% |         23
Increased by more than 25% |         12

并且变量存储为:

         storage   display    value
variable name   type    format     label      variable label
--------------------------------------------------------------------------------------------------------------------------
income_change            int     %26.0g      Lchg

为了根据上面的五个类别创建三个组,我运行了这个,但是我得到了这个错误消息“类型不匹配”

gen perc_change = income_change            
recode   perc_change ="Income Decreased"  if perc_change =="1"  | if perc_change =="2"
recode   perc_change ="Same Income"  if perc_change =="3"
recode   perc_change ="Income Increased"  if perc_change =="4" | if perc_change =="5"

perc_change 变量存储方式如下:


              storage   display    value
variable name   type    format     label      
--------------------------------------------------------------------------------------------------------------------------
perc_change     float   %9.0g 

已通过以下建议的解决方案解决:

gen inc_change = income_change 
gen inc_perc_change = ""
replace inc_perc_change ="Income Decreased"  if inc_change == 1 | inc_change == 2
replace inc_perc_change ="Same Income"       if inc_change_perc == 3
replace inc_perc_change ="Income Increased"  if inc_change_perc == 4 | inc_change_perc == 5
tab inc_perc_change 

生成了我正在寻找的图表:

catplot  tn_cor22_str inc_perc_change, percent(tn_cor22_str)

【问题讨论】:

    标签: stata


    【解决方案1】:

    income_change 似乎是一个带有文本标签的数值变量。你可以试试这样的:

    gen perc_change = ""
    replace perc_change ="Income Decreased"  if income_change == 1 | income_change == 2
    replace perc_change ="Same Income"       if income_change == 3
    replace perc_change ="Income Increased"  if income_change == 4 | income_change == 5
    tab perc_change 
    

    如果上面的代码不起作用,很可能income_change 的值不是1到5,你需要把1-5改成你数据中income_change 的相关值来设置正确的条件。

    【讨论】:

    • 考虑也使用-inlist()-。
    • 是的。抱歉,我复制了您的代码并忘记更改它。
    【解决方案2】:

    或者,您可以使用:

    gen perc_change = ""
    replace perc_change ="Income Decreased"  if inrange(perc_change, 1, 2)
    replace perc_change ="Same Income"       if perc_change == 3
    replace perc_change ="Income Increased"  if inrange(perc_change, 4, 5)
    

    【讨论】:

      【解决方案3】:

      虽然你得到了你想要的,但结果变量是不完美的,因为(特别是)它甚至不会按照你想要的方式排序。另一种可能性是带有新值标签的粗化数值变量,正如 say 所做的那样

      gen change_class = 1 if inlist(perc_change, "1", "2") 
      replace change_class = 2 if perc_change == "3" 
      replace change_class = 3 if inlist(perc_change, "4", "5") 
      label def change_class 1 Decreased 2 Same 3 Increased 
      label val change_class change_class 
      

      【讨论】:

      • 谢谢,图表确实有效,请检查编辑后的代码。
      猜你喜欢
      • 2017-03-03
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      • 2017-11-03
      • 1970-01-01
      • 1970-01-01
      • 2022-11-14
      • 1970-01-01
      相关资源
      最近更新 更多