Announcement

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

  • How to produce output for this

    Hi,

    I have this coding and when I enter it into STATA nothing happens. How do I get it to produce the answers/output? Thanks.


    program framingham, rclass version 13.0 syntax [if] [in] , /// Male(varname numeric) /// integer 0 or 1 AGE(varname numeric) /// SBP(varname numeric) /// TRhtn(varname numeric) /// integer 0 or 1 SMoke(varname numeric) /// integer 0 or 1 DIAB(varname numeric) /// integer 0 or 1 HDL(varname numeric) /// CHOL(varname numeric) /// [ OPTimal /// REPLace SUFFix(str) *]

    marksample touse markout `touse' `v' `sbp' `hdl' `chol' `male' `trhtn' `smoke' `diab' qui count if `touse' if r(N) == 0 error 2000 /* check to ensure binary variables contain 0 or 1 */ foreach v in `male' `trhtn' `smoke' `diab' { capture assert inlist(`v', 0, 1) if `touse' if _rc { di as err "`v' contains values other than 0 or 1" exit 498 }

    } /* drop variables if option "replace" is chosen */ if "`replace'" != "" { local framvars : char _dta[framvars`suffix'] if "`framvars'" != "" { foreach f of local framvars { capture drop `f' } } }

    quietly { tempvar risk gen `risk' =. replace `risk' = ln(`age') * 3.06117 + ln(`sbp') * 1.93303 + ln(`chol') * 1.1237 + ln(`hdl') * -0.93263 + `smoke' * 0.65451 + `diab' * 0.57367 if `male' == 1 & `trhtn' == 0 & `touse'

    replace `risk' = ln(`age') * 3.06117 + ln(`sbp') * 1.99881 + ln(`chol') * 1.1237 + ln(`hdl') * -0.93263 + `smoke' * 0.65451 + `diab' * 0.57367 if `male' == 1 & `trhtn' == 1 & `touse'

    replace `risk' = ln(`age') * 2.32888 + ln(`sbp') * 2.76157 + ln(`chol') * 1.20904 + ln(`hdl') * -0.70833 + `smoke' * 0.52873 + `diab' * 0.69154 if `male' == 0 & `trhtn' == 0 & `touse'

    replace `risk' = ln(`age') * 2.32888 + ln(`sbp') * 2.82263 + ln(`chol') * 1.20904 + ln(`hdl') * -0.70833 + `smoke' * 0.52873 + `diab' * 0.69154 if `male' == 0 & `trhtn' == 1 & `touse' gen risk10`suffix' =. replace risk10`suffix' = 1 - 0.88936^exp(`risk'- 23.9802) if `male' == 1 & `touse'

    replace risk10`suffix' = 1 - 0.95012^exp(`risk'- 26.1931) if `male' == 0 & `touse'

    if "`optimal'" !="" { tempvar optrisk gen `optrisk' =. replace `optrisk' = ln(`age') * 3.06117 + ln(110) * 1.93303 + ln(160) * 1.1237 + ln(60) * -0.93263 if `male' == 1 & `touse'

    replace `optrisk' = ln(`age') * 2.32888 + ln(110) * 2.76157 + ln(160) * 1.20904 + ln(60) * -0.70833 if `male' == 0 & `touse'

    gen optrisk10`suffix' =. replace optrisk10`suffix' = 1 - 0.88936^exp(`optrisk'- 23.9802) if `male' == 1 & `touse'

    replace optrisk10`suffix' = 1 - 0.95012^exp(`optrisk'- 26.1931) if `male' == 0 & `touse'

    }

    local framvars risk10`suffix' optrisk10`suffix' char def _dta[framvars`suffix'] "`framvars'"

    } // close quietly loop

    end
    Last edited by Joe Tuckles; 02 Jul 2018, 07:05.

  • #2
    I'm not sure I understand - what do you expect to be produced? I see -gen- and -replace- statements and the messages they produce will (generally) be blocked by your -quietly- command; further, since you don't show us what command you are actually using to invoke this program, it is very hard to assess; finally, please show code with CODE blocks (read the FAQ if you don't know what I mean)

    Comment


    • #3
      That's really hard to read. I think you intended something more like this. Note how CODE delimiters (do please read FAQ Advice #12) make code much easier to read. Also please read the whole of that from #1 to#18.

      Code:
      program framingham, rclass
      version 13.0
      syntax [if] [in] , ///
      Male(varname numeric) /// integer 0 or 1
      AGE(varname numeric) ///
      SBP(varname numeric) ///
      TRhtn(varname numeric) /// integer 0 or 1
      SMoke(varname numeric) /// integer 0 or 1
      DIAB(varname numeric) /// integer 0 or 1
      HDL(varname numeric) ///
      CHOL(varname numeric) ///
      [ OPTimal ///
      REPLace SUFFix(str) *]
      
      marksample touse
      markout `touse' `v' `sbp' `hdl' `chol' `male' `trhtn' `smoke' `diab'
      qui count if `touse'
      if r(N) == 0 error 2000
      
      /* check to ensure binary variables contain 0 or 1 */
      foreach v in `male' `trhtn' `smoke' `diab' {
           capture assert inlist(`v', 0, 1)
           if `touse' if _rc {
               di as err "`v' contains values other than 0 or 1"
               exit 498
          }
      }
      
      /* drop variables if option "replace" is chosen */
      if "`replace'" != "" {
           local framvars : char _dta[framvars`suffix']
           if "`framvars'" != "" {
                foreach f of local framvars {
                     capture drop `f'
                }
           }
      }
      
      quietly {
      
      tempvar risk
      gen `risk' =.
      replace `risk' = ln(`age') * 3.06117 + ln(`sbp') * 1.93303 + ln(`chol') * 1.1237 + ln(`hdl') * -0.93263 + `smoke' * 0.65451 + `diab' * 0.57367 if `male' == 1 & `trhtn' == 0 & `touse'
      
      replace `risk' = ln(`age') * 3.06117 + ln(`sbp') * 1.99881 + ln(`chol') * 1.1237 + ln(`hdl') * -0.93263 + `smoke' * 0.65451 + `diab' * 0.57367 if `male' == 1 & `trhtn' == 1 & `touse'
      
      replace `risk' = ln(`age') * 2.32888 + ln(`sbp') * 2.76157 + ln(`chol') * 1.20904 + ln(`hdl') * -0.70833 + `smoke' * 0.52873 + `diab' * 0.69154 if `male' == 0 & `trhtn' == 0 & `touse'
      
      replace `risk' = ln(`age') * 2.32888 + ln(`sbp') * 2.82263 + ln(`chol') * 1.20904 + ln(`hdl') * -0.70833 + `smoke' * 0.52873 + `diab' * 0.69154 if `male' == 0 & `trhtn' == 1 & `touse'
      
      gen risk10`suffix' =.
      replace risk10`suffix' = 1 - 0.88936^exp(`risk'- 23.9802) if `male' == 1 & `touse'
      replace risk10`suffix' = 1 - 0.95012^exp(`risk'- 26.1931) if `male' == 0 & `touse'
      
      if "`optimal'" !="" {
           tempvar optrisk
           gen `optrisk' =.
           replace `optrisk' = ln(`age') * 3.06117 + ln(110) * 1.93303 + ln(160) * 1.1237 + ln(60) * -0.93263 if `male' == 1 & `touse'
           replace `optrisk' = ln(`age') * 2.32888 + ln(110) * 2.76157 + ln(160) * 1.20904 + ln(60) * -0.70833 if `male' == 0 & `touse'
           gen optrisk10`suffix' =.
           replace optrisk10`suffix' = 1 - 0.88936^exp(`optrisk'- 23.9802) if `male' == 1 & `touse'
           replace optrisk10`suffix' = 1 - 0.95012^exp(`optrisk'- 26.1931) if `male' == 0 & `touse'
      }
      
      local framvars risk10`suffix' optrisk10`suffix'
      char def _dta[framvars`suffix'] "`framvars'"
      
      } // close quietly loop
      
      end
      As to why "when I enter it" "nothing happens", this code just defines a program. You need to run it with some data and specify at least options

      Code:
      Male(varname numeric) /// integer 0 or 1
      AGE(varname numeric) ///
      SBP(varname numeric) ///
      TRhtn(varname numeric) /// integer 0 or 1
      SMoke(varname numeric) /// integer 0 or 1
      DIAB(varname numeric) /// integer 0 or 1
      HDL(varname numeric) ///
      CHOL(varname numeric) ///
      So -- I am not a medic, epidemiologist or biostatistician -- but it seems that you need to specify those variables for patients in order to calculate various quantities using data from the Framingham study.

      Last edited by Nick Cox; 02 Jul 2018, 07:23.

      Comment


      • #4
        Hi there, thanks for your reply. Sorry I did not see about the CODE blocks.

        I am trying to create a new variable that is the calculation of patient risk. This is generated using 8 other variables and various calculations. I have not used any command to invoke the program. Everything I have put into STATA is copied above

        Comment


        • #5
          Hi thanks for your reply. I have imported an excel sheet with all those variables for each of my patients but still nothing happens

          Comment


          • #6
            Joe: Messages are criss-crossing here but as already advised

            1. "since you don't show us what command you are actually using to invoke this program, it is very hard to assess" (Rich in #2, still true)

            You need to type a command that starts

            Code:
            framingham, age(whatever)
            where whatever is the name of your age variable (could be age for all we know, but we can't see your data and you don't give a data example) and that continues with the other options that are needed.

            2. please do as asked and read the entire FAQ Advice, which among other things, makes the same point at length.
            (Why did you not see about the CODE blocks? Presumably because you didn't read the FAQ Advice.)
            Last edited by Nick Cox; 02 Jul 2018, 07:39.

            Comment


            • #7
              Ahh thanks so much for your help. Does this help with what you are saying?

              Code:
               
              
              . program framingham, rclass
                1. 
              . version 13.0
                2. 
              . syntax [if] [in] ,  ///
                3. 
              . Male( gender )  /// integer 0 or 1
                4. 
              . AGE( age )    /// 
                5. 
              . SBP( sbp )  ///
                6. 
              . TRhtn( hyptreat ) /// integer 0 or 1
                7. 
              . SMoke( smoke ) /// integer 0 or 1
                8. 
              . DIAB( diab ) /// integer 0 or 1
                9. 
              . HDL( hdl ) ///
               10. 
              . CHOL( chol ) ///
               11. 
              . [ OPTimal///
               12. 
              . REPLace SUFFix(str) *]
               13. 
              . 
              . 
              . marksample touse 
               14. 
              . markout `touse' `v' `sbp' `hdl' `chol' `male' `trhtn' `smoke' `diab' 
               15. 
              . 
              . 
              . qui count if `touse' 
               16. 
              . if r(N) == 0 error 2000 
               17. 
              . 
              . 
              . /* check to ensure binary variables contain 0 or 1 */
               18. 
              . foreach v in  `male' `trhtn' `smoke' `diab' {
               19. 
              . capture assert inlist(`v', 0, 1) if `touse' 
               20. 
              . if _rc { 
               21. 
              . di as err "`v' contains values other than 0 or 1" 
               22. 
              . exit 498 
               23. 
              . } 
               24. 
              . } 
               25. 
              . 
              . 
              . /* drop variables if option "replace" is chosen */
               26. 
              . if "`replace'" != "" {
               27. 
              . local framvars : char _dta[framvars`suffix'] 
               28. 
              . if "`framvars'" != "" {
               29. 
              . foreach f of local framvars { 
               30. 
              . capture drop `f' 
               31. 
              . }
               32. 
              . }
               33. 
              . }
               34. 
              . 
              . 
              . quietly {
               35. 
              . 
              . 
              . tempvar risk 
               36. 
              . 
              . 
              . gen `risk' =.
               37. 
              . 
              . 
              . replace `risk' = ln(`age') * 3.06117 + ln(`sbp') * 1.93303 + ln(`chol') * 1.1237 + ln(`hdl') * -0.93263 + `smoke' * 0.65451 + `diab' * 0.57367 if `male' == 1 & `trhtn' == 0 & 
              > `touse'
               38. 
              . 
              . 
              . replace `risk' = ln(`age') * 3.06117 + ln(`sbp') * 1.99881 + ln(`chol') * 1.1237 + ln(`hdl') * -0.93263 + `smoke' * 0.65451 + `diab' * 0.57367 if `male' == 1 & `trhtn' == 1 & 
              > `touse'
               39. 
              . 
              . 
              . replace `risk' = ln(`age') * 2.32888 + ln(`sbp') * 2.76157 + ln(`chol') * 1.20904 + ln(`hdl') * -0.70833 + `smoke' * 0.52873 + `diab' * 0.69154 if `male' == 0 & `trhtn' == 0 &
              >  `touse'
               40. 
              . 
              . 
              . replace `risk' = ln(`age') * 2.32888 + ln(`sbp') * 2.82263 + ln(`chol') * 1.20904 + ln(`hdl') * -0.70833 + `smoke' * 0.52873 + `diab' * 0.69154 if `male' == 0 & `trhtn' == 1 &
              >  `touse'
               41. 
              . 
              . 
              . gen risk10`suffix' =.
               42. 
              . 
              . 
              . replace risk10`suffix' = 1 - 0.88936^exp(`risk'- 23.9802) if `male' == 1 & `touse'
               43. 
              . 
              . 
              . replace risk10`suffix' = 1 - 0.95012^exp(`risk'- 26.1931) if `male' == 0 & `touse'
               44. 
              . 
              . 
              . 
              . 
              . if "`optimal'" !="" { 
               45. 
              . 
              . 
              . tempvar optrisk
               46. 
              . 
              . 
              . gen `optrisk' =.
               47. 
              . 
              . 
              . replace `optrisk' = ln(`age') * 3.06117 + ln(110) * 1.93303 + ln(160) * 1.1237 + ln(60) * -0.93263 if `male' == 1 & `touse'
               48. 
              . 
              . 
              . replace `optrisk' = ln(`age') * 2.32888 + ln(110) * 2.76157 + ln(160) * 1.20904 + ln(60) * -0.70833 if `male' == 0 & `touse'
               49. 
              . 
              . 
              . gen optrisk10`suffix' =.
               50. 
              . 
              . 
              . replace optrisk10`suffix' = 1 - 0.88936^exp(`optrisk'- 23.9802) if `male' == 1 & `touse'
               51. 
              . 
              . 
              . replace optrisk10`suffix' = 1 - 0.95012^exp(`optrisk'- 26.1931) if `male' == 0 & `touse'
               52. 
              . 
              . 
              . }
               53. 
              . 
              . 
              . local framvars risk10`suffix' optrisk10`suffix'
               54. 
              . char def _dta[framvars`suffix'] "`framvars'" 
               55. 
              . 
              . 
              . } // close quietly loop
               56. 
              . 
              . 
              . end

              Comment


              • #8
                Not really. You aren't adding information. Defining a program is needed before you can run it, but it is not the same as running it. I can define a program

                Code:
                program four
                    di 2 + 2
                end
                but that is not running it. I have to type

                Code:
                four
                and I should then see

                Code:
                4
                as output.

                In your case you are not telling the program about your data. It doesn't automatically look at your data, any more than I type 2 and 2 in adjacent cells in Excel, Excel will guess that i want it to add those numbers.

                Comment


                • #9
                  I'm sorry for my confusion. I thought I had defined my data at the top where I said #
                  Code:
                  program framingham
                  . I'm not sure what else I need to do to define it?
                  Last edited by Joe Tuckles; 02 Jul 2018, 08:38.

                  Comment


                  • #10
                    Originally posted by Joe Tuckles View Post
                    I'm sorry for my confusion. I thought I had defined my data at the top where I said #
                    Code:
                    program framingham
                    . What more do I need to do to define it?
                    Hmm ... perhaps there is some confusion here.

                    Your data are the patients you have. We hope that they have variables indicating gender, age, cholesterol, diabetes and smoking status, whatever TRhtn is, etc. Your data would be in, we hope, the Stata .dta file you opened (or you can import from an Excel file, a csv file, etc). You can actually post an extract of your data on the forum using the command -dataex- as detailed in my signature, but you should of course consider HIPAA regulations or whatever regulations apply in your country if this is actual patient data. We are assuming you already have your data imported. If you don't, then you need to do this - for example, if it was a csv file, you can use the import function in the drop down menu (file -> one of the import options). If it was a SAS file, then ask your SAS programmer very nicely to translate it to Stata (if you can't do this, there is a workaround using R, which is free, or you can use the commercial Stat Transfer software).

                    Separately from your data, the syntax you posted defines a program in Stata. You can open your do file editor, enter that block of code, and run the whole thing. You'll see in the command window that all those things got entered - in fact, your post in #7 gave us Stata's response to you doing exactly this. It is merely reflecting in the command window that it accepts your syntax and it has it in memory - for your current session, you now have a program called -framingham- that you would treat like any Stata command. So, you type something like

                    Code:
                    framingham, Male(male) AGE(age) SBP(systolic_bp) blahblahblah
                    You have to follow the capitalization in the code you entered, so you type Male, AGE, SBP. You put the variable names in the brackets. You may have to recode the variables in your study, e.g. if you have a variable called gender that has a value of 0 for male and 1 for female, you need to generate a new variable:

                    Code:
                    recode gender (0 = 1) (1 = 0), gen(male)
                    Now, you did not have to enter this program to define the Framingham score. You could have just copied its internal syntax, then modified it to accommodate your own variable names.

                    Does this perhaps make more sense?
                    Be aware that it can be very hard to answer a question without sample data. You can use the dataex command for this. Type help dataex at the command line.

                    When presenting code or results, please use the code delimiters format them. Use the # button on the formatting toolbar, between the " (double quote) and <> buttons.

                    Comment


                    • #11
                      Thank you so much for your help. I am a complete novice so really appreciate it all.

                      Just to confirm - yes I have patients all of whom have variables indicating gender, age, smoking etc. The variables are in stata right now after I imported them from an excel file.

                      I understand from entering this syntax I have now defined a program. Hopefully this will allow me to generate a new variable which gives the percentage risk for each patient (based on the current defined variables and the various calculations I have copied into the syntex on previous posts). Has the program I have defined allowed me to do this? Because I cannot currently see how to run the syntax and generate this variable. I don't think I need to recode any of my current variables. Currently 1=male and 0=female, 1=yes, 0=no.

                      Now, you did not have to enter this program to define the Framingham score. You could have just copied its internal syntax, then modified it to accommodate your own variable name
                      . Could you possibly expand on this? Thanks
                      Last edited by Joe Tuckles; 02 Jul 2018, 09:15.

                      Comment


                      • #12
                        Maybe this well help - https://econpapers.repec.org/softwar...de/s457997.htm.

                        This is what I am trying to do. The bottom link has sample data. Can anyone see where I am going wrong?

                        Thanks

                        Comment


                        • #13
                          Ah! I didn't realize that this was a program from Statistical Software Components (SSC). As the FAQ mentions, if you're using a user-written package, it helps to mention where it's from.

                          This matters because SSC has quasi-official status. That term is a vast oversimplification. What I mean is that, as the very site you linked mentions, you can install the -framingham- program from within your own Stata command window by typing:

                          Code:
                          ssc install framingham
                          Of course you can open your do file editor, then copy the entire .ado file, then run it. However, trust me on this, go restart Stata, then type the command above into your browser. Then you can just run the program. The program syntax will be documented nicely in the help file, which you can access by typing

                          Code:
                          help framingham
                          The help file even provides a worked example using the stock dataset called framingham.dta, which will load automatically (pretty sure it's copied to your hard drive as part of the installation process) when you type

                          Code:
                          use framingham
                          Side note: everything I said about capitalization in my earlier post is wrong. I forgot something about how .ado files work. That said, in your real data, you use the options (the things after the comma in the help file) to tell the program which of your variables does what.

                          Ignore what I said about copying the program internal syntax into a do file.
                          Be aware that it can be very hard to answer a question without sample data. You can use the dataex command for this. Type help dataex at the command line.

                          When presenting code or results, please use the code delimiters format them. Use the # button on the formatting toolbar, between the " (double quote) and <> buttons.

                          Comment

                          Working...
                          X