I want to create a data frame which would look like the data frame consist of mean expected shortfall for the mentioned stocks wrt to the S&P 500 performance. The code is written below. I am getting error mentioned below. Please help.

Thanks in advance.

year AAPL JPM KO MCD PG

2022 x x x x x

2021 x x x x x

2020 x x x x x

2019 x x x x x

2018 x x x x x

```
> # Load required packages
> library(quantmod)
> library(PerformanceAnalytics)
>
> # Set start and end dates
> start_date <- as.Date("2018-01-01")
> end_date <- as.Date("2022-02-22")
>
> # Download NYSE stock data
> symbols <- c("AAPL", "JPM", "KO", "MCD", "PG")
> getSymbols(symbols, src = "yahoo", from = start_date, to = end_date)
> prices_nyse <- na.omit(merge(Cl(AAPL), Cl(JPM), Cl(KO), Cl(MCD), Cl(PG)))
> returns_nyse <- na.omit(Return.calculate(prices_nyse))
>
> # Download S&P500 data
> getSymbols("^GSPC", src = "yahoo", from = start_date, to = end_date)
> prices_spx <- na.omit(Ad(GSPC))
> returns_spx <- na.omit(Return.calculate(prices_spx))
>
> # Set the VaR level to 95%
> var_level <- 0.95
>
> # Calculate the Value at Risk (VaR) for NYSE
> var_nyse <- VaR(returns_nyse, p = 1 - var_level, method = "gaussian")
>
> # Calculate the expected losses that exceed the VaR level for NYSE
> losses_nyse <- returns_nyse[returns_nyse < -var_nyse]
> expected_loss_nyse <- mean(losses_nyse)
>
> # Calculate the Mean Expected Shortfall (MES) for NYSE
> mes_nyse <- mean(losses_nyse[losses_nyse < -expected_loss_nyse])
>
> # Calculate the Value at Risk (VaR) for S&P500
> var_spx <- VaR(returns_spx, p = 1 - var_level, method = "gaussian")
>
> # Calculate the expected losses that exceed the VaR level for S&P500
> losses_spx <- returns_spx[returns_spx < -var_spx]
> expected_loss_spx <- mean(losses_spx)
>
> # Calculate the Mean Expected Shortfall (MES) for S&P500
> mes_spx <- mean(losses_spx[losses_spx < -expected_loss_spx])
>
> # Print the results
> cat("NYSE - VaR:", round(-var_nyse, 4), "\n")
> cat("NYSE - Expected Losses:", round(-expected_loss_nyse, 4), "\n")
> cat("NYSE - MES:", round(-mes_nyse, 4), "\n")
> cat("\n")
> cat("S&P500 - VaR:", round(-var_spx, 4), "\n")
> cat("S&P500 - Expected Losses:", round(-expected_loss_spx, 4), "\n")
> cat("S&P500 - MES:", round(-mes_spx, 4))
```

Error

```
> # Calculate the expected losses that exceed the VaR level for NYSE
> losses_nyse <- returns_nyse[returns_nyse < -var_nyse]
Error in `<.default`(returns_nyse, -var_nyse) : non-conformable arrays
>
```