Announcement

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

  • generating a variable that computes conditional expectation

    Hi,

    I have two variables of height and weight, I need the command that generates a third variable that gives the expected value of height for each value of weight.

  • #2
    please read the FAQ which has excellent advice on how to ask questions

    what do you mean by the "expected value of height for each value of weight"? if you mean the mean height at each weight, then something like the following will do it (here making assumptions about you data setup since you supplied no data example as per the FAQ):
    Code:
    egen meanht=mean(height), by(weight)
    I can imagine lots of data sets where this would not be worthwhile because most weights occur only once but without a data sample or better guidance on what you actually want ...

    Comment


    • #3
      In the future, when asking for help with code, please show example data. And be sure to use the -dataex- command to do that. If you are running version 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.

      For now, I will assume that your data set contains one variable called height and another called weight (and perhaps other variables as well). Then you can do this:

      Code:
      by weight, sort: egen expected_height = mean(height)
      Added: Crossed with #2. The proposed code there is equivalent to that here. Rich Goldstein's additional comments about the suitability of doing this are astute--do heed them.

      Comment

      Working...
      X