jeudi 22 mars 2018

Sample matrix in R

I'm trying to create two matrices one that can be filled with 0's and 0.25's and another with 0's and 1's.

vector <- c()
for(i in 1:1000){
  dummy_qt <- as.data.frame(matrix(sample(c(0, 0.25), 44, replace = TRUE), 4, 11))
  colnames(dummy_qt) <- c(2005:2015)
  boot_qt <- dummy_qt %>%
    summarise_all(funs(sum)) %>%
    sum()/11

  dummy_y <- as.data.frame(matrix(sample(c(0, 1), 44, replace = TRUE), 4, 11))
  colnames(dummy_y) <- c(2005:2015)
  boot_y <- dummy_y %>%
    mutate(sumrow = rowSums(.)/11) %>%
    select(sumrow) %>%
    sum()

  qt_y <- sum(boot_qt, boot_y)
  vector[i] <- qt_y
}

After creating the matrices, I run a mathematical formula, and put it into a vector. However, I know the maximum the matrix dummy_qt can get is 1, and the dummy_y is 4, making the final result 5 if all values within the matrix are 0.25 for the first matrix and 1 for the second.

Whenever I run this, usually the histogram shows that I only get values between 0 and 3.25 which doesn't reflect the reality. Saying so, I wonder what's going wrong as they should vary between 0 and 5.




Aucun commentaire:

Enregistrer un commentaire