Ingrid Campos
Ingrid Campos

Reputation: 1

Error in Ops.Date(left, right) : * not defined for "Date" objects

This is an assigment I got for Financial Economics, I'm using yahoo data base for stocks of 5 different corporations, so I cannot modify the data or how it is presented.

What I have to do is to get a portfolio with three different beta and I just keep getting the same message

Error in Ops.Date(left, right) : * not defined for "Date" objects

Here's my code:

portafolio_retorno <- rowSums(returns_data[, 1:5] * as.numeric(ponderaciones))
  modelo_portafolio <- lm(portafolio_retorno ~ returns_data$GSPC)
  return(coef(modelo_portafolio)["returns_data$GSPC"])
}

betas_deseados <- c(0.8, 1.0, 1.2)


ponderaciones_resultados <- matrix(NA, nrow = length(betas_deseados), ncol = 5)


for (i in 1:length(betas_deseados)) {
  objetivo <- function(ponderaciones) {
    ponderaciones <- ponderaciones / sum(ponderaciones)
    return(abs(calcular_beta_portafolio(ponderaciones, returns_data) - betas_deseados[i]))
  }
  
  restricciones <- list(sum = 1)
  
  resultado_optimizacion <- optim(rep(0.2, 5), objetivo, method = "L-BFGS-B", lower = rep(0, 5), upper = rep(1, 5), control = list(fnscale = -1))
  

  ponderaciones_resultados[i, ] <- resultado_optimizacion$par / sum(resultado_optimizacion$par)
}

resultados <- data.frame(Beta_Deseado = betas_deseados, Ponderaciones = NA)
for (i in 1:length(betas_deseados)) {
  resultados$Ponderaciones[i] <- paste(round(ponderaciones_resultados[i, ], 3), collapse = ", ")
}

print(resultados)

This is where I can't get my data frame:

   ponderaciones_resultados[i, ] <- resultado_optimizacion$par / sum(resultado_optimizacion$par)
 }
Error in Ops.Date(left, right) : * not defined for "Date" objects

I tried ChatGPT and just made it worst, that gave me 2 errors instead of one.

Upvotes: 0

Views: 287

Answers (0)

Related Questions