1

I want to run a loop with 26 matrices, 13 with numbers (e.g. 1,1,2,2,2,3) and the other 13 with letters simulating diferent parameters (e.g. U1, U2, U3...etc). My problem comes when I want to run them in a loop attaching the first term of each matrix in order to run them simultaneously in the function. The errors that appears is the next:

Errors were caught in checkModelList The model value for U is not
allowed. Check ?MARSS.form Error: Stopped in checkModelList() due to specification problem(s).

I drop the code and the data structure below:

str(Y)
 num [1:43, 1:24] NA NA NA 0.158 -1.172 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:43] "WGR_D_l.s" "WGR_D_l.m" "WGR_D_l.l" "WGR_Sh_l.s" ...
  ..$ : NULL
    str(Z1)
 num [1:43, 1] 1 1 1 1 1 1 1 1 1 1 ...
str(U1)
 chr [1, 1] "U1"

 z = c("Z1_","Z2_",'Z3_', 'Z4_', 'Z5_', "Z6_","Z7_",'Z8_', 'Z9_', 'Z10_','Z11_', 'Z12_', 'Z13_')
 u = c("U1_","U2_",'U3_', 'U4_', 'U5_', "U6_","U7_",'U8_', 'U9_', 'U10_','U11_', 'U12_', 'U13_')
 Q = c("unconstrained", "diagonal and unequal", "diagonal and equal")
 q = c('Qun1','Qdu1','Qde1')
    
for(g in 1:length(U)){
  model01$U = U[g]

  for(i in 1:length(Z)){
     model01$Z = Z[[i]]

      for(j in 1:length(Q)){
        model01$Q = Q[j]

        print(paste(q[j], sep=""))

        m1 = MARSS(Y, model=model01,
                   control=list(maxit = 5000,trace = -1, conv.test.slope.tol=100),
                   silent=2, method="kem")

        model.name.txt = paste("C:/Users/ubeda/OneDrive/Desktop/Resultados post-TFM/1. RUN_Nocovariates_2xU/TXT/",q[j],'.txt', sep='')
        capture.output(m1,file=model.name.txt)
        print(model.name.txt)
        model.name.rds = paste("C:/Users/ubeda/OneDrive/Desktop/Resultados post-TFM/1. RUN_Nocovariates_2xU/RDS/",q[j], '.rds', sep='')
        print(model.name.rds)
        saveRDS(m1,model.name.rds)
      }#j
   }#i
 }#end g

However, if I run them one by one the code run smoothly. This would cost me an eternity to run the code 13 times, and I thought the loop was the best option. Here I drop the code in which the model run withouth problems:

Q = c("unconstrained", "diagonal and unequal", "diagonal and equal")
q = c('Qun1','Qdu1','Qde1')
h = "catches raw"

      for(j in 1:length(Q)){
        model01$Q = Q[j]

        print(paste(q[j], sep=""))

        m1 = MARSS(Y, model=model01,
                   control=list(maxit = 5000,trace = -1, conv.test.slope.tol=100),
                   silent=2, method="kem")

        model.name.txt = paste("C:/Users/ubeda/OneDrive/Desktop/Resultados post-TFM/RUN_NAO_catches_raw_2xU/TXT/",q[j],h[1],'.txt', sep='')
        capture.output(m1,file=model.name.txt)
        print(model.name.txt)
        model.name.rds = paste("C:/Users/ubeda/OneDrive/Desktop/Resultados post-TFM/RUN_NAO_catches_raw_2xU/RDS/",q[j],h[1], '.rds', sep='')
        print(model.name.rds)
        saveRDS(m1,model.name.rds)
      }#j

Anyone has any idea about what is my problem?

user241296
  • 29
  • 4
  • Please provide a reproducible example. Does the saving of the model impact the functionality? Where are `U` and `Y` defined? – Robin Gertenbach Aug 25 '21 at 11:54
  • The saving model does not impact the functionality. U and Z are matrices with one colum and "x" number of rows. My aim is to apply the function one by one, and I thought that importing them in a list it could work, but it didnt.... If I run the same code but importing the matrices one by one it work. I dont know why it does work in the loop. I have add to the question the way in which it gives me the outputs withouth problems. – user241296 Aug 25 '21 at 12:27
  • Check out [this post](https://stackoverflow.com/a/5963610/5426909) on how to provide a reproducible R example. If the export isn't part of the problem it shouldn't be part of the code in the question, data should be supplied via `dput` so that we can run the code ourselves and reproduce the error otherwise we can only guess. – Robin Gertenbach Aug 25 '21 at 13:56

0 Answers0