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.
-
When the
incorrect number of subscripts on matrix
error goes get fixed, thefor
loop will not return anything becauseFx_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 initializeFx_returns
in the global environment here as a matrix object with dimensions equal to the number of rows and columns anticipated, filled withNA
. The assignment ofFX_returns[, i]
will fill the object all rows at a time for each column and the results will persist. -
If
FX_returns
has two dimensions, as indicated byFX_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.