Announcement

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

  • Converting C++ into STATA code

    Hi,

    I should say I am a novice! Is there any simple way of converting C++ coding into something I can use in STATA?

    Thanks

  • #2
    C++ into Mata is a better bet. There being a simple way depends not only on how simple the code is but also on how close it is to Mata.

    Comment


    • #3
      I've never used Mata before. Can I convert C++ into Mata and then into STATA?

      Comment


      • #4
        Does I mean you, knowing no Mata and a little Stata, or some one in general?

        This is a little like asking can I translate Spanish to Italian? The translation is possible and may even be trivial, but it still hinges on a person knowing both languages.

        I don't want to sound negative, but it is really hard to be more definite. The real answer depends on showing us the code.



        Comment


        • #5
          I means me

          This is the code: https://qrisk.org/three/src.php

          Comment


          • #6
            Looking at the code you have linked to, it seems clear that this is a simple risk score calculation implemented from a regression equation. In other words, it should be straight forward to implement in Stata (since that is what you seem to know how to use). So why not try it out yourself?

            Comment


            • #7
              Yes that is correct, it is simple risk score. I am not sure how to go about implementing it in Stata from the coding. Is there anything you can recommend to get me started?
              Thanks

              Comment


              • #8
                When I began using Stata in a serious way, I started - as others here did - by reading my way through the Getting Started with Stata manual relevant to my setup. Chapter 18 then gives suggested further reading, much of which is in the Stata User's Guide, and I worked my way through much of that reading as well. All of these manuals are included as PDFs in the Stata installation and are accessible from within Stata - for example, through Stata's Help menu. The objective in doing this was not so much to master Stata as to be sure I'd become familiar with a wide variety of important basic techniques, so that when the time came that I needed them, I might recall their existence, if not the full syntax, and know how to find out more about them in the help files and manual.

                Chapter 18 of the Stata User's Guide, in particular, will be helpful if your goal is to implement the code as a Stata function. On the other hand, you might just want to implement it within a single program on a single dataset, in which case the effort of creating an ado file with a function may be overkill. In any event, especially since you are relatively new to Stata, it might be best to find an example of data and result created using the C++ code, and transfer the data into Stata and write a simple do-file to implement to translated code, using the variable names used in the data. That way, when your results match those in the example, you can have some confidence that you've understood the C++ and correctly interpreted it in Stata.

                Stata supplies exceptionally good documentation that amply repays the time spent studying it - there's just a lot of it. The path I followed surfaces the things you need to know to get started in a hurry and to work effectively.

                Comment


                • #9
                  Thanks, my goal is to implement the code within a single program on a single dataset, so wasn’t sure which specific documentation to follow/look at to achieve this. Would it be chapter 18? Am I required to write a plugin to achieve this?
                  Last edited by Alex Williams; 09 Nov 2019, 11:31.

                  Comment


                  • #10
                    If this is a prediction from a regression, look at the regress documentation (check the pdf documentation which is more complete than the command line documentation) and regress postestimation. Every Stata estimator includes a documentation chapter and then a postestimation chapter.
                    If this really is a regress then prediction, it would look something like:
                    regress y x
                    predict yhat

                    or using margins (which uses averages for the variables not stated)
                    regress y x sex
                    margins, at(sex=(0 1))

                    Comment


                    • #11
                      Originally posted by Phil Bromiley View Post
                      If this is a prediction from a regression, look at the regress documentation (check the pdf documentation which is more complete than the command line documentation) and regress postestimation. Every Stata estimator includes a documentation chapter and then a postestimation chapter.
                      If this really is a regress then prediction, it would look something like:
                      regress y x
                      predict yhat

                      or using margins (which uses averages for the variables not stated)
                      regress y x sex
                      margins, at(sex=(0 1))
                      Phil has given excellent advice in general, when one is able to estimate their own model. In this case, Alex is working with a published model which is apparently provided as a C++ routine, at least in code.

                      Originally posted by Alex Williams View Post
                      Thanks, my goal is to implement the code within a single program on a single dataset, so wasn’t sure which specific documentation to follow/look at to achieve this. Would it be chapter 18? Am I required to write a plugin to achieve this?
                      No, a plugin is not necessary here. Simple Stata statements would more than suffice, though I would personally prefer to write a simple -program- and stick it in a .do or .ado file.

                      In essence, what you are doing is reproducing the regression model (specifically, the linear predictor portion). In math, this looks something like:

                      y = b0 + b1*x1 + b2*x2 + ... + bn*xn

                      There is also some pre-processing to do involving variable transformations, but those are similarly straightforward.

                      Comment


                      • #12
                        Thanks for your help. I am still lost on how to proceed with this. I don't know how to begin. I have never written a -program- before. Can anyone help me break this down into small steps? I take it the first step is to do some variable transformations. Does anyone know which lines of this C++ code relate to such transformations and how do I interpret it to then do such transformations to my own variables in stata?
                        Thanks

                        Comment


                        • #13
                          Hello,

                          Just wondering if anyone can help me


                          Code:
                          * Example generated by -dataex-. To install: ssc install dataex
                          clear
                          input int(sex age) float(renal sle) double bmi
                          0 67 0 0           31.9096
                          1 56 0 0           34.7353
                          1 56 0 0           34.7353
                          1 45 0 0                 .
                          1 45 0 0                 .
                          1 45 0 0                 .
                          1 45 0 0                 .
                          1 45 0 0                 .
                          0 49 0 0                 .
                          1 41 0 0           25.0675
                          1 41 0 0           25.0675
                          1 62 0 0           27.7767
                          1 62 0 0           27.7767
                          0 56 0 0           22.9929
                          1 46 0 0 32.16190000000001
                          1 60 1 0                 .
                          0 58 0 0           34.0636
                          0 67 0 0           19.6763
                          0 59 0 0           17.5737
                          1 41 0 0            52.228
                          0 56 0 0           26.2721
                          1 52 0 0           29.0698
                          1 52 0 0           29.0698
                          1 52 0 0           29.0698
                          0 68 0 0           27.0312
                          1 62 0 0           27.4018
                          1 58 0 0            27.305
                          1 58 0 0            27.305
                          1 58 0 0            27.305
                          0 44 0 0           40.3854
                          1 63 0 0           19.7378
                          1 53 0 0 35.70430000000001
                          1 53 0 0 35.70430000000001
                          1 69 0 0                 .
                          1 69 0 0                 .
                          1 61 0 0           26.0933
                          0 65 0 0           26.0519
                          0 45 0 0           26.9922
                          1 59 0 0           35.0781
                          1 59 0 0           35.0781
                          0 49 0 0           37.1147
                          0 49 0 0           37.1147
                          1 43 0 0           22.6912
                          1 61 0 0           25.3146
                          1 62 0 0           26.7299
                          1 41 0 0           38.0499
                          0 47 0 0            21.402
                          0 56 0 0           52.1639
                          0 56 0 0           52.1639
                          1 62 0 0           29.0543
                          1 62 0 0           29.0543
                          1 62 0 0           29.0543
                          0 58 0 0           20.9959
                          0 58 0 0           20.9959
                          1 59 0 0           23.8751
                          1 59 0 0           23.8751
                          1 59 0 0           23.8751
                          0 46 0 0           29.7689
                          1 65 0 0           23.1753
                          1 65 0 0           23.1753
                          1 47 0 0           28.4408
                          1 54 0 0           27.9538
                          1 49 0 0           35.1332
                          0 45 0 0           29.7268
                          1 46 0 0            29.075
                          1 46 0 0            29.075
                          0 49 0 0           27.6839
                          0 45 0 0            25.157
                          1 47 0 0           27.6262
                          1 50 0 0           27.5318
                          1 57 0 0           28.7132
                          1 47 0 0           33.0904
                          1 47 0 0           33.0904
                          1 63 0 0           23.4877
                          1 63 0 0           23.4877
                          1 63 0 0           23.4877
                          1 67 0 0           21.2654
                          1 67 0 0           21.2654
                          1 47 0 0           19.9452
                          1 57 0 0           24.9827
                          1 43 0 0           45.8475
                          1 63 0 0           27.2399
                          1 63 0 0           27.2399
                          0 58 0 0           23.2869
                          0 58 0 0           23.2869
                          1 50 0 0           27.9912
                          0 64 0 0 33.32050000000001
                          1 63 0 0           29.5679
                          1 63 0 0           29.5679
                          1 63 0 0           29.5679
                          1 63 0 0           29.5679
                          0 62 0 0           19.8903
                          1 52 0 0           30.2815
                          1 63 0 0             41.07
                          1 63 0 0             41.07
                          1 63 0 0             41.07
                          0 63 0 0           37.2021
                          0 63 0 0           37.2021
                          0 66 0 0           27.8497
                          0 66 0 0           27.8497
                          end
                          label values sex 
                          label def 0 "Female", modify
                          label def 1 "Male", modify

                          Comment

                          Working...
                          X