Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Creating standardized variables per participants on multiple variables

    I am working with eye-tracking data and I would like to standardize pupil size per participant on multiple variables. However, the standardized variable shows missing values. In total, I have 131 participants and 24 variables. I added a screenshot of what my data looks like. In my data rec stands for a record (participant number) and mp_BE6a etc are the variables.

    Thank you for your advice and help in advance!

    Best,
    Nader Brishna
    Attached Files

  • #2
    However, the standardized variable shows missing values.
    As you do not show, nor even hint at, the code you used, it is impossible to troubleshoot whatever you did.

    Suffice it to say, this seems like a fairly straightforward problem:
    Code:
    foreach v of varlist mp_* {
        egen std_`v' = std(`v')
        order std_`v', after(`v')
    }
    Your attempt to show example data is appreciated. But screenshots are not nearly as helpful as you might imagine. Even when they are readable (which they often aren't), it is not possible to import data from a screenshot into Stata to test code on. Consequently, the code above is untested and may contain typos or other errors.

    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Added: It dawns on me that I may have completely misunderstood what you want. The code I proposed creates, foreach variable mp_*, a new variable which is then standardized over the distribution of that variable among participants.

    It may be that what you really want is for each participant, to create a series of variables, paralleling the mp_* variables, that are standardized within the participant across the distribution of the different mp_* variables. In that case, the code would be
    Code:
    egen M = rowmean(mp_*)
    egen SD = rowsd(mp_*)
    foreach v of varlist mp_* {
        gen std_`v' = (`v'-M)/SD, after(`v')
    }


    Last edited by Clyde Schechter; 14 Apr 2024, 10:08.

    Comment


    • #3
      Dear Clyde,

      Thank you for your answer! I appreciate your advice.
      Indeed, I used this code:
      foreach v of varlist mp_* { egen std_`v' = std(`v') order std_`v', after(`v') } And it give missing values.
      The second code suggested by you is helpful and it works however my question is how can I create a variable of standardized pupil size for each participant by taking multiple variables.
      My data looks as follows:

      clear
      input int rec_n float(mp_BE6b mp_CO6b mp_BE6a mp_CO6a mp_SE5a mp_ST5a)
      rec_n mp_BE6b mp_CO6b mp_BE6a mp_CO6a mp mp_SE5a mp_ST5a
      76 1.935146 1.8218018 1.8797386 1.8696063 1.785799 1.97918
      17 1.97967 2.09859 2.0773 2.10706 2.001925 2.19189
      40 2.0002618 1.939713 1.8846872 1.916662 1.943745 1.9766574
      77 2.0113244 2.010264 2.0449686 2.0421133 2.0314066 2.0196443


      Thank you!
      Last edited by Nader Brishna; 15 Apr 2024, 06:15. Reason: I

      Comment


      • #4
        Indeed, I used this code:
        foreach v of varlist mp_* { egen std_`v' = std(`v') order std_`v', after(`v') } And it give missing values.
        When I run that code with your example data on my setup, it does not produce missing values. It creates valid variables and the numbers it creates appear to be correct:
        Code:
        . clear
        
        . input int rec_n float(mp_BE6b mp_CO6b mp_BE6a mp_CO6a mp_SE5a mp_ST5a)
        
                rec_n    mp_BE6b    mp_CO6b    mp_BE6a    mp_CO6a    mp_SE5a    mp_ST5a
          1. rec_n mp_BE6b mp_CO6b mp_BE6a mp_CO6a mp mp_SE5a mp_ST5a
        'rec_n' cannot be read as a number
          1. 76 1.935146 1.8218018 1.8797386 1.8696063 1.785799 1.97918
          2. 17 1.97967 2.09859 2.0773 2.10706 2.001925 2.19189
          3. 40 2.0002618 1.939713 1.8846872 1.916662 1.943745 1.9766574
          4. 77 2.0113244 2.010264 2.0449686 2.0421133 2.0314066 2.0196443
          5. end
        
        .
        . foreach v of varlist mp_* {
          2.                 egen std_`v' = std(`v')
          3.                 order std_`v', after(`v')
          4. }
        
        . list, noobs clean
        
            rec_n    mp_BE6b   std_m~E6b    mp_CO6b   std_m~O6b    mp_BE6a   std_m~E6a    mp_CO6a   std_m~O6a    mp_SE5a   std_m~E5a    mp_ST5a   std_m~T5a  
               76   1.935146   -1.381227   1.821802   -1.246886   1.879739   -.8826345   1.869606   -1.040954   1.785799   -1.414616    1.97918   -.6146309  
               17    1.97967   -.0573994    2.09859    1.120371     2.0773    1.014081    2.10706    1.122455   2.001925    .5588894    2.19189    1.471741  
               40   2.000262    .5548515   1.939713   -.2384391   1.884687   -.8351247   1.916662   -.6122354   1.943745    .0276321   1.976657   -.6393738  
               77   2.011324    .8837747   2.010264    .3649544   2.044969    .7036783   2.042113    .5307345   2.031407    .8280945   2.019644    -.217736
        I have non explanation for why you are getting missing values on your set up. The generic response to this kind of situation is:
        1. Make sure your version of Stata is fully updated by running -update all, force-. Also reboot your computer. Then try again.
        2. If that doesn't work, uninstall Stata and reinstall it.
        3. If that doesn't work, contact StataCorp's technical support, providing them with a do-file containing the code that is performing incorrectly, example data, and the output of running -about- on your system.
        The second code suggested by you is helpful and it works however my question is how can I create a variable of standardized pupil size for each participant by taking multiple variables.
        As I understand your request, the second block of code I showed in #2 does this. If that is not what you meant, please post back with a clearer explanation of what you are looking for. Perhaps do that by illustrating how you would go about calculating it by hand if you had to.

        Comment

        Working...
        X