Announcement

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

  • loop for scatter plots

    Hi everyone,
    i have 1 dependent variable and 20 independent variables and i would like to plot scatter plots with overlaid predictive line using a loop for all the independent variables vs the dependent variable.
    thanks in advance

  • #2
    On versus see https://stats.stackexchange.com/ques...-data-analysis

    For the problem see aaplot (SSC) as announced at https://www.stata.com/statalist/arch.../msg01131.html


    Code:
    local j = 1 
    foreach x of var x1-x20 { 
         aaplot y `x', name(G`j') 
         local ++j 
    }
    where, naturally, you need to substitute your own variable names.

    This is an alternative:


    Code:
    local j = 1 
    foreach x of var x1-x20 { 
         scatter  y `x' || lfit y `x', name(G`j') 
         local ++j 
    }

    Comment


    • #3
      Thanks Nick for your help. i have succeeded to create my graphs using the code below. How can i combine all these graphs and show them in one sheet. i know how to use the combine command but i don't know how to add it to the code below.

      regards

      local j = 1
      foreach var of varlist bodyfatbmi-fastingdurationhours {
      scatter salivasalivaryamylaseumlnew `var' || lfit salivasalivaryamylaseumlnew `var', name(graph`j')
      local ++j
      }
      Last edited by abdelilah arredouani; 15 Jan 2019, 05:27.

      Comment


      • #4
        Please use CODE delimiters. This should be legal code, but I doubt that the result will be very readable. 20 graphs are quite a lot to combine.

        Code:
        local j = 1
        local names 
        foreach var of varlist bodyfatbmi-fastingdurationhours {
             scatter salivasalivaryamylaseumlnew `var' || lfit salivasalivaryamylaseumlnew `var', name(graph`j')
             local names `names' graph`j' 
             local ++j
        }
        
        graph combine `names'
        I hope you are using good explanations in variable labels.

        Comment


        • #5
          Sorry to bother you again. i am trying to use the same code as above to create box plot but i get a message error ( | invalid name r(198)). the code works perfectly for scatter plots.

          thanks in advance


          local j = 1
          local names
          foreach var of varlist bodyfatbmi-fastingdurationhours {
          graph box salivasalivaryamylaseumlnew `var' || lfit salivasalivaryamylaseumlnew `var', name(graph`j')
          local names `names' graph`j'
          local ++j
          }

          graph combine `names'

          Comment


          • #6
            graph box is incompatible with graph twoway and thus with any twoway command such as lfit.

            In any case the command makes no sense as well as being illegal.

            graph box with two variables will give you separate box plots for each variable. That can make sense if the two variables are measured in the same units. But the second variable is not an x axis variable as it is with lfit. So the two designs are incompatible in principle as well as in practice.

            Sometimes box plots are compatible with scatter plots, but you'd need to show how that might work with your data. Every prompt on writing a new post reminds you to read the FAQ Advice, and there we ask that you give data examples. So, show the results of


            Code:
            dataex  salivasalivaryamylaseumlnew bodyfatbmi
            If dataex doesn't work, then please see

            https://www.statalist.org/forums/help#version You're using an out-of-date version of Stata, and should be telling us that.

            https://www.statalist.org/forums/help#stata You need to install dataex. This tells you how.

            PS: Please use CODE delimiters, as already requested. They make code easier to read! No more, no less.
            Last edited by Nick Cox; 15 Jan 2019, 06:37.

            Comment

            Working...
            X