Discussion: View Thread

  • 1.  converting Python code to R code/manuscript

    Posted 05-24-2024 10:27
      |   view attached
    I'd appreciate some help converting this python script ( in a .txt format, this system does not accept a .PY) to R. I have never used python before. I also have the .PY script and can separately email that if needed..   . I am prepared to learn enough python  to understand and guide the conversion  to R or I may be able to do the conversion with assistance from a python expert.  I regularly use R. My co-author and colleague wrote the python code. My co-author is also graduate student with homework, classes and exams and has limited availability at the present time.   This is one of two separate simulations we prepare in the manuscript. I wrote the second simulation in R. Only the python code needs to be converted. both simulations need to be in the manuscript.    I recognize The attached python script (in a .txt format ) has modest documentation. Fortunately the manuscript was originally my idea and I know all the background to this simulation.   .  Understandably, for a manuscript the scripts for simulations must be in the same language - R.  
    • I will credit  anyone assisting with converting python to R as appropriate.
    • Importantly My co author and I will be re-submitting a revision of manuscript before the end of the current month (may 30 2024).
    --- I will be glad to provide as much background explanation of the manuscript and the objective of the simulation in the attached python  as needed. I prefer to have that discussion offline - by telephone or zoom or email. My email chris.barker@barkerstats.com
    And I am temporarily living in Berlin Germany and so there are likely to be timezone differences for organizing zoom or phone calls. 


    ------------------------------
    Chris Barker, Ph.D.
    Past Chair
    Statistical Consulting Section
    Consultant and
    Adjunct Associate Professor of Biostatistics
    www.barkerstats.com


    ---
    "In composition you have all the time you want to decide what to say in 15 seconds, in improvisation you have 15 seconds."
    -Steve Lacy
    ------------------------------

    Attachment(s)

    txt
    python.txt   2 KB 1 version


  • 2.  RE: converting Python code to R code/manuscript

    Posted 05-24-2024 10:34

    Chris - if you haven't tried ChatGPT for code conversion, I recommend it. For all it's faults, code translation is one of the tasks where ChatGPT genuinely excels. It doesn't always get it right the first time, but it's remarkably good at answering follow up questions about error messages.



    ------------------------------
    David P Miller
    Principal
    DPM Biostatistics LLC
    ------------------------------



  • 3.  RE: converting Python code to R code/manuscript

    Posted 05-24-2024 10:43
    Perhaps you may consider calling the python code in R using the reticulate package- no need for conversion. Our group does that regularly. We write our functions in Python and make an R-package of the codes using the reticulate package so that non-users of R can still use our methods.

    Sandra
    Assistant Professor of Biostatistics
    McKnight Land-Grant Professor
    Division of Biostatistics and Health Data Science
    Affiliated Faculty, Data Science Initiative
    College of Science and Engineering
    University of Minnesota
    Phone: 612-625-9142









  • 4.  RE: converting Python code to R code/manuscript

    Posted 05-27-2024 03:03

    Thank you. confirming yes I got the reticulate package to work on running python code. Before I did that I successfully installed python and ran it under DOS. Based on my reading, it also (started to) installed "pip", and I abandoned installing "anaconda" - as it was 5Gig and i did not have that much space free on my laptop.  A plus ot reticulate. I downloaded and successfully installed pip. However, I abandoned trying to modify a windows environment variable so thtat I could invoke pip at the dos level. Fortunately Reticulate seems to take care of that part of the installation of pip.

    running the python script required that I install "numPy". That succeeded and   I could execute the lines of code 

    py_install("numPy")
    np <- import("numpy", convert = FALSE)

    az <- np$array(c(1:4))
    sum <- az$cumsum()
    py_to_r(sum)



    ------------------------------
    Chris Barker, Ph.D.
    Past Chair
    Statistical Consulting Section
    Consultant and
    Adjunct Associate Professor of Biostatistics
    www.barkerstats.com


    ---
    "In composition you have all the time you want to decide what to say in 15 seconds, in improvisation you have 15 seconds."
    -Steve Lacy
    ------------------------------



  • 5.  RE: converting Python code to R code/manuscript

    Posted 05-27-2024 20:42

    I only recommend translating code when you're proficient in both languages.  AI is imperfect, so you have to understand what the original code is doing and be able to verify that the translated code does the same thing.  I've seen some creative translations made by AI.  Thus, I recommend Sandra's suggestion of running the Python code as is using R's reticulate or similar package.



    ------------------------------
    Chuck Coleman
    Mathematical Statistician
    US Census Bureau
    ------------------------------



  • 6.  RE: converting Python code to R code/manuscript

    Posted 05-31-2024 09:15

    thx. I boldly forged ahead and translated. :) 

     And the translated code had some sophisticated (to me) R commands. chatgpt 'figured out" to use an sapply() statement. however it did have a mistake rendering that step in the code does not run. The code gets random values from a bernoulli. Chatgpt converted to the  rbinom() . And the R code got an error because it appears to be giving the wrong values to one of the function parameters.

    As another person identified a problem in the implementation of the histogram. I returned all the errors for fixing by my co-author. 

    -thank you all for the replies



    ------------------------------
    Chris Barker, Ph.D.
    Past Chair
    Statistical Consulting Section
    Consultant and
    Adjunct Associate Professor of Biostatistics
    www.barkerstats.com


    ---
    "In composition you have all the time you want to decide what to say in 15 seconds, in improvisation you have 15 seconds."
    -Steve Lacy
    ------------------------------



  • 7.  RE: converting Python code to R code/manuscript

    Posted 05-28-2024 16:09

    Hi Chris, I took a quick look, as the python code was very short. I'll email you offline.



    ------------------------------
    Aaron Rendahl, Ph.D.
    Associate Professor of Statistics and Informatics
    College of Veterinary Medicine, University of Minnesota
    ------------------------------