【问题标题】:Replace values of a variable for values of other variables Stata 13将变量的值替换为其他变量的值 Stata 13
【发布时间】:2014-07-20 19:45:20
【问题描述】:

我正在与 Stata 进行第一场比赛。直到这周我才使用它,并试图通过一些例子来工作。我有以下一组数据:

contruse | educ_none | educ_prim | educ_secabove
1        | 0         | 1         | 0
0        | 1         | 0         | 0
...

我用相应的数据集创建了以下变量,以便我可以tab contruse 接受所有不同的教育。

gen education=0
replace education=1 if educ_none==1
replace education=2 if educ_prim==1
replace education=3 if educ_secabove==1
replace education=. if educ_none==. | educ_prim==. | educ_secabove==.
tab education, missing

contruse | educ_none | educ_prim | educ_secabove | education
1        | 0         | 1         | 0             | 2
0        | 1         | 0         | 0             | 1

基本上有更好的方法可以做到这一点:例如,我的 varlist 可能任意大,并且执行上述操作很痛苦。有没有办法说颠倒以下内容来处理多个变量并给单个变量一个值?

foreach x of varlist educ_none educ_prim educ_secabove {
    replace `x' = . if var > 3
}

【问题讨论】:

    标签: loops for-loop foreach stata


    【解决方案1】:

    你能自动化这个过程吗? 答案是“否”,因为每个组件变量都有一个唯一的后缀。因此,例如,如果您有“race_black”、“race_hisp_nonw”、“race_white”,则不能以相同的方式处理“education”和“race”变量。您还将有唯一的值标签来分配给每个变量。 请参阅下面的第二个答案

    另外两个问题:

    1. 阅读您的示例,似乎对于那里的教育 正好是三类。所以你正在初始化 一个不存在的类别。

    2. 您对缺失的处理可能不正确。 如果教育的任何组成部分,您已将教育设置为缺失 不见了。面试官可能将其中一个组成变量正确编码为“1”并离开 其他值应为“0”时为空白(缺失)。不应设置该观察的教育 错过了。

    这是我对代码的想法:

    set linesize 100
    clear
    input id educ_none educ_prim educ_secabove
    1 0 1 0
    2 1 0 0
    3 0 0 1
    4 . 1 .    /* Okay */
    5 . . .    /* Really Missing */
    6 0 0 0    /* Really Missing */
    7 . 1 1    /* Illegal */
    end
    
    egen etot = rowtotal(educ_*) /* = 1 for valid values */
    foreach x of varlist educ_* {
    /* Tentatively fix incorrect missings */
        replace `x'= 0 if `x'==. & etot==1
        }
    list
    gen   education = 1 if educ_none==1
    replace education=2 if educ_prim==1
    replace education=3 if educ_secabove==1
    
    
    /* Assign extended missing for illegal values*/
    replace education = .a if etot >1 & etot<.
    #delim ;
    label define educl
        1 "None"
        2 "Primary"
        3 "Secondary+"
        .a  ">1 indicator is 1"
     ;
    #delim cr
    label values education educl
    list
    tab education, missing
    

    【讨论】:

      【解决方案2】:

      除了Steve Samuels 的优秀建议之外,该领域的三个标准设备是

      A.使用recode。查看它的帮助。

      B.

      gen education = educ_none + 2 * educ_prim + 3 * educ_secabove
      

      (当且仅当至多一个指标为 1 时才有效)

      C.

      gen education = cond(educ_secabove == 1, 3, 
                      cond(educ_prim == 1, 2, 
                      cond(educ_none == 1, 1)))
      

      注意事项:

      C1。上面的代码是一个语句。布局只是为了帮助使结构可见。

      C2。就像在初等代数中一样,每个左括号 ( 都暗示着用右括号 ) 匹配它的承诺。对cond() 的嵌套调用不会改变这一点。

      C3。在http://www.stata-journal.com/sjpdf.html?articlenum=pr0016cond() 上还有更多信息

      【讨论】:

        【解决方案3】:

        自动化方法 2014-06-02

        在说明创建和标记新变量的过程无法自动化之后,我决定尝试一下。我在 SSC 上找到了两个有用的命令:Roger Newson 的 varlabdef 和 Daniel Klein 的 labvalch3。两者都可以从 Stata 中下载,例如ssc install varlabdef

        我假设,就像在原始示例中一样,每个 0-1 变量名称的形式为“root_suffix”,并且具有相同根的变量中恰好有一个值为 1。目标是创建一个新变量对于每个根,其值对应于值为 1 的指示变量(如果有)的顺序。用户首先创建一个包含 所有根的本地宏。程序循环遍历根,每次通过创建一个变量;内部循环实现尼克的解决方案(B); varlabdef 根据原始指标的名称创建值标签; labvalch3 去掉除了后缀之外的所有内容,并将每个项目大写。然后使用label values 语句将此值标签分配给新变量。在循环之外,新变量被赋予带有label variable变量标签。

        在下面的示例中,有两个“根”,educgender。例如,根为“gender”的变量是gender_malegender_female。初始化一个新变量gender,然后为男性分配值 1,为女性分配值 2。定义了一个对应的值标签(也称为“性别”)并与新变量相关联,并且该变量本身被标记为“性别”。

         clear
        input id educ_none educ_prim educ_secabove  gender_male gender_female
        1 0 1 0  1 0
        2 1 0 0  1 0
        3 0 0 1  0 1
        4 0 1 0  1 0
        end
        
        /* Create local macro to hold root names */
        local roots educ gender
        
        /* Loop over each root */
        foreach v of local roots {
           qui gen `v' = 0  /* Initialize new variable from root */
        
            /* Get number of component variables */
           qui ds `v'_*
           local wc : word count `r(varlist)'
        
           /* Create new variables */
           forvalues k = 1/`wc' {
              /* z`k' is the k-th component variable */
              local z`k' : word `k' of `r(varlist)'  /* extended macro */
              qui replace `v' = `v'+`k'*`z`k''
              }
           /* Total components to check for missing/illegal values*/
           egen `v'tot = rowtotal(`v'_*)
           replace `v' = . if `v'tot != 1
           replace `v' = .a if `v'tot>1 & `v'tot<.
           /* Create value labels from variable names. Note that
              value labels can have same names as the
              the variables they label*/
        
           /* Create a value label consisting of the component variable names */
           varlabdef `v', vlist(`v'_*) from(name)
           label define `v' .a "Illegal", add
        
           /* Remove the roots from the labels and capitalize */
          labvalch3 `v', subst("`v'_" "")
          labvalch3 `v', strfcn(proper("@"))
          /* Assign the value labels to the new variables */
           label values `v' `v'
        }
        /* Give nice labels to the new variables */
        label var educ "Education"
        label var gender "Gender"
        
        label list
        tab educ
        tab gender
        

        结果是:

        . label list
        gender:
                   1 Male
                   2 Female
                  .a Illegal
        educ:
                   1 None
                   2 Prim
                   3 Secabove
                  .a Illegal
        
        . tab educ
        
          Education |      Freq.     Percent        Cum.
        ------------+-----------------------------------
               None |          1       25.00       25.00
               Prim |          2       50.00       75.00
           Secabove |          1       25.00      100.00
        ------------+-----------------------------------
              Total |          4      100.00
        
        .  tab gender
        
             Gender |      Freq.     Percent        Cum.
        ------------+-----------------------------------
               Male |          3       75.00       75.00
             Female |          1       25.00      100.00
        ------------+-----------------------------------
              Total |          4      100.00
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-01-15
          • 1970-01-01
          • 2012-01-26
          • 2016-04-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多