Stefano Sofia wrote:
Dear Splus users,
I have got some problems about input parameters for a Splus personal
procedure.
If I call the procedure below reported
myprocedure(-38.94376,0.001842975)
there is an error in "smallg(Y1)" which says:
Error in small(Y1): Object "E7" not found.
Why?
E7 and V7 are two scalars.
Defining E7 and V7 as
array(NA, dim = c(1, 1)),
mode(E7) is "numeric", mode(V7) is "numeric".
I encounter the same problem if I define E7 and V7 as matrices of dimesion 1
by 1
or even if I do not define them at all.
Why smallg can't be evaluated for eta=E7?
If I use the internal procedure without E7 and V7 but with pure numbers, i.e.
internal(-38.94376,0.001842975,1)
everything works fine. Why?
I am using Splus4 with W XP.
grateful for your help
Stefano
This is a scoping issue. Replace function internal with:
internal <- function(normalmean, normalvariance, state){
alpha <- 0.01
if(state == 0) {
smallg <- substitute(function(eta, mu, sigma){
(dnorm(eta, mean = mu, sd = sigma) * 1)/(1 + exp(alpha * eta))})
} else {
smallg <- substitute(function(eta, mu, sigma){
(dnorm(eta, mean = mu, sd = sigma) * exp(alpha * eta))/(1 +
exp(alpha * eta))})
}
repeat {
# STEP 1: SAMPLE A POINT Y1 FROM bigg(Y)
Y1 <- rnorm(1, mean = normalmean, sd = sqrt(normalvariance))
# STEP 2: SAMPLE A UNIFORM(0,1) RANDOM VARIABLE U
U <- runif(1, min = 0, max = 1)
# STEP 3: IF U <= g(Y1)/G(Y1) ACCEPT Y1;
# REPEAT THIS LOOP UNTIL ONE Y IS ACCEPTED
Y1.num <- smallg(Y1, normalmean, sqrt(normalvariance))
Y1.den <- dnorm(Y1, mean = normalmean, sd = sqrt(normalvariance))
if(U <= Y1.num/Y1.den) {
sampledvalue <- Y1
break
}
}
result <- sampledvalue
result
}
|