Pushing circles around
Friday, 13 July 2012
Occasionally, a need comes up for drawing a circle. Say you have a scattershot of points that follow a bivariate normal distribution, and you want to illustrate being within a given radius inside that scattershot somewhere. You will want to draw a circle so you can use its functional form to isolate the dots that fall inside it, as well as help guide your eye.
Nick Cox already explained how to draw a unit circle here. The code below shows how you can start from there and draw any other circle.
clear all
set more off
pause on
// A circle starts like this:
// 1. unit circle: x^2+y^2=1
// 2. circle of radius r centered in origin: x^2+y^2=r^2
// 3. circle of radius r centered at (a, b): (x-a)^2+(y-b)^2=r^2
// OK, so 3. is the generic form. Use it to describe y:
// y-b= [+/-]sqrt(r^2-(x-a)^2)
// y =b[+/-]sqrt(r^2-(x-a)^2)
// Now you can draw circles, one half at a time
// as Nick Cox explained it in the Stata Journal,
// using the twoway function graph:
// http://www.stata-journal.com/sjpdf.html?articlenum=gr0010
// If you want more than one circle in a picture,
// it's worth doing some setting up first.
// E.g., formula components with values to be
// filled in later can be written once here:
// 1. the square-root part
local sqrt sqrt(\`r'^2-(x-\`a')^2)
// 2. functions for the top and bottom semicircles
local topf function y=\`b'+\`sqrt', range(\`rl' \`rr') \`c'
local bottomf function y=\`b'-\`sqrt', range(\`rl' \`rr') \`c'
// An example of a circle drawn in the 1st quadrant
local a=1
local b=1
local r=.5
local rl=`a'-`r' // x [r]ange [l]eft end
local rr=`a'+`r' // x [r]ange [r]ight end
local c color(red)
local circle1 (`topf' aspect(1)) (`bottomf')
// An example of a circle drawn in the 3rd quadrant
local a=-1
local b=-1
local r=.5
local rl=`a'-`r'
local rr=`a'+`r'
local c color(blue)
local circle2 (`topf') (`bottomf')
// Reference case: the unit circle
local a=0
local b=0
local r=1
local rl=`a'-`r'
local rr=`a'+`r'
local c color(black)
local circleu (`topf') (`bottomf')
// Might as well string them all together
local circles `circle1' `circle2' `circleu'
// Draw the picture
twoway `circles', legend(off)
No. 1 — September 13th, 2012 at 3:50 am
Hi Gabi,
I was wondering if you could help me with some advise on a simple calculation that I am trying to do is Stata. The questions, however does not relate to your post.
I have a panel of over 500 companies. Some of them are independent some are subsidiaries of parent companies. I also have in the same smaple a number of companies that are parent companies.Therefore the dataset comprises companies held by other companies, independent companies and parent companies.
Suppose you have all the necessary id and other variables that help you distinguish between them. The parent companies' data is consolidated and therefore I would like to compute simple ratios to see the weight of total assets of all the subsidiaries in the total assets of the consolidated Balance Sheet of the parent company. Therefore what I would like to achieve e.g. Total Assets_subsidiaryX/Total Assets(ParentX.
In case you have a readily available solution for this or any suggestions please let me know.
Regards,
Adi
No. 2 — September 13th, 2012 at 8:57 pm
I'd do something like this:
/*
Rules:
A parent company is its own subsidiary.
An independent company is its own subsidiary, and its own parent.
A subsidiary has a parent that is not the subsidiary itself.
You generate sub to parent asset ratio just like that:
gen subtoparentassetratio=subassets/parentassets
But you need to identify the respective assets first, as
separate variables.
*/
clear all
set more off
pause on
set obs 500
gen subid=_n
gen parentid=ceil(subid/100)*100 // id 100...500 are parents
replace parentid=subid in 1/10 // 1st 10 companies are indies
gen subassets=runiform()
tempvar x
bysort parentid: gen `x'=subassets if _n==1
replace `x'=0 if missing(`x')
bysort parentid: egen parentassets=max(`x')
drop `x'
gen subtoparentassetratio=subassets/parentassets