0

I am building a decision analytic model (markov model). I am trying to check that the sum of all the rows in my transition probability array is equal to 1. The array is 85 rows, 85 columns and 35 slices,

I use this code

# Set a flag to keep track of whether or not all slices have rowsums equal to 1
all_slices_have_rowsums_equal_to_1 <- FALSE



# Loop through all of the slices in a_P_tunnels
for (i in 1:35) {
  # Find the row sums of the current slice
  row_sums <- rowSums(a_P_tunnels [ , , i])
  
  # Check if any of the row sums are not equal to 1
  if (any(row_sums != 1)) {
    # Print the slice number and the row sums that are not equal to 1
    cat("Slice", i, "has row sums", row_sums[row_sums != 1], "\n")
  }
}

i get this output:

> # Set a flag to keep track of whether or not all slices have rowsums equal to 1
> all_slices_have_rowsums_equal_to_1 <- FALSE
> 
> # Loop through all of the slices in a_P_tunnels
> for (i in 1:35) {
+   # Find the row sums of the current slice
+   row_sums <- rowSums(a_P_tunnels [ , , i])
+   
+   # Check if any of the row sums are not equal to 1
+   if (any(row_sums != 1)) {
+     # Print the slice number and the row sums that are not equal to 1
+     cat("Slice", i, "has row sums", row_sums[row_sums != 1], "\n")
+   }
+ }
Slice 1 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 2 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 3 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 4 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 5 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 6 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 7 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 8 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 9 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 10 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 11 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 12 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 13 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 14 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 15 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 16 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 17 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 18 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 19 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 20 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 21 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 22 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 23 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 24 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 25 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 26 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 27 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 28 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 29 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 30 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 31 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 32 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 33 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 34 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
Slice 35 has row sums 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
> 

Why does it print the row if the sum is equal to 1?

Thanks for any help

THis code works:



# Loop through all of the slices in a_P_tunnels
for (i in 1:35) {
  # Find the row sums of the current slice
  row_sums <- rowSums(a_P_tunnels [ , , i])
  
  # Check if any of the row sums are not equal to 1
  # with a tolerance of 5 decimal places
  if (!isTRUE(all.equal(row_sums, 1, tolerance = 1e-5))) {
    # Instead of comparing the row sums directly to 1,
    # compute the absolute difference between each row sum and 1
    # and check if any of the differences are greater than the tolerance
    differences <- abs(row_sums - 1)
    if (any(differences > 1e-5)) {
      # Print the slice number and the row sums that are not equal to 1
      # with 4 decimal places
      cat("Slice", i, "has row sums", format(round(row_sums[differences > 1e-5], 4), nsmall = 20), "\n")
    }
  }
}

aaPe
  • 3
  • 3
  • 7
    Probably [Why are these numbers not equal?](https://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal/) – user2974951 Dec 07 '22 at 13:08
  • 5
    Please read [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) :) – harre Dec 07 '22 at 13:08
  • 5
    specifically, try `if (!all(abs(row_sums-1) < 1e-8))` ... – Ben Bolker Dec 07 '22 at 13:37
  • Thanks for your help! Wasnt able to get a reproducible example (the data I use is pretty big and unsure of how to make it smaller and keep information etx). Tried adding more decimals and every row is 1.00000000000000000000 with that many decimals. The ```if (!all(abs(row_sums-1) < 1e-8)) ``` seems to be workning also. Can you explain why? – aaPe Dec 08 '22 at 10:55
  • Also I edited the post and added a code that seems to be working. – aaPe Dec 08 '22 at 10:55

0 Answers0