File: test-residuals.R

package info (click to toggle)
r-bioc-glmgampoi 1.18.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,796 kB
  • sloc: cpp: 665; ansic: 124; makefile: 2
file content (168 lines) | stat: -rw-r--r-- 6,016 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
test_that("residual calculation works", {
  set.seed(1)
  X <- cbind(1, matrix(rnorm(4 * 2) , nrow = 4, ncol = 2))
  Y <- matrix(rnbinom(n = 2 * 4, mu = 30, size = 0.7), nrow = 2, ncol = 4)
  fit <- glm_gp(Y, X, size_factors = FALSE, overdispersion = 1/0.7)
  r_fit1 <- glm(Y[1,] ~ X - 1, family = MASS::negative.binomial(theta = 0.7))
  r_fit2 <- glm(Y[2,] ~ X - 1, family = MASS::negative.binomial(theta = 0.7))
  expect_equal(unname(fit$Beta[1,]), unname(coef(r_fit1)), tolerance = 1e-4)
  expect_equal(unname(fit$Beta[2,]), unname(coef(r_fit2)), tolerance = 1e-4)

  expect_equal(c(t(residuals(fit, "response"))),
              unname(c(residuals.glm(r_fit1, "response"), residuals.glm(r_fit2, "response"))),
              tolerance = 1e-4)
  expect_equal(c(t(residuals(fit, "working"))),
               unname(c(residuals.glm(r_fit1, "working"), residuals.glm(r_fit2, "working"))),
               tolerance = 1e-5)
  expect_equal(c(t(residuals(fit, "pearson"))),
               unname(c(residuals.glm(r_fit1, "pearson"), residuals.glm(r_fit2, "pearson"))),
               tolerance = 1e-5)
  expect_equal(c(t(residuals(fit, "deviance"))),
               unname(c(residuals.glm(r_fit1, "deviance"), residuals.glm(r_fit2, "deviance"))),
               tolerance = 1e-5)

  # Randomized Quantiles are by definition not equal
  r_qs <- c(statmod::qresiduals(r_fit1),  statmod::qresiduals(r_fit2))
  res <- c(t(residuals(fit, "randomized_quantile")))
  expect_gt(cor(r_qs, res), 0.99)
})


test_that("residuals are named", {
  set.seed(1)
  X <- cbind(1, matrix(rnorm(4 * 2) , nrow = 4, ncol = 2))
  Y <- matrix(rnbinom(n = 2 * 4, mu = 30, size = 0.7), nrow = 2, ncol = 4)
  rownames(Y) <- paste0("Gene_", seq_len(nrow(Y)))
  colnames(Y) <- paste0("Cell_", seq_len(ncol(Y)))
  fit <- glm_gp(Y, X, size_factors = FALSE, overdispersion = 1/0.7)
  expect_equal(dimnames(residuals(fit)), dimnames(Y))
})



test_that("residual calculation works with Delayed Matrix", {
  set.seed(1)
  X <- cbind(1, matrix(rnorm(4 * 2) , nrow = 4, ncol = 2))
  Y <- matrix(rnbinom(n = 2 * 4, mu = 30, size = 0.7), nrow = 2, ncol = 4)
  Y_hdf5 <- HDF5Array::writeHDF5Array(Y)
  fit <- glm_gp(Y_hdf5, X, size_factors = FALSE, overdispersion = 1/0.7)
  r_fit1 <- glm(Y[1,] ~ X - 1, family = MASS::negative.binomial(theta = 0.7))
  r_fit2 <- glm(Y[2,] ~ X - 1, family = MASS::negative.binomial(theta = 0.7))
  expect_equal(unname(fit$Beta[1,]), unname(coef(r_fit1)), tolerance = 1e-4)
  expect_equal(unname(fit$Beta[2,]), unname(coef(r_fit2)), tolerance = 1e-4)

  expect_s4_class(residuals(fit, "response"), "DelayedMatrix")
  expect_s4_class(residuals(fit, "working"), "DelayedMatrix")
  expect_s4_class(residuals(fit, "pearson"), "DelayedMatrix")
  expect_s4_class(residuals(fit, "deviance"), "DelayedMatrix")

  expect_true(DelayedArray::isPristine(residuals(fit, "response")))
  expect_true(DelayedArray::isPristine(residuals(fit, "working")))
  expect_true(DelayedArray::isPristine(residuals(fit, "pearson")))
  expect_true(DelayedArray::isPristine(residuals(fit, "deviance")))


  expect_equal(c(t(residuals(fit, "response"))),
               unname(c(residuals.glm(r_fit1, "response"), residuals.glm(r_fit2, "response"))),
               tolerance = 1e-4)
  expect_equal(c(t(residuals(fit, "working"))),
               unname(c(residuals.glm(r_fit1, "working"), residuals.glm(r_fit2, "working"))),
               tolerance = 1e-5)
  expect_equal(c(t(residuals(fit, "pearson"))),
               unname(c(residuals.glm(r_fit1, "pearson"), residuals.glm(r_fit2, "pearson"))),
               tolerance = 1e-5)
  expect_equal(c(t(residuals(fit, "deviance"))),
               unname(c(residuals.glm(r_fit1, "deviance"), residuals.glm(r_fit2, "deviance"))),
               tolerance = 1e-5)

  # Randomized Quantiles are by definition not equal
  r_qs <- c(statmod::qresiduals(r_fit1),  statmod::qresiduals(r_fit2))
  res <- c(t(residuals(fit, "randomized_quantile")))
  expect_gt(cor(r_qs, res), 0.99)

})




test_that("qres.gampoi can handle extreme value", {
  Y <- matrix(27)
  Mu <- matrix(2)
  overdispersion <- 0
  res <- qres.gampoi(Y, Mu, overdispersion)
  expect_false(is.infinite(res))

  Y <- matrix(2700)
  res <- qres.gampoi(Y, Mu, overdispersion)
  expect_false(is.infinite(res))


  Y <- matrix(2)
  Mu <- matrix(270)
  res <- qres.gampoi(Y, Mu, overdispersion)
  expect_false(is.infinite(res))


  Y <- matrix(c(2, 2), ncol = 1)
  Mu <- matrix(c(270, 270), ncol = 1)
  overdispersion <- c(0, 500)
  res <- qres.gampoi(Y, Mu, overdispersion)
  expect_false(is.infinite(res[1,1]), is.infinite(res[2,1]))
  expect_false(res[1,1] == res[2,1])
})



test_that("qres.gampoi can handle other weird values", {
  if(R.version$arch == "i686") {
    skip("i686 does not exhibit this weird issue.")
  }
  # This specific combination of parameters caused NA's
  Y <- matrix(27)
  Mu <- matrix(0.435023)
  overdispersion <- 0

  a <- ppois(Y - 1, lambda = Mu)
  b <- ppois(Y, lambda = Mu)
  # This really shouldn't happen
  # Nonetheless, it does for this combination of parameters
  # at least on Linux and MacOS
  if(! is_windows()){
    expect_gt(a, b)
  }

  # However, now qres.gampoi handles this edge case
  res <- qres.gampoi(Y, Mu, overdispersion)
  expect_false(is.nan(res))
})



test_that("compute_gp_deviance can handle weird values", {
  # This specific combination of parameters caused negative deviance
  y <- 1
  mu <- 0.99999999999994
  theta <- 1e-7

  # However, now qres.gampoi handles this edge case
  res <- compute_gp_deviance(y, mu, theta)
  expect_gte(res, 0)
})


test_that("residuals are never NA", {

  Y <- matrix(0, nrow = 3, ncol = 10)
  Y[1, 4] <- 4
  Y[3, 2] <- 17

  fit <- glm_gp(Y, size_factors = FALSE)

  expect_true(all(! is.na(residuals(fit, "deviance"))))
  expect_true(all(! is.na(residuals(fit, "pearson"))))
  expect_true(all(! is.na(residuals(fit, "randomized_quantile"))))
  expect_true(all(! is.na(residuals(fit, "working"))))
  expect_true(all(! is.na(residuals(fit, "response"))))

})