Skip to content

Commit 86a0091

Browse files
committed
checks sandwich estimator
1 parent 1ad4a9f commit 86a0091

File tree

5 files changed

+38
-13
lines changed

5 files changed

+38
-13
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: PLNmodels
22
Title: Poisson Lognormal Models
3-
Version: 1.2.1
3+
Version: 1.2.2
44
Authors@R: c(
55
person("Julien", "Chiquet", , "julien.chiquet@inrae.fr", role = c("aut", "cre"),
66
comment = c(ORCID = "0000-0002-3629-3429")),

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Current version
22

3+
* fix sandwich variance estimation (PR #140)
34
* fix use of native pipe to ensure compatibility with R 3.6 (merge PR #125, fix #124)
45

56
# PLNmodels 1.2.0 (2024-03-05)

R/PLNfit-S3methods.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ sigma.PLNfit <- function(object, ...) {
156156
#' @description Extracts univariate standard errors for the estimated coefficient of B. Standard errors are computed from the (approximate) Fisher information matrix.
157157
#'
158158
#' @param object an R6 object with class PLNfit
159-
#' @param type string describing the type of variance approximation: "variational", "jackknife", "sandwich" (only for fixed covariance). Default is "variational".
159+
#' @param type string describing the type of variance approximation: "variational", "jackknife", "sandwich". Default is "sandwich".
160160
#' @param parameter string describing the target parameter: either B (regression coefficients) or Omega (inverse residual covariance)
161161
#'
162162
#' @seealso [vcov.PLNfit()] for the complete variance covariance estimation of the coefficient
@@ -169,13 +169,13 @@ sigma.PLNfit <- function(object, ...) {
169169
#' control = PLN_param(config_post = list(variational_var = TRUE)))
170170
#' standard_error(myPLN)
171171
#' @export
172-
standard_error <- function(object, type = c("variational", "jackknife", "sandwich"), parameter = c("B", "Omega")) {
172+
standard_error <- function(object, type = c("sandwich", "variational", "jackknife"), parameter = c("B", "Omega")) {
173173
UseMethod("standard_error", object)
174174
}
175175

176176
#' @describeIn standard_error Component-wise standard errors of B in [`PLNfit`]
177177
#' @export
178-
standard_error.PLNfit <- function(object, type = c("variational", "sandwich", "jackknife", "bootstrap"), parameter = c("B", "Omega")) {
178+
standard_error.PLNfit <- function(object, type = c("sandwich", "variational", "jackknife", "bootstrap"), parameter = c("B", "Omega")) {
179179
type <- match.arg(type)
180180
par <- match.arg(parameter)
181181
if (type == "variational" & is.null(attr(object$model_par$B, "variance_variational")))
@@ -191,7 +191,7 @@ standard_error.PLNfit <- function(object, type = c("variational", "sandwich", "j
191191

192192
#' @describeIn standard_error Component-wise standard errors of B in [`PLNfit_fixedcov`]
193193
#' @export
194-
standard_error.PLNfit_fixedcov <- function(object, type = c("variational", "sandwich", "jackknife", "bootstrap", "sandwich"), parameter = c("B", "Omega")) {
194+
standard_error.PLNfit_fixedcov <- function(object, type = c("sandwich", "variational", "jackknife", "bootstrap"), parameter = c("B", "Omega")) {
195195
type <- match.arg(type)
196196
par <- match.arg(parameter)
197197
if (par == "Omega")

man/standard_error.Rd

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-standard-error.R

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ data(trichoptera)
44
trichoptera <- prepare_data(trichoptera$Abundance, trichoptera$Covariate)
55

66
## Error messages ---------------------
7-
test_that("Check that fisher and standard_error return objects with proper dimensions and sign", {
7+
test_that("Variational Fisher: Check that fisher and standard_error return objects with proper dimensions and sign", {
88

99
myPLN_cov <- PLN(Abundance ~ Wind + offset(log(Offset)), data = trichoptera, control = PLN_param(config_post = list(variational_var = TRUE)))
1010
expect_is(myPLN_cov, "PLNfit")
@@ -31,20 +31,19 @@ test_that("Check that fisher and standard_error return objects with proper dimen
3131
myPLN <- PLN(Abundance ~ 1 + offset(log(Offset)), data = trichoptera, control = PLN_param(config_post = list(variational_var = TRUE)))
3232

3333
## Consistency -----------------------
34-
test_that("Check internal consistency of Fisher matrix for PLN models with no covariates", {
34+
test_that("Variational Fisher: Check internal consistency of Fisher matrix for PLN models with no covariates", {
3535
tol <- 1e-8
3636

3737
n <- nrow(myPLN$fitted)
3838
## Consistency of the standard error matrix
39-
sem <- (sqrt(n) * standard_error(myPLN)) %>% as.numeric()
39+
sem <- (sqrt(n) * standard_error(myPLN, "variational")) %>% as.numeric()
4040
manual.sem <- 1/colMeans(myPLN$fitted) %>% sqrt()
4141

4242
## Internal consistency
4343
expect_equal(sem, manual.sem, tolerance = tol)
4444

4545
})
4646

47-
4847
test_that("Check temporal consistency of Fisher matrix for PLN models with no covariates", {
4948
tol <- 1e-2
5049

@@ -73,6 +72,31 @@ test_that("Check temporal consistency of Fisher matrix for PLN models with no co
7372

7473
})
7574

75+
76+
## Error messages ---------------------
77+
test_that("Sandwich: check that fisher and standard_error return objects with proper dimensions and sign", {
78+
79+
myPLN_cov <- PLN(Abundance ~ Wind + offset(log(Offset)), data = trichoptera, control = PLN_param(config_post = list(sandwich_var = TRUE)))
80+
expect_is(myPLN_cov, "PLNfit")
81+
p <- myPLN_cov$p
82+
d <- myPLN_cov$d
83+
84+
85+
sem <- standard_error(myPLN_cov)
86+
## Dimensions
87+
expect_equal(dim(sem), c(d, p))
88+
89+
## Names
90+
expect_equal(rownames(sem), rownames(coef(myPLN_cov)))
91+
expect_equal(colnames(sem), colnames(coef(myPLN_cov)))
92+
93+
## Standard errors are all positive
94+
for (i in 1:(p*d)) {
95+
expect_gte(sem[i], 0)
96+
}
97+
98+
})
99+
76100
params <- PLNmodels:::create_parameters(n = 30, p = 3, d = 1, depths = 1e3)
77101
B <- params$B
78102
X <- as.matrix(params$X)

0 commit comments

Comments
 (0)