sysuse auto
reg price weight rep78
I want to calculate predicted price for each observation with rep78 equal to the mean and weight equal to the actual value, and make the predicted price a new variable called yhat.
for example, observation 1: weight=2930, rep78=3 (the mean rep78 is 3.405797 for the whole sample). The predicted price for the first observation is calculated based on weight=2930, rep78=3.405797.
I tried "margins weight, atmeans" which reports error, because weight should be a factor variable instead, rather than continuous variable as it is.
I tried "margins, at((asobs) weight) atmeans", but the predicted price is based on weight equal to its mean, which is not what I want.
What's more, I don't know how to generate a new variable yhat equal to the predicted price.
Thanks a lot!
reg price weight rep78
I want to calculate predicted price for each observation with rep78 equal to the mean and weight equal to the actual value, and make the predicted price a new variable called yhat.
for example, observation 1: weight=2930, rep78=3 (the mean rep78 is 3.405797 for the whole sample). The predicted price for the first observation is calculated based on weight=2930, rep78=3.405797.
I tried "margins weight, atmeans" which reports error, because weight should be a factor variable instead, rather than continuous variable as it is.
I tried "margins, at((asobs) weight) atmeans", but the predicted price is based on weight equal to its mean, which is not what I want.
What's more, I don't know how to generate a new variable yhat equal to the predicted price.
Thanks a lot!

Comment