Jump to content
Sign in to follow this  
de la Mancha

Converting code source in R to Delphi

Recommended Posts

I try to convert source code in R to Delphi code...

Thank you in advance for your help...

 

nparACT_IS = function(data_hrs, mean_all, bin_hr){
    ## ---- IS numerator calculation
    result_ISnum <- matrix(NA, nrow = 24)
    n <- nrow(data_hrs)
    p <- 1440/bin_hr
    for (h in 1:24){
      s <- ceiling(n/p)
      data_hrs3 <- data_hrs
      data_hrs3[s*p] <- NA
      data_hrs3 <- matrix(data_hrs3)
      hrlydat <- data_hrs3[c(seq(h,nrow(data_hrs3),24)),]
      hrlymean <- mean(hrlydat, na.rm = T)
      x <- (hrlymean-mean_all)^2
      result_ISnum[h,] <- x
    }
    ISnum <- sum(result_ISnum)
    ISnumerator <- n*ISnum
        ## ---- IS denominator calculation
    result_ISdenom <- matrix(NA, nrow = n)
    for (j in 1:n){
      y <- ((data_hrs[j,]-mean_all)^2)
      result_ISdenom[j,] <- y
    }
    ISdenom <- sum(result_ISdenom)
    ISdenominator <- p*ISdenom
    ## -----------------------------
    IS <- round(ISnumerator/ISdenominator, digits = 2)
    return_IS <- c(IS, ISdenom, n, p)
    return(return_IS)
  },

Share this post


Link to post

Here's what to do.  First of all you need to understand the R code.  You need to know what it's purpose is, and how it achieves it.  You will need a clear understanding of what data types are expected to be passed to, and returned from the function.  You may need to talk to the author of the code, or learn enough R to work it out yourself.  Once you have a clear understanding then you will be in a good place to write an equivalent function in Delphi.

 

Good luck.

  • Like 4

Share this post


Link to post

No problem, I wanted to save time. It's not hard for me R. The program is free. I know the author. Good luck and keep your advice to yourself.

  • Like 1
  • Sad 1

Share this post


Link to post

@de la Mancha- you asked and got a relevant answer from someone known for doing heavy math with Delphi. No reason to be rude.  

  • Like 6

Share this post


Link to post
Posted (edited)
2 hours ago, Lars Fosdal said:

@de la Mancha- you asked and got a relevant answer from someone known for doing heavy math with Delphi. No reason to be rude.  

To be fair: David also tends to be - lets say "terse" - in his answers. That can also be interpreted as rude, in particular by people for whom English is a foreign language.

Edited by dummzeuch
  • Like 4

Share this post


Link to post
14 minutes ago, dummzeuch said:

To be fair: David also tends to be - lets say "terse" - in his answers. That can also be interpreted as rude, in particular by people for whom English is a foreign language.

Perhaps "terse" can be understood as rudeness. But for me personally, the words "keep your advice to yourself" mean unambiguously rudeness. I don't know, maybe I was brought up wrong.

  • Like 7

Share this post


Link to post

I don't know R at all. But looking at your code it's all fairly clear what's going on (<- is the same as delphi := [took me 30s to work this out]). Your key challenge is to make sure you select the correct types for the variables used.

I am a strong advocate of writing readable (and hence supportable) code. I would respectfully suggest that you use longer, more meaningful names for the function and

for the variables it uses. This would make it much much easier to see what the function does and how it does it.

Share this post


Link to post

Thank you for your help. I've written to the author this morning and hope she reply to me. This function compute the Intradaily Variability (IV)

Share this post


Link to post
7 hours ago, dummzeuch said:

To be fair: David also tends to be - lets say "terse" - in his answers. That can also be interpreted as rude, in particular by people for whom English is a foreign language.

What else could be said for post of "I have a problem pls solve it completely for me" kind? If the one who needs solution does zero for the problem, why someone else should do more?

  • Like 2

Share this post


Link to post
On 3/28/2022 at 9:50 AM, David Heffernan said:

First of all you need to understand the R code.  You need to know what it's purpose is, and how it achieves it.  You will need a clear understanding of what data types are expected to be passed to, and returned from the function.  You may need to talk to the author of the code, or learn enough R to work it out yourself.  Once you have a clear understanding then you will be in a good place to write an equivalent function in Delphi.

 

Reading between the lines means learning actuarial science as side effect you learn how use various tools perhaps getting tools paid for as intern. 

 

The time spent porting would be better used becoming a Subject Matter Expert that way you know if the output is reasonable or just pretty. 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×