0
$\begingroup$

My (example) data frame is rather simple. I would like to know if there is a difference in numbers between the factors (n=2) within variable X.

example.df <- structure(list(X = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                         1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                         1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 
                                         2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 
                                         2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                         2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                         2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                         1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                         1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                         1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                         1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 
                                         1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 
                                         1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 
                                         1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 
                                         2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                         2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                         1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                         1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                         1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                         1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 
                                         1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 
                                         1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                         2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                         2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 
                                         1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                         1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                         1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
                                         2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 
                                         2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 
                                         1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 
                                         1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                         2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                         2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                         1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                         1L, 1L, 1L, 1L, 1L), .Label = c("Factor1", "Factor2"), class = "factor"), 
                         Surface = c(0.0136, 0.0144, 0.01475, 0.0166, 0.018, 0.0182, 
                                     0.0405, 0.0438, 0.046, 0.0502, 0.0502, 0.0506, 0.0512, 0.01475, 
                                     0.0136, 0.0144, 0.01475, 0.0166, 0.0182, 0.0144, 0.01475, 
                                     0.0166, 0.018, 0.0405, 0.0438, 0.0144, 0.0136, 0.0144, 0.01475, 
                                     0.0166, 0.0182, 0.0438, 0.0166, 0.0182, 0.0502, 0.0144, 0.018, 
                                     0.0405, 0.0506, 0.0136, 0.0502, 0.0506, 0.0136, 0.0144, 0.01475, 
                                     0.0166, 0.0182, 0.018, 0.0405, 0.046, 0.0502, 0.0506, 0.018, 
                                     0.0166, 0.0166, 0.046, 0.68769, 0.86016, 0.68769, 0.714, 
                                     0.75858, 0.86016, 0.68769, 0.714, 0.75858, 0.86016, 0.68769, 
                                     0.714, 0.75858, 0.86016, 0.75858, 0.68769, 0.714, 0.75858, 
                                     0.86016, 0.75858, 0.68769, 0.714, 0.75858, 0.86016, 0.68769, 
                                     0.714, 0.86016, 0.68769, 0.714, 0.75858, 0.86016, 0.68769, 
                                     0.714, 0.75858, 0.86016, 0.68769, 0.0369, 0.0132, 0.0151, 
                                     0.0204, 0.0341, 0.0352, 0.0369, 0.0418, 0.0473, 0.0508, 0.0132, 
                                     0.0473, 0.0132, 0.0151, 0.0204, 0.0341, 0.0352, 0.0369, 0.0418, 
                                     0.0473, 0.0508, 0.0132, 0.0341, 0.0418, 0.0473, 0.0508, 0.0132, 
                                     0.0369, 0.0418, 0.0204, 0.0473, 0.0418, 0.0204, 0.0341, 0.0132, 
                                     0.0204, 0.0341, 0.0473, 0.0132, 0.0369, 0.0418, 0.0508, 0.0132, 
                                     0.0204, 0.0341, 0.0369, 0.0473, 0.0508, 0.0473, 0.0204, 0.0341, 
                                     0.0473, 0.0132, 0.0341, 0.0369, 0.0418, 0.0473, 0.0508, 0.0136, 
                                     0.0144, 0.01475, 0.0166, 0.018, 0.0182, 0.0405, 0.0438, 0.046, 
                                     0.0502, 0.0502, 0.0506, 0.0512, 0.01475, 0.0506, 0.0144, 
                                     0.0166, 0.018, 0.0182, 0.0438, 0.046, 0.0502, 0.0506, 0.01475, 
                                     0.01475, 0.018, 0.0502, 0.0502, 0.0506, 0.018, 0.046, 0.0144, 
                                     0.01475, 0.0166, 0.018, 0.0182, 0.0136, 0.0144, 0.0438, 0.018, 
                                     0.0182, 0.0502, 0.0506, 0.0405, 0.0502, 0.0144, 0.01475, 
                                     0.046, 0.0506, 0.0166, 0.018, 0.0182, 0.0405, 0.046, 0.0502, 
                                     0.0502, 0.0512, 0.0166, 0.018, 0.0512, 0.01475, 0.68769, 
                                     0.714, 0.75858, 0.86016, 0.68769, 0.714, 0.75858, 0.86016, 
                                     0.68769, 0.714, 0.75858, 0.86016, 0.68769, 0.714, 0.75858, 
                                     0.86016, 0.68769, 0.714, 0.75858, 0.86016, 0.68769, 0.714, 
                                     0.75858, 0.86016, 0.86016, 0.714, 0.75858, 0.86016, 0.68769, 
                                     0.714, 0.75858, 0.86016, 0.68769, 0.714, 0.75858, 0.86016, 
                                     0.86016, 0.0151, 0.0204, 0.0341, 0.0352, 0.0369, 0.0418, 
                                     0.0473, 0.0508, 0.0132, 0.0204, 0.0508, 0.0132, 0.0204, 0.0369, 
                                     0.0418, 0.0473, 0.0508, 0.0204, 0.0341, 0.0418, 0.0132, 0.0132, 
                                     0.0352, 0.0204, 0.0369, 0.0508, 0.0369, 0.0151, 0.0204, 0.0352, 
                                     0.0369, 0.0418, 0.0473, 0.0508, 0.0151, 0.0132, 0.0369, 0.0473, 
                                     0.0132, 0.0151, 0.0204, 0.0341, 0.0369, 0.0418, 0.0473, 0.0508, 
                                     0.0204, 0.0132, 0.0144, 0.01475, 0.0166, 0.018, 0.0182, 0.0405, 
                                     0.0438, 0.046, 0.0502, 0.0502, 0.0506, 0.0512, 0.01475, 0.0144, 
                                     0.01475, 0.0166, 0.0182, 0.0405, 0.0144, 0.0166, 0.0144, 
                                     0.0166, 0.018, 0.0144, 0.0144, 0.01475, 0.0166, 0.0506, 0.0166, 
                                     0.0405, 0.0144, 0.0512, 0.0166, 0.018, 0.046, 0.0144, 0.0136, 
                                     0.01475, 0.0166, 0.0502, 0.0144, 0.0166, 0.75858, 0.86016, 
                                     0.68769, 0.714, 0.75858, 0.86016, 0.68769, 0.714, 0.75858, 
                                     0.86016, 0.68769, 0.714, 0.75858, 0.86016, 0.68769, 0.714, 
                                     0.75858, 0.86016, 0.68769, 0.714, 0.75858, 0.86016, 0.714, 
                                     0.75858, 0.86016, 0.68769, 0.86016, 0.68769, 0.714, 0.75858, 
                                     0.86016, 0.68769, 0.714, 0.0132, 0.0151, 0.0204, 0.0341, 
                                     0.0369, 0.0418, 0.0473, 0.0132, 0.0151, 0.0204, 0.0341, 0.0352, 
                                     0.0369, 0.0473, 0.0132, 0.0341, 0.0352, 0.0473, 0.0151, 0.0204, 
                                     0.0369, 0.0418, 0.0151, 0.0204, 0.0341, 0.0352, 0.0473, 0.0132, 
                                     0.0341, 0.0132, 0.0369, 0.0418, 0.0341, 0.0473, 0.0204, 0.0151, 
                                     0.0132, 0.0151, 0.0132, 0.0369, 0.0418, 0.0132, 0.0341, 0.0136, 
                                     0.0144, 0.01475, 0.0166, 0.018, 0.0182, 0.0405, 0.0438, 0.046, 
                                     0.0502, 0.0502, 0.0506, 0.0512, 0.0182, 0.0405, 0.0136, 0.0144, 
                                     0.0405, 0.0438, 0.046, 0.0502, 0.0502, 0.0144, 0.0182, 0.0405, 
                                     0.0136, 0.0506, 0.0136, 0.0166, 0.0182, 0.0438, 0.018, 0.0182, 
                                     0.0144, 0.0166, 0.0182, 0.0405, 0.0502, 0.046, 0.018, 0.0136, 
                                     0.0144, 0.018, 0.0182, 0.0405, 0.0438, 0.046, 0.0502, 0.0506, 
                                     0.0166, 0.018, 0.0182, 0.0405, 0.046, 0.0502, 0.0144, 0.0405, 
                                     0.0166, 0.018, 0.714, 0.86016, 0.68769, 0.714, 0.75858, 0.86016, 
                                     0.68769, 0.714, 0.75858, 0.86016, 0.68769, 0.714, 0.75858, 
                                     0.86016, 0.714, 0.68769, 0.714, 0.86016, 0.68769, 0.714, 
                                     0.86016, 0.714, 0.86016, 0.68769, 0.714, 0.75858, 0.86016, 
                                     0.68769, 0.714, 0.75858, 0.86016, 0.86016, 0.0132, 0.0151, 
                                     0.0204, 0.0341, 0.0369, 0.0418, 0.0473, 0.0508, 0.0341, 0.0352, 
                                     0.0132, 0.0341, 0.0369, 0.0132, 0.0341, 0.0369, 0.0132, 0.0132, 
                                     0.0341, 0.0352, 0.0132, 0.0204, 0.0341, 0.0369, 0.0418, 0.0473, 
                                     0.0508, 0.0151, 0.0341, 0.0352, 0.0132, 0.0151, 0.0341, 0.0132, 
                                     0.0369, 0.0508), Number = c(1L, 27L, 4L, 15L, 31L, 41L, 4L, 
                                                                 3L, 32L, 8L, 7L, 7L, 3L, 6L, 1L, 5L, 18L, 6L, 7L, 2L, 2L, 
                                                                 4L, 1L, 1L, 1L, 1L, 4L, 1L, 1L, 4L, 1L, 1L, 1L, 1L, 1L, 2L, 
                                                                 1L, 1L, 1L, 2L, 3L, 1L, 4L, 7L, 3L, 15L, 6L, 2L, 2L, 1L, 
                                                                 1L, 2L, 1L, 5L, 1L, 1L, 4L, 8L, 37L, 12L, 82L, 30L, 181L, 
                                                                 54L, 249L, 144L, 266L, 329L, 238L, 218L, 1L, 27L, 54L, 13L, 
                                                                 65L, 1L, 50L, 34L, 7L, 22L, 3L, 4L, 2L, 2L, 2L, 5L, 3L, 14L, 
                                                                 7L, 4L, 4L, 1L, 1L, 152L, 43L, 98L, 39L, 13L, 5L, 10L, 230L, 
                                                                 13L, 1L, 2L, 20L, 1L, 1L, 1L, 38L, 8L, 7L, 19L, 1L, 2L, 1L, 
                                                                 1L, 2L, 2L, 6L, 10L, 3L, 2L, 1L, 2L, 1L, 3L, 4L, 1L, 5L, 
                                                                 1L, 3L, 31L, 31L, 2L, 4L, 5L, 8L, 4L, 48L, 1L, 2L, 2L, 3L, 
                                                                 5L, 9L, 1L, 11L, 17L, 2L, 1L, 1L, 2L, 9L, 10L, 14L, 50L, 
                                                                 8L, 5L, 5L, 11L, 6L, 22L, 11L, 2L, 1L, 9L, 1L, 3L, 2L, 1L, 
                                                                 1L, 1L, 3L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 4L, 9L, 2L, 
                                                                 13L, 1L, 1L, 1L, 1L, 3L, 1L, 4L, 1L, 1L, 1L, 1L, 1L, 3L, 
                                                                 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 12L, 2L, 2L, 1L, 1L, 1L, 
                                                                 1L, 17L, 18L, 14L, 40L, 13L, 138L, 60L, 70L, 79L, 299L, 377L, 
                                                                 172L, 269L, 31L, 49L, 32L, 68L, 10L, 19L, 8L, 6L, 1L, 2L, 
                                                                 2L, 3L, 2L, 1L, 1L, 3L, 13L, 3L, 9L, 12L, 1L, 477L, 82L, 
                                                                 29L, 60L, 312L, 124L, 15L, 43L, 25L, 53L, 1L, 3L, 6L, 1L, 
                                                                 4L, 1L, 1L, 2L, 2L, 2L, 1L, 8L, 2L, 2L, 5L, 2L, 1L, 3L, 5L, 
                                                                 3L, 71L, 30L, 1L, 2L, 1L, 1L, 3L, 1L, 7L, 1L, 7L, 5L, 10L, 
                                                                 16L, 1L, 2L, 1L, 2L, 36L, 5L, 27L, 8L, 8L, 23L, 1L, 5L, 6L, 
                                                                 1L, 6L, 1L, 1L, 27L, 4L, 6L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 
                                                                 1L, 4L, 3L, 4L, 1L, 3L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 4L, 1L, 
                                                                 3L, 1L, 1L, 2L, 2L, 2L, 1L, 11L, 26L, 7L, 49L, 19L, 47L, 
                                                                 54L, 994L, 1175L, 739L, 1163L, 83L, 171L, 248L, 148L, 51L, 
                                                                 220L, 15L, 36L, 7L, 1L, 6L, 1L, 2L, 35L, 44L, 12L, 18L, 1L, 
                                                                 2L, 262L, 3L, 417L, 144L, 5L, 2L, 47L, 241L, 6L, 7L, 16L, 
                                                                 1L, 5L, 1L, 207L, 1L, 3L, 4L, 23L, 2L, 35L, 1L, 1L, 1L, 1L, 
                                                                 1L, 5L, 1L, 1L, 1L, 5L, 1L, 1L, 1L, 1L, 2L, 1L, 3L, 8L, 7L, 
                                                                 3L, 1L, 1L, 9L, 30L, 4L, 51L, 43L, 67L, 21L, 20L, 36L, 49L, 
                                                                 27L, 60L, 2L, 1L, 9L, 2L, 11L, 3L, 3L, 2L, 4L, 1L, 1L, 1L, 
                                                                 2L, 1L, 1L, 3L, 6L, 2L, 1L, 1L, 2L, 1L, 2L, 3L, 1L, 7L, 1L, 
                                                                 1L, 1L, 1L, 2L, 1L, 12L, 1L, 1L, 2L, 2L, 3L, 2L, 3L, 2L, 
                                                                 9L, 1L, 2L, 1L, 1L, 1L, 1L, 15L, 9L, 12L, 93L, 6L, 155L, 
                                                                 74L, 48L, 40L, 47L, 250L, 5L, 1310L, 2L, 5L, 4L, 25L, 8L, 
                                                                 60L, 91L, 1L, 8L, 2L, 5L, 2L, 2L, 15L, 54L, 6L, 49L, 1L, 
                                                                 105L, 3L, 50L, 100L, 1L, 5L, 21L, 2L, 1L, 3L, 136L, 45L, 
                                                                 3L, 41L, 5L, 4L, 3L, 1L, 1L, 1L, 46L, 6L, 27L, 7L, 1L, 5L, 
                                                                 1L, 4L, 1L, 7L, 5L, 1L, 5L, 1L, 1L, 1L)), row.names = c(NA, 
                                                                                                                         -541L), class = "data.frame")

As you can see the original data (=numbers) is count data. The amount of sampling effort (=surface) differs between observations. In the explorative phase I calculated the density by dividing the numbers with surface:

example.df$Den <- example.df$Number/example.df$Surface

When plotted it looks like this:

enter image description here

Factor1 clearly has an overall higher density. The mean is many times higher as well.

When implmenting a NB-model I accounted for the difference in sampling effort by using an offset (with and without log) and created the following model:

glm.example <- glm.nb(Number ~ X + offset(log(Surface+1)), data=example.df)
summary(glm.example)

At first the output looks normal. But in contrary to the what the figure (and raw data) demonstrates, the model produces beta-values which suggests that factor1 is associated with lower numbers:

plot(example.df$X, predict(glm.example, type= "response"), main= "Predicted data")

enter image description here

Question

What causes this (what looks like) deviating output in relation to the original data? My feeling is that the offset converts the rates in very different way.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.