Bookmark and Share

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


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

Re: st: equation of a lfit in a scatter plot


From   Maarten Buis <[email protected]>
To   [email protected]
Subject   Re: st: equation of a lfit in a scatter plot
Date   Fri, 9 Aug 2013 12:12:05 +0200

On Fri, Aug 9, 2013 at 11:34 AM, Chiara Mussida wrote:
> is there an option to show the equation of a linear fit on a scatter
> plot. In detail, my command is as follows:
>
> twoway (scatter wage_dip urate, mlabel(regio)) (lfit wage_dip urate)
>
> It shows the scatter plot between my variable and their linear fit.
> How would I get the corresponding equation on my graph?

*------------------ begin example ------------------
sysuse auto, clear
reg price mpg

// find the dependt variable
local eq `"`e(depvar)' ="'

// choose a nice display format for the constant
local eq "`eq' `: di  %7.2f _b[_cons]'"

// should we add or subtract
local eq `"`eq' `=cond(_b[mpg]>0, "+", "-")'"'

// we already chose the plus or minus sign
// so we need to strip a minus sign when it is there
local eq `"`eq' `:di %6.2f abs(_b[mpg])' mpg"'

// add the error term
local eq `"`eq' + {&epsilon}"'

// add the equation as a note to the graph
twoway scatter price mpg || ///
       lfit price mpg,      ///
	   note("`eq'")
*------------------- end example -------------------
* (For more on examples I sent to the Statalist see:
* http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany

http://www.maartenbuis.nl
---------------------------------
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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