Marianna Filippelli
Marianna Filippelli

Reputation: 43

Problems with the names of the elements of a list

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. .

Upvotes: 2

Views: 68

Answers (1)

Gowachin
Gowachin

Reputation: 1270

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 !

Upvotes: 2

Related Questions