Announcement

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

  • Creating a new variable from several items' mean

    Hi, I have 4 items at baseline for which I calculated the mean and SD with the following commands:

    sum var1 if timepoint == 1
    sum var2 if timepoint == 1
    sum var3 if timepoint == 1
    sum var3 if timepoint == 1

    Then I need to estimate a new variable (mean and SD) of the sum of each items mean
    (mean var1 + mean var2 + meanvar3 + meanvar4)/4
    What would be the command for this second part?

    Thanks!

  • #2
    If
    Code:
    timepoint == 1
    defines a single observation then consider

    Code:
    egen wanted = rowmean(var1 var2 var3 var4) if timepoint == 1
    or indeed

    Code:
    gen wanted = (var1 + var2  + var3 + var4) / 4 if timepoint == 1
    -- except that the latter is more fragile in the face of missing values.

    Otherwise, you need something like this to average first over variables and then over observations. In principle, the opposite direction works too, but the code is more awkward.

    Code:
    egen wanted = rowmean(var1 var2 var3 var4) if timepoint == 1
    egen wanted2 = mean(wanted) if timepoint == 1
    Come to think, this should work too

    Code:
    egen wanted = mean((var1 + var2 + var3 + var4)/4) if timepoint == 1
    so long as there are no missing values.
    Last edited by Nick Cox; 30 Nov 2023, 05:57.

    Comment


    • #3
      Correct string:
      sum var1 if timepoint == 1
      sum var2 if timepoint == 1
      sum var3 if timepoint == 1
      sum var4 if timepoint == 1

      Comment

      Working...
      X