Debugging -solved-


#1

Hi all,

I'm pretty new to R, and need some help in debugging this code. I got three errors that i couldn't find solutions online.

  1. Error: unexpected symbol in "while (sqrt((SA-IT1)^2 + (V0-IT2)^2) > 0.1*(1+sqrt(IT1^2 + IT2^2)) and"
  2. Error in VA/VE : non-numeric argument to binary operator.
  3. Error: unexpected '}' in "}" .

Any help is much appreciated! Many thanks for your time

The code:

library(foreign)

#Solving nonlinear system of two equations following newton's | broyden's starting values.

VE = 47440291 
SE = 0.02396919
#SO = 4740291 
#sigmaS = 0.02396919
D= 33404048
r=2.32
T-t = 1

#SO = 4740291 
SA = 0.02396919

a = c(VE,SE)

fnewton <- function(x) {
  y <- numeric(2)
  d1 = (log(x[1]/D) + (r + x[2]^2/2)*T)/x[2]/sqrt(T)
  d2 = d1 - x[2]*sqrt(T)
  y[1] <- VE - (x[1]*pnorm(d1) - exp(-r*T)*D*pnorm(d2))
  y[2] <- SE * VE - pnorm(d1)*x[2]*x[1]
  y
}

#nleqslv(c(VE,SE), fnewton, control=list(btol=.01), method ="Broyden") \$x

nleqslv(c(VE,SE), fnewton, control=list(btol=.01), method ="Broyden")

nleqslv(c(VE,SE), fnewton, control=list(btol=.01), method ="Newton")

#Now, the iteration

V0 = 4740291
sigmaV = 0.02396919

#counter = 1
D1 <- function(V0, D, r, sigmaV, T)
{(log(V0/D) + (r + sigmaV^2/2)*T)/sigmaV/sqrt(T)}
D2 <- function(d1,sigmaV,T) {d1-sigmaV*sqrt(T)}
#SA = 0.02396919
f1 <- function(Va)
{Va*pnorm(D1(Va,D,r,SA,1)) - exp(-r)*D*pnorm(D2(D1(Va,D,r,SA,1), SE,1))-VE}
f2 <- function(Sa) {VA/VE*pnorm(D1(VA,D,r,Sa,1))*Sa -SE}
IT1 <- VE; 
IT2 <- SE; 
counter <- 1
while (sqrt((SA-IT1)^2 + (V0-IT2)^2) > 0.1*(1+sqrt(IT1^2 + IT2^2)) and (counter<1000)) {
  SA <- IT2
  IT1 <- uniroot(f1,c(0,VE*100))
  VA <- IT1 
  IT2 <- uniroot(f2,c(0,SE*100))
  counter <- counter + 1
}

``

#2

Without a sample of the data you're using, we can't run your code and actually see what's going wrong (search around the forum for "reproducible example" or "reprex" and you can see examples).

However these look mostly like syntax errors, so I think I can point you in the right direction:

  1. "and" is not a standard binary operator in R. I believe you want to use & or &&
  2. This error means that, at the time of execution, either VA or VE is non-numeric. Parse through the flow of your program and find the spot where one or both goes from number to not-a-number
  3. Somewhere your brackets aren't matching. Are you using a text editor that does bracket and paren highlighting? If you aren't already using Rstudio, you should download it (this is the Rstudio forum afterall). It makes debugging stuff like this very easy--mouse over one bracket/paren/etc... and it will highlight its pair for you.

In general I'd recommend running through this type of thing line by line so you can see exactly where the errors are. And then if you don't understand the errors you can often google the exact error text and, likely as not, someone has already explained what it means. Good luck!

edit: oh! And I'm not precisely sure what you mean by "I got three errors that i couldn't find solutions online", but, unless you've stumbled across a known bug in a (well used) package, you won't find your exact bug already explained on StackOverflow or here. You will often find a similar bug / a similar error message, and it can help you understand--in a general sense--what's going on. But you're almost certainly going to have to dig into your code and find out exactly what's happening yourself. It happens to all of us, so it's a good skill to develop.


#3

I'm continuing debugging this, many thanks for the helpful insights.