Help with loop code for a factor model

Hello everyone,

I have a dataset (nrow = 10,000 ncol = 29) called random_draws for a factor model.
the first 27 columns are exchange rate returns, 28th and 29th columns are the factors observations.

I want to calculate exchange rate returns with the following factor model formula:

FX_returns = mean(i) + std_returns(i) * (rho1 * w1 + sqrt(1 - rho1^2) * w(i))
+ std_returns(i) * (rho2 * w2 + sqrt(1 - rho2^2) * w(i))

w: factor values
w(i): idiosyncratic values

The code I have right now is the following:

set.seed(123) # Set a seed for reproducibility
num_draws <- 10000
num_pairs <- 27
num_variables <- num_pairs + 2

for (i in 1:num_pairs) {
rho1 <- factor1_loadings[i]
rho2 <- factor2_loadings[i]
factor1_shocks <- random_draws[, num_pairs + 1]
factor2_shocks <- random_draws[, num_pairs + 2]
independent_shocks <- random_draws[, i]
FX_returns[, i] <- mean_returns[i] + std_returns[i] * (rho1 * factor1_shocks + sqrt(1 - rho1^2) * (independent_shocks)+ std_returns[i] * (rho2 * factor2_shocks + sqrt(1 - rho2^2) * independent_shocks)
}

factor1_loadings is a vector of factor loadings for the first principal component I got from a PCA and factor2_loadings is the same but for the second principal component
factor1_shocks are observations from the 28th column in the dataset
factor2_shocks are the observations from the 29th column in the dataset
independent _shocks are the observations per column from 1-27

I have calculated the mean and standard deviation of each column 1-27 which are the mean and standard deviations for each exchange rate

I want to place the FX returns outputs in a dataset with nrow = 10000 and ncol = 27 (same number of exchange rates)

The error code I keep getting is this one:
Error in FX_returns[, i] <- mean_returns[i] + std_returns[i] * (rho1 * :
incorrect number of subscripts on matrix

Can anyone help with how to fix this code so that it works?

This is as bar as it's possible to follow the problem because of a syntax error. When corrected, the next problem is that factor1_loadings is not in namespace. These are problems that a reprex (see the FAQ can solve to get everyone on the same page.

So, I can only offer generic advice.

  1. When the incorrect number of subscripts on matrix error goes get fixed, the for loop will not return anything because Fx_returns is created in the local environment of the function and is not returned to the global environment. And the usual attempt to make a return almost always returns just the final iteration. The usual fix is to initialize Fx_returns in the global environment here as a matrix object with dimensions equal to the number of rows and columns anticipated, filled with NA. The assignment of FX_returns[, i] will fill the object all rows at a time for each column and the results will persist.

  2. If FX_returns has two dimensions, as indicated by FX_returns[,i], no fewer and no more may be assigned to it. If it is actually a vector it has only one dimension and the assignment should be indicated FX_returns[1]` and no more can be stuffed. So, you should look at each term being assigned to it and make sure they result in a vector.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.