Help with loop code for a factor model

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.