Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: Clarification requested about the at() option of -margins-

From   "Seed, Paul" <>
To   "" <>
Subject   RE: st: Clarification requested about the at() option of -margins-
Date   Thu, 24 Oct 2013 10:13:07 +0000

Dear Statalist, 
Trevor Zink asks why -margins- does not behave as he would expect following 
logistic regression. 

The answer is found only by going back to exactly what logistic regression 
actually does; and how it compares to linear regression.  

Linear regression is carried out under an assumption of constant slope, and has no problem 
therefore in estimating the slope at any value of the predictors.  With a single predictor, 
the estimated slope does not change. (Point 1 of Stata output).

However, it is inappropriate for a binary outcome, as it can lead to estimated proportions beyond 0 and 1.
(Point 2).

Logistic regression solves this by working with the log-odds, rather than the probability.  There are no 
impossible values.  Extreme log-odds correspond to probabilities close to 0 or 1. 
-margins- converts log-odds (and their slopes) to probabilities (and their slopes) for us.  (Point 3)

Although the slope for the log-odds is fixed;  that for the probability is not.  As 0 and 1 are approached, the 
slope tends to 0, and the possible values and SE are also constrained. (Point 4)

Plotting the estimated values against weight reveals this quite clearly. (Point 5)

The code below uses Trevor's example (amended and expanded).

************** Begin Stata code*******************
set more off
sysuse auto, clear

gen wt_tons = weight/2240 
* Change units to make results easier to understand
summarize wt_tons 
* maximum weight is 2.1607 tons

regress foreign wt_tons  
margins, dydx(wt_tons) at(wt_tons=(0(0.2)2 20)) 
* Point 1

margins, at(wt_tons=(0(0.2)2 )) 
* Point 2

logit foreign wt_tons  
margins, at(wt_tons=(0(0.2)2 ))
* Point 3

margins, dydx(wt_tons) at(wt_tons=(0(0.2)2 ))
* Point 4

predict Foreign if foreign
predict USA if !foreign

label var Foreign Foreign 
label var USA USA 
label var wt_tons "Car weight (tons)"

gr7 Foreign USA wt_tons, xlab(0 1 2) ylab(0 .5 1.0) l1title("Estimated probability of car being foreign")
* Point 5
**************** End Stata code *************

Best wishes,

Paul T Seed, Senior Lecturer in Medical Statistics, 
Division of Women's Health, King's College London
Women's Health Academic Centre, King's Health Partners 
(+44) (0) 20 7188 3642.

> Date: Wed, 23 Oct 2013 23:21:13 -0700
> From: Trevor Zink <>
> Subject: st: Clarification requested about the at() option of -margins-
> Long-time lurker, first-time post. I couldn't find a good explanation in
> the archives.
> I'm confused about what, specifically, -margins- is doing with the at()
> option, such that it can calculate margins for values of variable that
> don't exist in the data. To articulate with an example:
> sysuse auto
> summarize weight //maximum weight is 4840
> logit foreign weight  //nonsensical, but ok for the example
> margins, dydx(weight) at(weight=(0(1000)10000 100000))
> Here I ask for the slope of the function at a variety of weights from 0
> to 10,000 and also 100,000. The maximum weight observed in the data is 4840.
> My understanding of -margins- with at() was that it calculates the slope
> of the function holding the specified variables constant at the
> specified levels. But if the specified level doesn't appear in the data,
> how can Stata determine what the slope is at this value? Ok, it's
> clearly extrapolating, but based on what information? The only other
> information included in the above model is a constant. When I try the
> above but specifying the nocons option to -logit- Stata returns an
> error, so it must be forecasting based on the constant; but specifically
> how?
> What's even more strange to me is that the standard errors *shrink* as
> the estimates extend beyond the observed data. If Stata is forecasting
> based on only the constant this seems counter-intuitive to me.
> Thanks, and sorry if this is silly.
> Trevor Zink

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index