Announcement

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

  • Sample from data

    Hello, everybody.
    I am a beginner, and need help for my bachelor thesis.
    I have 600 numbers and need to get samples from that.
    To simplifise, I need to sample by random choice 30 numbers from that 600, then to get a mean from that 30.
    This operation I need to do 100 times. So, in the end I should have 100 mean-numbers.
    What is the best way to do it? can you explain, please, how to do it step by step.

    Thanks a lot,
    Stefan.

  • #2
    Stef:
    welcome to the list.
    What follows is possibly what you're looking for:
    Code:
    . set obs 600
    number of observations (_N) was 0, now 600
    
    . g A=4*uniform()
    
    . set seed 1
    
    . g double mean=.
    (600 missing values generated)
    
    . quietly forvalues i = 1/100 {
    .           preserve
    . sample 30 if A !=., count
    .          sum A
    .          scalar Am = r(mean)
    .          restore
    .          replace mean = Am in `i'
    . }
    All credits goes out to Brian Poi, who helped me out some years ago for a similar probem. Obviously, all possible mistakes are my own responsibility..
    Kind regards,
    Carlo
    (Stata 18.0 SE)

    Comment


    • #3
      Hi, Carlo. Thank you very much for aour responce.
      I tried to use your code, but I think, I did comething wrong since there is no result. Can you please look at my code?


      . import excel "F:\Bachelor\584 st dev.xlsx", sheet("Tabelle1")

      . set obs 584
      number of observations (_N) was 584, now 584

      . set seed 1

      . g double mean=.
      (584 missing values generated)

      . quietly forvalues i= 1/100 {
      . preserve
      . sample 30 if A !=., count
      . sum A
      . scalar Am = r(mean)
      . restore
      . replace mean = Am in `i'
      . }



      Last edited by Stef Appel; 24 Mar 2017, 04:55.

      Comment


      • #4
        Originally posted by Carlo Lazzaro View Post
        Stef:
        welcome to the list.
        What follows is possibly what you're looking for:
        Code:
        . set obs 600
        number of observations (_N) was 0, now 600
        
        . g A=4*uniform()
        
        . set seed 1
        
        . g double mean=.
        (600 missing values generated)
        
        . quietly forvalues i = 1/100 {
        . preserve
        . sample 30 if A !=., count
        . sum A
        . scalar Am = r(mean)
        . restore
        . replace mean = Am in `i'
        . }
        All credits goes out to Brian Poi, who helped me out some years ago for a similar probem. Obviously, all possible mistakes are my own responsibility..
        Hi, Carlo. Thank you very much for aour responce.
        I tried to use your code, but I think, I did comething wrong since there is no result. Can you please look at my code?

        . import excel "F:\Bachelor\584 st dev.xlsx", sheet("Tabelle1")

        . set obs 584
        number of observations (_N) was 584, now 584

        . set seed 1

        . g double mean=.
        (584 missing values generated)

        . quietly forvalues i= 1/100 {
        . preserve
        . sample 30 if A !=., count
        . sum A
        . scalar Am = r(mean)
        . restore
        . replace mean = Am in `i'
        . }




        Best regards,
        Stef.
        Last edited by Stef Appel; 24 Mar 2017, 04:56.

        Comment


        • #5
          Stef:
          works for me.
          Hint: copy and paste the code on a do-file, scroll down -Tools- and then select and click on -Execute selection (do)-

          Code:
          set obs 584
          g A=4*uniform()
          set seed 1
          g double mean=.
          quietly forvalues i = 1/100 {
          preserve
          sample 30 if A !=., count
          summ A
          scalar Am = r(mean)
          restore
          replace mean = Am in `i'
          }
          Kind regards,
          Carlo
          (Stata 18.0 SE)

          Comment


          • #6
            Originally posted by Carlo Lazzaro View Post
            Stef:
            works for me.
            Hint: copy and paste the code on a do-file, scroll down -Tools- and then select and click on -Execute selection (do)-

            Code:
            set obs 584
            g A=4*uniform()
            set seed 1
            g double mean=.
            quietly forvalues i = 1/100 {
            preserve
            sample 30 if A !=., count
            summ A
            scalar Am = r(mean)
            restore
            replace mean = Am in `i'
            }
            Carlo, it does not work. I send you my printscreen.
            Click image for larger version

Name:	Unbenannt.PNG
Views:	1
Size:	350.2 KB
ID:	1379963

            Comment


            • #7
              Steff:
              try this:
              Code:
              drop _all
              set obs 584
              g A=4*uniform()
              set seed 1
              g double mean=.
              quietly forvalues i = 1/100 {
              preserve
              sample 30 if A !=., count
              summ A
              scalar Am = r(mean)
              restore
              replace mean = Am in `i'
              }
              Kind regards,
              Carlo
              (Stata 18.0 SE)

              Comment


              • #8
                Originally posted by Carlo Lazzaro View Post
                Steff:
                try this:
                Code:
                drop _all
                set obs 584
                g A=4*uniform()
                set seed 1
                g double mean=.
                quietly forvalues i = 1/100 {
                preserve
                sample 30 if A !=., count
                summ A
                scalar Am = r(mean)
                restore
                replace mean = Am in `i'
                }
                Carlo,
                the result, still can't find the numbers:
                Click image for larger version

Name:	Unbenannt.PNG
Views:	1
Size:	269.8 KB
ID:	1379971


                Best regards,
                Stef.

                Comment


                • #9
                  Stef:
                  have you tried:
                  Code:
                  su A mean
                  Kind regards,
                  Carlo
                  (Stata 18.0 SE)

                  Comment


                  • #10
                    Originally posted by Carlo Lazzaro View Post
                    Stef:
                    have you tried:
                    Code:
                    su A mean
                    Carlo:
                    Where should I put it?

                    Comment


                    • #11
                      Stef:
                      try:
                      Code:
                      drop _all
                      set obs 584
                      g A=4*uniform()
                      set seed 1
                      g double mean=.
                      quietly forvalues i = 1/100 {
                      preserve
                      sample 30 if A !=., count
                      summ A
                      scalar Am = r(mean)
                      restore
                      replace mean = Am in `i'
                      }
                      su A mean
                      Kind regards,
                      Carlo
                      (Stata 18.0 SE)

                      Comment


                      • #12
                        Originally posted by Carlo Lazzaro View Post
                        Stef:
                        try:
                        Code:
                        drop _all
                        set obs 584
                        g A=4*uniform()
                        set seed 1
                        g double mean=.
                        quietly forvalues i = 1/100 {
                        preserve
                        sample 30 if A !=., count
                        summ A
                        scalar Am = r(mean)
                        restore
                        replace mean = Am in `i'
                        }
                        su A mean
                        Carlo, I got a result. I have two questions. First, I wanted to get 100 numbers. Here I see a mean for of 2, 049231. If I understand right, it is a mean number of 100 numbers, which we wanted to get initially. Is it right? If yes, it is alright. Actually I need this number.
                        But, I wanted to use my numbers, which I have in my excel sheet. Is it possible? I have 584 numbers, and sample should use them.
                        Click image for larger version

Name:	Unbenannt.PNG
Views:	1
Size:	258.3 KB
ID:	1379982


                        Thanks a lot,
                        Stef.

                        Comment


                        • #13
                          Stef:
                          - you have the overall mean of 100 means, each calculated on a random sample of 30 out 584 observations, as per your request;
                          - you can import your spreadsheet in Stata via File>Import><select the option which is right for you>
                          Kind regards,
                          Carlo
                          (Stata 18.0 SE)

                          Comment


                          • #14
                            Originally posted by Carlo Lazzaro View Post
                            Stef:
                            - you have the overall mean of 100 means, each calculated on a random sample of 30 out 584 observations, as per your request;
                            - you can import your spreadsheet in Stata via File>Import><select the option which is right for you>
                            Carlo:
                            I already imported my excel sheet. But numbers in my excel sheet vary between 0,005... and 0,063.... I mean, the mean of 2,049231 can not be right for my numbers.
                            Sorry for your time, but I really need it.

                            I try to explain again, I have 584 numbers that vary between 0,005... and 0,063...
                            First, I need a random sample of 30 numbers from 584. Then a mean from that 30 numbers.
                            This opperation should be done 100 times. In the end, I have 100 numbers, and from them I need to get a mean.

                            I think, the code is absolutely correct, and refers my needs. But result of 2, 049231 is not right. Do you know what I mean?


                            Best regards,
                            Stef.

                            Comment


                            • #15
                              Stef:
                              simply replace your values in A (or import your 584-number variable and name it -A- in Stata.dta) and run the rest of the code.
                              Kind regards,
                              Carlo
                              (Stata 18.0 SE)

                              Comment

                              Working...
                              X