Coversion of R-code to VBA -- UDF Error

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
349
Office Version
  1. 2003 or older
Platform
  1. Windows
I have a code in R which I am trying to convert to Excel VBA.

The R-code is -

Code:
[FONT=Courier][SIZE=3][FONT=Courier][SIZE=3]draw.zeta<-function(nrep,alpha){
if (alpha<=1){stop("alpha must be greater than 1!\n")}
zeta<-numeric(nrep) ; for (i in 1:nrep){
index<-0 ; while (index<1){
u1<-runif(1) ; u2<-runif(1)
x<-floor(u1^(-1/(alpha-1))) ; t<-(1+1/x)^(alpha-1)
w<-x<(t/(t-1))*(2^(alpha-1)-1)/(2^(alpha-1)*u2)
zeta[i]<-x ; index<-sum(w)}}
zeta}
[/SIZE][/FONT][/SIZE][/FONT]

And, the corresponding VBA version, which is not executing, is -

Code:
Public Function Zipf(Optional ByVal alpha As Double = 3#) As Double
If alpha <= 1# Then Exit Function
Dim index As Double, u1 As Double, u2 As Double, x As Double, t As Double, w As Double
index = 0
While index < 1
u1 = RND()
u2 = RND()
x = WorksheetFunction.Floor(u1 ^ (-1 / (alpha - 1)), 1)
t = (1 + 1 / x) ^ (alpha - 1)
w = x < (t / (t - 1)) * (2 ^ (alpha - 1) - 1) / (2 ^ (alpha - 1) * u2)
Zipf = x
index = index + w
Wend
Zipf = x
End Function

I am not able to figure out the error.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I can get it to run, if I clean it up a little

Code:
Public Function Zipf(ByVal alpha As Double) As Double
    Dim index As Double, u1 As Double, u2 As Double, x As Double, t As Double, w As Double

    If alpha > 1 Then
        index = 0
        While index < 1
            u1 = Rnd()
            u2 = Rnd()
            x = WorksheetFunction.Floor(u1 ^ (-1 / (alpha - 1)), 1)
            t = (1 + 1 / x) ^ (alpha - 1)
            w = x < (t / (t - 1)) * (2 ^ (alpha - 1) - 1) / (2 ^ (alpha - 1) * u2)
            index = index + w
        Wend
    Else
        x = 0    ' or whatever
    End If
    Zipf = x
End Function

Whether it runs correctly is another matter. I'm not familiar with R, but your R function appears to have two input parameters nrep, and alpha. But your VBA function only has alpha. Why? It seems like you are missing a loop of the form

Code:
for i = to to nrep

'do something

next i

This next line
Code:
w = X < (t / (t - 1)) * (2 ^ (alpha - 1) - 1) / (2 ^ (alpha - 1) * u2)

makes no sense to me and I suggest that you revist the conversion from R. In vba

Code:
X < (t / (t - 1))

is a boolean expression, i.e. X is less than (t / (t - 1)) so you are basically multiplying a boolean expression

Code:
w = Some boolean expression  * (2 ^ (alpha - 1) - 1) / (2 ^ (alpha - 1) * u2)

Is that really what you want? I suspect the boolean expression in that equation is going to flip back and forth from 0 and -1 depending on whether the condition is true or false.
 
Upvote 0
May we know what the function is supposed to calculate?
 
Upvote 0
I am calling zipf function in a SUB using a for next loop, which repeats itself for nrep times and populates the random number in a column beep times.
 
Upvote 0

Forum statistics

Threads
1,215,022
Messages
6,122,726
Members
449,093
Latest member
Mnur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top