2

I have created a list whose elements are themselves a list of matrices. I want to be able to extract the vectors of observations for each variable

    p13 = 0.493;p43 = 0.325;p25 = 0.335;p35 = 0.574;p12 = 0.868 
    std_e2 = sqrt(1-p12^2) 
    std_e3 = sqrt(1-(p13^2+p43^2)) 
    std_e5 = sqrt(1-(p25^2+p35^2+2*p25*p35*(p13*p12)))
    set.seed(1234)
    z1<-c(0,1)
    z2<-c(0,1)
    z3<-c(0,1)
    z4<-c(0,1)
    z5<-c(0,1)
    s<-expand.grid(z1,z2,z3,z4,z5); s
    s<-s[-1,];s
    shift<-3
    scenari<-s*shift;scenari
    scenario_1<-scenari[1];scenario_1
    genereting_fuction<-function(n){
      sample<-list()
      for (i in 1:nrow(scenario_1)){
        X1=rnorm(n)+scenari[i,1]
        X4=rnorm(n)+scenari[i,4]
        X2=X1*p12+std_e2*rnorm(n)+scenari[i,2]
        X3=X1*p13+X4*p43+std_e3*rnorm(n)+scenari[i,3]
        X5=X2*p25+X3*p35+std_e5*rnorm(n)+scenari[i,5]
        sample[[i]]=cbind(X1,X2,X3,X4,X5)
        colnames(sample[[i]])<-c("X1","X2","X3","X4","X5")
      }
      sample
    } 
    set.seed(123)
    dati_fault<- lapply(rep(10, 100), genereting_fuction)

     dati_fault[[1]]
    [[1]]
                X1       X2        X3         X4         X5
     [1,] 2.505826 1.736593 1.0274581 -0.6038358  1.9967656
     [2,] 4.127593 3.294344 2.8777777  1.2386725  3.0207723
     [3,] 1.853050 1.312617 1.1875699  0.5994921  1.0471564
     [4,] 4.481019 3.330629 2.1880050 -0.1087338  2.7331061
     [5,] 3.916191 3.306036 0.7258404 -1.1388570  1.0293168
     [6,] 3.335131 2.379439 1.2407679  0.3198553  1.6755424
     [7,] 3.574675 3.769436 1.1084120 -1.0065481  2.0034434
     [8,] 3.203620 2.842074 0.6550587 -0.8516120 -0.1433508
     [9,] 2.552959 2.642094 2.5376430  2.0387860  3.5318055
    [10,] 2.656474 1.607934 2.2760391 -1.3959822  1.0095796

I only want to save the elements of X1 in an object, and so for the other variables. .

  • Hi, we don't know what is `dati_fault` so we can't reproduce your code. Could you please provide a minimal example of your dataset ? See https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Also could you please show us what you have before and what you want after using lapply ? – Gowachin Feb 10 '22 at 10:39
  • Thanks for the edit, but we still miss all `pN` variables. Also to me dati_fault don't look like a list to me yet but is a dataframe. – Gowachin Feb 10 '22 at 11:01
  • Sorry, here is the missing part: p13=0.493;p43=0.325;p25=0.335;p35=0.574;p12=0.868 std_e2=sqrt(1-p12^2) std_e3=sqrt(1-(p13^2+p43^2)) std_e5=sqrt(1-(p25^2+p35^2+2*p25*p35*(p13*p12))) – Marianna Filippelli Feb 10 '22 at 11:08
  • This sample, generated 100 times with the lapply function, defines a list of 100 samples each containing a matrix as in the example – Marianna Filippelli Feb 10 '22 at 11:10
  • Can you edit the lapply function you use to create this list (let's say for jsut 5 or 10 samples)? – Gowachin Feb 10 '22 at 11:11
  • Thank you for the modifications of the question, I have modified more to understand better. Yes you want me to insert the code? – Marianna Filippelli Feb 10 '22 at 11:27
  • Yes that could be great. Could you also precise what is the problem you want to solve ? What do you mean by extracting X1 ? – Gowachin Feb 10 '22 at 12:21
  • 1
    I have modified the script – Marianna Filippelli Feb 10 '22 at 15:40

1 Answers1

2

Here you have a list of matrix with scenario in row and n columns.

genereting_fuction <- function(n, scenario, scenari){ 
  # added argument because you assume global variable use
  nr <- nrow(scenario)
  sample <- vector("list", length = nr) # sample<-list() 
  # creating a list is better than expanding it each iteration
  for (i in 1:nr){
    X1=rnorm(n)+scenari[i,1]
    X4=rnorm(n)+scenari[i,4]
    X2=X1*p12+std_e2*rnorm(n)+scenari[i,2]
    X3=X1*p13+X4*p43+std_e3*rnorm(n)+scenari[i,3]
    X5=X2*p25+X3*p35+std_e5*rnorm(n)+scenari[i,5]
    sample[[i]]=cbind(X1,X2,X3,X4,X5)
    colnames(sample[[i]])<-c("X1","X2","X3","X4","X5")
  }
  sample
} 

set.seed(123)
dati_fault<- lapply(rep(3, 2), function(x) genereting_fuction(x, scenario_1, scenari))
dati_fault


lapply(dati_fault, function(x) {
  tmp <- lapply(x, function(y) y[,"X1"])
  tmp <- do.call(rbind, tmp)
})

If you want to assemble this list of matrix, like using cbind, I suggest you just use a single big n value and not the lapply with rep inside it.

Also I bet there is easier way to simulate this number of scenari, but it's difficult to estimate without knowing the context of your code piece.

Also, try to solve your issue with a minimal example, working with a list of 100 list of 32 matrix of 5*10 is a bit messy !

Good luck !

Gowachin
  • 1,251
  • 2
  • 9
  • 17
  • Thank you very much, I have done it and that is exactly what I wanted to achieve. The context I am working in is statistical quality control and I need this result to build a control chart. In fact "scenario" represents the different combinations of variables when they have a shift in the mean, which in a state under control is assumed to be zero. Thanks again – Marianna Filippelli Feb 10 '22 at 18:24
  • Hi, I have noticed a problem. The data generated does not match the assigned instructions. That is, I should have data for variable X1 close to 3, and for the other variables close to 0. Why are all the data generated close to three? The following scenario is not taken into account – Marianna Filippelli Feb 15 '22 at 09:39
  • How did you change the scenario used ? – Gowachin Feb 15 '22 at 22:25
  • I have noticed that the values generated in data_fault for the individual variables are different from those extracted with the function you implemented – Marianna Filippelli Feb 16 '22 at 17:25
  • Can we reproduce this with your code ? Isn't it juste a modification of seed ? – Gowachin Feb 16 '22 at 22:55