Juggler_IN
Active Member
- Joined
- Nov 19, 2014
- Messages
- 349
- Office Version
- 2003 or older
- Platform
- Windows
I have a code in R which I am trying to convert to Excel VBA.
The R-code is -
And, the corresponding VBA version, which is not executing, is -
I am not able to figure out the error.
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.