Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • What function can I use to look at several different vars with same code ?

    Dear statalist

    I am about to look at the frequency dist of several independent variables in my dataset, using the lengthy code similar to the example i have shown below.

    In the example below, imagine I wanted do the analyses for headroom, reat_seat, trunk weight etc

    For the past years, I have been copying everything into MSword, used search/replace "headroom" and change to "rearseat". And then pasted this new output into my (even lengthier) do file.

    There must be a simpler way to run the same code on several variables?

    I would be forever grateful for a tip on this!

    I am using Stata/IC 14.2 for Mac

    Code:
    use http://www.stata-press.com/data/r14/autofull.dta, clear
    
    *Example of 3 IVs could be:    headroom reat_seat trunk weight
    *name used in var and when naming graphs
                
                    *transformations
                    gen    sqr_headroom     =sqrt(headroom)
                    gen l_headroom            =ln(headroom)
                    gen l10_headroom        =log10(headroom)
                    gen i_headroom            =1/(headroom)
            
                    *do they resemble Gaussian?
                    dotplot headroom        if price<15000 , ml(order)  name(d1, replace)    ytitle("raw")
                    dotplot sqr_headroom     if price<15000 , ml(order)     name(d2, replace)     ytitle("sqrt")
                    dotplot l_headroom         if price<15000 , ml(order)     name(d3, replace)     ytitle("ln")
                    dotplot l10_headroom     if price<15000 , ml(order)     name(d4, replace)     ytitle("log10")
                    dotplot i_headroom         if price<15000 , ml(order)     name(d5, replace)     ytitle("inv")
                            
                    qnorm  headroom     if price<15000 , ml(order)     name(q1, replace)     ytitle("raw")    
                    qnorm sqr_headroom     if price<15000 , ml(order)     name(q2, replace)     ytitle("sqrt")
                    qnorm l_headroom     if price<15000 , ml(order)     name(q3, replace)     ytitle("ln")
                    qnorm l10_headroom     if price<15000 , ml(order)     name(q4, replace)     ytitle("log10")
                    qnorm i_headroom     if price<15000 , ml(order)     name(q5, replace)     ytitle("inv")
                    
                    swilk headroom         if price<15000
                    swilk sqr_headroom     if price<15000
                    swilk l_headroom     if price<15000
                    swilk l10_headroom     if price<15000
                    swilk i_headroom     if price<15000
    Last edited by Anne Dam; 18 Feb 2022, 04:57.

  • #2
    A command (strictly, not a function) that you might use is do with a dedicated do-file that you pass a variable name to. As a token put this in mytrans.do

    Code:
    args which 
     
     *transformations
                    gen    sqr_`which'    =sqrt(`which' )
                    gen l_`which'             =ln(`which' )
                    gen l10_`which'         =log10(`which' )
                    gen i_`which'             =1/(`which' )
                    
    su *_`which', detail 
                    
    drop *_`which'
    and then in your case try

    Code:
    do mytrans.do headroom
    or mention whatever other variable you want. The do-file has to be in your present working directory, or if not you need to tell Stata where to look.

    I would differ from anybody who says "write a program" here. Not needed for what you are asking, and far more things should be understood how to do it.

    In your particular case, ln() and log10() won't tell you anything different about a distribution, as each is the other apart from a multiplication, so skewness, kurtosis and appearances of outliers are identical. The choice is that the numbers for one might be easier or more appropriate.

    See also https://www.statalist.org/forums/for...dable-from-ssc

    Comment


    • #3
      Of course! That is much cleverer!

      And the transplot package is such a great tool - makes everything easier!!

      Thank you so much for sharing Nick!!!

      Comment

      Working...
      X