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:
- 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.
- 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!!