# Margins from a regression

I have been playing around with different functions as I learn statistical inference and I have two related questions about marginal effects. With the below being computed in Stata.

sysuse auto, clear

1. When setting the intervals for margins, what is being done?

reg mpg price
margins, at(price=(3200(400)16000)) atmeans
marginsplot


The minimum value of price is 3291, so how is a margin at 3200 being computed? Also, given that these cutpoints make sense from a spacing perspective but the exact values of the cutpoint rarely exist in the sample, what is this showing us?

1. What is being computed with a missing data point or a single data point?

reg price mpg
margins, at(mpg=(12(1)41)) atmeans
marginsplot


In this example, every cutpoint is in the data is covered in my specified interval but certain cutpoints do not exist, such as mpg == 27. How is Stata computing a point estimate and confidence interval at this point (in the margins output for mpg == 27, the point estimate is 4802.913 and the std. err. is 429.6935? Similarly, what (and how) is being estimated off of one data point, such as mpg == 31?

I have been scratching my head for some time and would appreciate some thoughts.

margins is taking the 74 observations used by the regression and predicting Y with X set to some common value(s). The atmeans option would set other variables to their means in the estimation sample for that prediction, but since you only have a single regressor it does not matter if you omit it.

Given the function form imposed by the simple specification, Y and X have a constant linear relationship (Y = a + b*X) and you can extrapolate that relationship to values of X that are not seen your data or even don't make sense (negative prices). This can lead to goofy results if you take it too far outside the range of your data.

Here's code showing both (1) and (2) in action:

cls
sysuse auto
reg mpg price
margins, at(price=(-100000)) atmeans
margins, at(price=(-100000))

margins, at(price=(-100000 100000))

/* manual prediction of yhat */
preserve
replace price = -100000
predict yhat1
replace price = 100000
predict yhat2
sum yhat?
restore

/* manual prediction of SE(yhat) */
/* Calculate the standard errors of yhats using the variance of a sum of correlated variables formula */
matrix list e(V), format(%16.15f)
display "SE(yhat1) " sqrt(1.94310228328790 + 0.00000004170432*(100000^2) - 2*100000*(-0.00025711781051))
display "SE(yhat2) " sqrt(1.94310228328790 + 0.00000004170432*(100000^2) + 2*100000*(-0.00025711781051))

• This makes a lot of sense. How can I use this regression to see if there are any non-linearities related to mpg on price or the other way around?
– CJ12
Dec 18, 2019 at 23:36
• I would use theory, which suggest this is unlikely for this particular example. I might also try to bin mpg into some buckets (or quintile) and enter these as dummies. I could also try using splines. Dec 19, 2019 at 0:42
• Binning makes sense as a workaround, is it possible to see the natural form with a continuous variable, such as mpg spanning from 4 to 25 in 0.1 intervals
– CJ12
Dec 19, 2019 at 0:53
• Spline would be the way if you don't want to bin. You can also try to use nonparametric regression, but you might need more data than 74 obs. Dec 19, 2019 at 5:29