0

I have got this error message: Error in str2lang(x) : :2:0: unexpected end of input 1: ~ ^ and have been trying for two days to find what the problem was. I really need your help guys!! My code is built in two stages:

  1. a function that returns the right-hand-side for my non-linear least-square regression. The parameters to estimate through the nls are f, m, mu. Xlags is a matrix with the lags of variable x. In the first column there are the x, second column the second lag, and so on.
  2. the non-linear least square regression where variable x is on the left side and the result of the function on the right side.

If you could help on that issue that would be amazing. Available for additional information.


XfitHV <- function(Xlags, R, Ff, M, Mu) {
  
  # Function to dermine the fraction of aggregate tolerance at a certain time (nominator)
  density_eq <- function(x) {
    output <- 1/(x*0.73*sqrt(2*pi))*exp(-((log(x)-1.84)^2)/(2*0.73^2))
    return(output)
    }
  
  # Aggregate risk tolerance (used then for the fraction of aggregate tolerance)
  aggr_risk_tolerance <- integrate(density_eq, lower = 0, upper = Inf)$value
  
  ## Creation of null vectors for computations ##
  uf<- numeric(10)
  um <- numeric(10)
  for(t in 1:10){
  uf[t] <- -(Xlags[t,1]-Ff*Xlags[t,3])^2
  um[t] <- -(Xlags[t,1]-Xlags[t,3]-M*(Xlags[t,3]-Xlags[t,4]))
  }
  
  # 2 vector of the weighted average of both heuristics' past performance at each period
  avg_uf <- numeric(255)
  avg_um <- numeric(255)
  
  avg_uf[1] <- mean(uf)
  avg_um[1] <- mean(um)
  
  # 2 vector of the weighted squared average of both heuristics' past performance at each period
  sqr_avg_uf <- numeric(255)
  sqr_avg_um <- numeric(255)
  
  sqr_avg_uf[1] <- avg_uf[1]^2
  sqr_avg_um[1] <- avg_um[1]^2
  
  # 2 vector of the weighted variance of both heuristics' past performance variance at each period
  var_f <- numeric(255)
  var_m <- numeric(255)
  
  var_f[1] <- var(uf)
  var_m[1] <- var(um)
  
  # Vector fraction of aggregate tolerance at a certain time
  fi <- numeric(254)
  
  # Vector of the risk aversion coefficient for which the mean-variance performance of the forecasting heuristics are equal in period t
  risk_avers_coef <- numeric(255)
  
  risk_avers_coef[1] <- 2*(avg_uf[1]-avg_um[1])/(var_f[1]-var_m[1])
  
  
  #### Loop to build the value of the vector of fraction of aggregate risk aversion
  for (t in 1:254) {
    
    # Function 19 of the paper
    if((var_f[t] == var_m[t]) & (avg_uf[t] == avg_um[t])) fi[t] <- 0.5
    else{if((var_f[t] >= var_m[t]) & (avg_uf[t] < avg_um[t])) fi[t] <- 0
    else{if((var_f[t] <= var_m[t]) & (avg_uf[t] > avg_um[t])) fi[t] <- 1
    else{if((var_f[t] < var_m[t]) & (avg_uf[t] <= avg_um[t])) fi[t] <- integral(density_eq, risk_avers_coef[t], Inf)/aggr_risk_tolerance
    else{if((var_f[t] > var_m[t]) & (avg_uf[t] >= avg_um[t])) fi[t] <- integral(density_eq, 0, risk_avers_coef[t])/aggr_risk_tolerance
    }}}}
    
    # Function 20 of the paper; Past weighted average performance for each period
    avg_uf[t+1] <- Mu*avg_uf[t]+(1-Mu)*(Xlags[11+t,1]-Ff*Xlags[11+t,3])^2
    avg_um[t+1] <- Mu*avg_um[t]+(1-Mu)*(Xlags[11+t,1]-Xlags[11+t,3]-M*(Xlags[11+t,3]-Xlags[11+t,4]))^2
    
    # Equation 20 of the paper; Past weighted average squared performance for each period
    sqr_avg_uf[t+1] <- Mu*sqr_avg_uf[t]+(1-Mu)*(Xlags[11+t,1]-Ff*Xlags[11+t,3])^4
    sqr_avg_um[t+1] <- Mu*sqr_avg_um[t]+(1-Mu)*(Xlags[11+t,1]-Xlags[11+t,3]-M*(Xlags[11+t,3]-Xlags[11+t,4]))^4
    
    # Equation 14 of the paper; Past weighted average performance variance for each period
    var_f[t+1] <- sqr_avg_uf[t+1]-avg_uf[t+1]^2
    var_m[t+1] <- sqr_avg_um[t+1]-avg_um[t+1]^2
    
    # Equation 18: Risk aversion coefficient
    risk_avers_coef[t+1] <- 2*(avg_uf[t+1]-avg_um[t+1])/(var_f[t+1]-var_m[t+1])
    
  }#end of loop
  
  # Computation of the RHS of the regression function
  rhs <- 1/R*(fi*Ff*Xlags[12:265,2]+(rep(1,254)-fi)*(Xlags[12:265,2]+M*(Xlags[12:265,2]-Xlags[12:265,3]))) # what is returned by the function
  return(rhs)
}#end of function

####### Build Matrix Xlags ######

T = length(x)

L=4 # Number of lags needed 

xlags = matrix(nrow = T+1, ncol = L) # Create matrix

# Fill the matrix
for(j in (L+1):(T+1)){
  for (l in 1:L) {
    xlags[j,l] = x[j-l];
  }
}

# Remove the first NA row of the matrix
xlags <- xlags[(L+1):(T+1), 1:L]

# Discount factor
r <- 1+i

# Nonlinear Least Square Regression in order to find coefficients f, m & mu
nlmod <- nls(xlags[12:265,1] ~ XfitHV(xlags,r,f,m,mu),
             start  = list(f=0.4, m=1.1, mu=0.25), 
             lower  = list(f=0.01, m=0.01, mu=0.01), 
             upper  = list(f=0.99, m=10, mu=0.9),
             algorithm = "port",
             trace  = T,
             control= nls.control(minFactor=1/10000, maxiter = 100, warnOnly = T)
)```

Thank you very much for your replies!!
  • Hi, welcome to Stack. Ok, so you provide a LOT of information, and at the same time a lot of things are missing. At the end of the day, it is about the function where you get the error from, and the input that you give to that function. In this case, use `traceback()` after getting the error to see where you get the error. I think the problem is in `nls` and the way you construct your formula. Next, look at what `xlags[12:265, 1]` and `XfitHV(xlags,r,f,m,mu)` look like. It would be helpful if you could give some example data or at least `str(xlags[12:265, 1])` and `str(XfitHV(xlags,r,f,m,mu))` – slamballais Aug 06 '21 at 06:27
  • Dear @slamballais, thank you very much for your reply. I replied to you in the answers. Best – Tristan Veuthey Aug 06 '21 at 20:03

1 Answers1

0

thank you very much for you reply. Yes I am new to this cool platform so I did not know how to explain my problem. As requested by you I have run traceback() and obtain the following that I do not understand too:

5: str2lang(x)
4: formula.character(object, env = baseenv())
3: formula(object, env = baseenv())
2: as.formula(paste("~", paste(vNms, collapse = "+")), env = environment(formula))
1: nls(xlags[12:265, 1] ~ XfitHV(xlags, r, f, m, mu), start = list(f = 0.4, 
       m = 1.1, mu = 0.25), lower = list(f = 0.01, m = 0.01, mu = 0.01), 
       upper = list(f = 0.99, m = 10, mu = 0.9), algorithm = "port", 
       trace = T, control = nls.control(minFactor = 1/10000, maxiter = 100, 
           warnOnly = T))

I know that my mistake comes from nls but I do not understand what I do wrong. I got inspiration from explanations on the internet but I still don't understand what is wrong.

Regarding the inputs of the function, when I run xlags in the command I get:

 >         [,1]         [,2]         [,3]         [,4]
 > [1,] -245.2166209 -215.3362201 -183.7168175 -190.0689974
  >[2,] -234.3886574 -245.2166209 -215.3362201 -183.7168175
  >[3,] -245.6797343 -234.3886574 -245.2166209 -215.3362201
  >[4,] -214.5815387 -245.6797343 -234.3886574 -245.2166209
  >[5,] -181.2048908 -214.5815387 -245.6797343 -234.3886574
  >[6,] -181.7299470 -181.2048908 -214.5815387 -245.6797343
  >[7,] -183.5586326 -181.7299470 -181.2048908 -214.5815387
  >[8,] -178.5667028 -183.5586326 -181.7299470 -181.2048908
  >[9,] -156.0660467 -178.5667028 -183.5586326 -181.7299470
 >[10,] -157.1065807 -156.0660467 -178.5667028 -183.5586326
 >[11,] -177.0782509 -157.1065807 -156.0660467 -178.5667028
 >[12,] -182.5230812 -177.0782509 -157.1065807 -156.0660467`






For command str(XfitHV(xlags,r,f,m,mu)), I obtain: 

`For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
For infinite domains Gauss integration is applied!
 num [1:254] -174 -179 -173 -163 -135 ...`