Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

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

Re: st: use loop to add plots to scatter plot

From   Alberto Dorantes <>
Subject   Re: st: use loop to add plots to scatter plot
Date   Wed, 27 Mar 2013 16:57:28 -0600

Hi Linda.
I did something similiar few weeks ago. The example I did was about
graphing different investment portfolios based on 2 stocks, but
varying the correlation among the monthly stock returns. Try my
following code (just copy and paset on your do-file-editor, and run

* Create a dataset of 11 observations with 2 variables: weight a and weight b
set obs 11
egen a=fill(0(1)10)
replace a=a/10
gen b=1-a
* Each observation represents 2 weights or percentages invested in 2
financial assets, so each observation represents 1 investment
* I will use real data of 2 Stocks, A and B.
local paramgraficas ""
local i=0
* Define the expected values of monthly Stock Return A and Stock
Return B, based on the last 27 months:
scalar ERA = .01387535
scalar ERB = .00309298
* The variance-covariance matrix (2x2) of historical monthly returns
of both Stocks is the following:
matrix input COV = (0.005692,-0.00108\-0.00108,0.011222)
* I do a loop varying the pairwise correlation between the returns
forvalues rho1=-1(0.5)1 {
   local i=`i' + 1
   * Calculate the expected value of the portfolio return based on the
Expected value of Stock A and Stock B returns:
   gen ERP`i' = a*ERA + b*ERB
   label variable ERP`i' "rho = `rho1'"
   * Calculate the expected value of the portfolio variance:
   gen varP`i' = a^2*COV[1,1] + b^2*COV[2,2] +
   * Now calculate the risk of the portfolio i:
   gen sdP`i' = sqrt(varP`i')
   * Now append the twoway graph parameters:
   local paramgraficas "`paramgraficas'(connected ERP`i' sdP`i', mlabel(a)) "
* Finally do a twoway graph using the appended macro paramgraficas:

twoway `paramgraficas', note(Weights of Stock A are shown in each point) ///
       title(Portfolio Frontiers (2 Stocks) varying their pairwise
correlation ) ///
  ytitle(Return) xtitle(Risk)

I hope this help.

Alberto Dorantes

2013/3/23 Harris, Linda J <>:
> I have macros with names `antigen01' to `antigen40'
> I want to create a two way graph with the following:
> 1)   scatter plots with a variable # of these items (approx 5 and not in numeric order), each with a different marker
> 2)   linear fits for the same items, with lines in the same color as the markers
> 3)   a linear fit that is based on the aggregate selection of items following to a selection of these antigens (say approx 5):
> If possible, I would like to use a loop with a foreach statement rather than having to modify the specific items and # of items each time i ran this.
> I know that the last line is wrong - just have no idea how to accomplish this
> This is my first posting - hope I've done it right!  I'm also new to Stata programming, so hope this isn't too trivial a question.
> example  - say want `antigen01',  `antigen04'  `antigen05'
> twoway  ///
> (scatter yvar xvar if ANTIGEN==`antigen01', mcolor(red)) ///
> (lfit yvar xvar if ANTIGEN==`antigen01', lcolor (red)) ///
> (scatter yvar xvar if ANTIGEN==`antigen04', mcolor(green)) ///
> (lfit yvar xvar if ANTIGEN==`antigen04'), lcolor(green) ///
> (scatter yvar xvar if ANTIGEN==`antigen05', mcolor(blue)) ///
> (lfit yvar xvar if ANTIGEN==`antigen05', lcolor(blue)) ///
> (lift yvar xvar if ANTIGEN oneof [`antigen01' `antigen04'  `antigen5'], lcolor(black))
> Thank you,
> Linda
> *
> *   For searches and help try:
> *
> *
> *
*   For searches and help try:

© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index