From ddaebc76bce529c9b926bcc144c0cc045c4dd928 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 27 Sep 2023 15:08:20 +0200 Subject: [PATCH] Recursion in `logLik.plm()` that slows down computations: (#623) Co-authored-by: Indrajeet Patil --- R/logLik.R | 2 +- tests/testthat/test-logLik.R | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/test-logLik.R diff --git a/R/logLik.R b/R/logLik.R index 90c12930b..5c42f41a5 100644 --- a/R/logLik.R +++ b/R/logLik.R @@ -55,7 +55,7 @@ logLik.plm <- function(object, ...) { attr(val, "nall") <- N0 attr(val, "nobs") <- N - attr(val, "df") <- insight::get_df(object, type = "model") + attr(val, "df") <- insight::n_parameters(object) + 1L class(val) <- "logLik" val diff --git a/tests/testthat/test-logLik.R b/tests/testthat/test-logLik.R new file mode 100644 index 000000000..2691f1d3c --- /dev/null +++ b/tests/testthat/test-logLik.R @@ -0,0 +1,27 @@ +test_that("logLik", { + skip_if_not_installed("plm") + skip_if_not_installed("withr") + + withr::local_options(list(expressions = 25)) + set.seed(1) + nnn <- 100 + ddta <- data.frame( + x1 = rnorm(nnn), + x2 = rnorm(nnn), + id = rep_len(1:(nnn / 10), nnn), + year = rep_len(1:11, nnn) + ) + ddta$y <- ddta$x1 * 0.5 - ddta$x2 * 0.5 + rnorm(nnn) + + m1 <- lm(y ~ x1 + x2, data = ddta) + l1 <- logLik(m1) + + m2 <- plm( + y ~ x1 + x2, + data = ddta, + model = "pooling", + index = c("id", "year") + ) + l2 <- logLik(m2) + expect_equal(l1, l2, tolerance = 1e-3, ignore_attr = TRUE) +})