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]

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


From   "Joseph Coveney" <[email protected]>
To   <[email protected]>
Subject   st: Re: use loop to add plots to scatter plot
Date   Sun, 24 Mar 2013 12:40:18 +0900

Linda J Harris wrote:

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))


--------------------------------------------------------------------------------

If I understand what you're trying to do correctly, then you'll probably want to
write a little ditty ado-file to call with your desired list of antigens as the
argument.  Something like that below.  It will create the bunch of
individual-antigen scatterplots and line-fit plots and the pooled-data line-fit
plot and leave them all behind in memory, each with a unique name that you can
then refer to them by.

Usage would be something like:

    graphem my_antigen40 my_antigen2 my_antigen35

    <do what you want to do with the graphs>

    graphem my_antigen2 my_antigen5 my_antigen18 my_antigen29

    <do what you want to do with the graphs>

and so on.

Joseph Coveney

program define graphem
    version <insert your Stata version here>
    syntax varlist

    local color_list red green blue // fill out with your preferences
    local graph_nr 1
    foreach antigen of varlist `varlist' {
    
        // Pick color in sequence of color list
        local marker_color : word `graph_nr' of `color_list'

        // Make individual-antigen scatterplot
        graph twoway scatter yvar xvar if ANTIGEN == `antigen', ///
            mcolor(`marker_color') name("`antigen'_scatter")
        
        // Make plot of individual-antigen linear regression fit
        graph twoway lfit yvar xvar if ANTIGEN == `antigen', ///
            lcolor(`marker_color') name("`antigen'_linear_fit")

        // Concatenate list for "aggregate selection of items"
        local aggregate_antigens `aggregate_antigens' `antigen'

        // Next set of individual-antigen graphs
        local ++graph_nr
    }

    // Plot for pooled-data linear regression fit
    local pooled_fit : subinstr local aggregate_antigens " ", "_", all
    local aggregate_antigens : subinstr local aggregate_antigens " ", ", ", all
    graph twoway lfit yvar xvar if inlist(ANTIGEN, `aggregate_antigens'), ///
        lcolor(black) name("`pooled_fit'")
end

*
*   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