I am trying to solve a quite straight forward non linear least squares problem where the formulation takes 'so to say' a vector of zeroes as dependent variable because I am only interested in the parameters of the minimization for a prediction exercise. However, whatever I do with this algorithm, the code keeps giving me the following error: Error in str2lang(x) : <text>:2:0: unexpected end of input 1: ~ ^
EDIT: The suggestion to replace the LHS of the equation with a specified variable seems to resolve the parser issue. However, I now get the following error:
Error in nlsLM(formula = y ~ fit(params, T_k_IV_matrix), start = x0, lower = lb, :
parameters without starting value in 'data': params
Does anyone know where I should be looking to solve this problem?
Here is the important part of the code I use:
fit <- function(params, T_k_IV_matrix) {
T <- T_k_IV_matrix[, "tenor"]
k <- T_k_IV_matrix[, "k"]
IV <- T_k_IV_matrix[, "impl_volatility"]
vt <- params[1]
mt <- params[2]
wt <- params[3]
nt <- params[4]
rhot <- params[5]
(1/4) * exp(-2 * nt * T) * (wt^2) * (T^2) * (IV^4) +
(1 - 2 * exp(-nt * T) * mt * T - exp(-nt * T) * wt * rhot *
sqrt(vt) * T) * (IV^2) -(vt + 2 * exp(-nt * T) * wt * rhot *
sqrt(vt) * k + exp(-2 * nt * T) * (wt^2) * (k^2))
}
for (i in 1:length(grouped_data)) {
T_k_IV_matrix <- data.table(
tenor = c(110, 110, 82, 138, 82, 173, 138, 173, 173, 47),
k = c(0.1629164648, 0.4307829161, -0.3811180797, 0.0009384402,
-0.1823215568, -0.0219789067, -0.0988005847, -0.4187103349,
-0.0737730947, -0.0527505654),
impl_volatility = c(0.950298, 1.106709, 0.732060, 0.740338,
0.638142, 0.525060, 0.689398, 0.652785, 0.955989, 0.962296)
)
y = rep(0, nrow(T_k_IV_matrix))
x0 = c(0.04, 0.1, 0.5, 0.3,-0.8)
lb = c(0.001,-Inf,0.001,0.001,-.999)
ub = c(Inf,Inf,Inf,Inf,.999)
result <- nlsLM(
formula = y ~ fit(params, T_k_IV_matrix),
start = x0,
lower = lb,
upper = ub
)
params[[i]] <- coef(result)
}