dimanche 16 juillet 2017

randomly assign values to two matrix based on conditions in R

I have two matrix: old1 and gl1, i want to randomly assign the values from the two matrix into another two new matrix. i used the method: 1) rbind the two matrix; 2) use sample function to random assign the values; 3) seperate the large matrix into two new matrix. Here are the codes:

 library(ms.sev)

    ### old Global MSSS matrix
    data(oldMsss)

    old <- data.matrix(oldMsss)

    old1 <- old[-1,-1]

    ## use the global MSSS
    data(globalMsss)

    gl <- data.matrix(globalMsss)

    gl1 <- gl[,-1]

    ## combine
    tc <- rbind(gl1,old1)

    set.seed(1)

    n.permutations = 5
    sum.table = rep(1,n.permutations)

    for(j in 1:n.permutations) {
        print(j)

        t1 <- matrix(sample(tc),nrow=60)
        tf <- t1[1:30,]
        tf1 <- t1[31:60,]
        sum.table[j] <- sum(tf1-tf)

    }

By checking the matrix in details, i noticed that for both old1 and gl1, from EDSS.0.0 to EDSS.9.5, the value should increase (for example, from 0.91 to 10 for gl1), same for the y, the value should decrease (for example, from 0.91 to 0.07 for gl1).

However, my method above is not correct as it didn't take the correlation into account when assigning the values. Any idea how to ramdonly assign the values to new matrix but followed similar pattern?

Many thanks...




Aucun commentaire:

Enregistrer un commentaire