Skip to content

Commit

Permalink
Recursion in logLik.plm() that slows down computations: (#623)
Browse files Browse the repository at this point in the history
Co-authored-by: Indrajeet Patil <[email protected]>
  • Loading branch information
strengejacke and IndrajeetPatil committed Sep 27, 2023
1 parent 462e8ae commit ddaebc7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion R/logLik.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions tests/testthat/test-logLik.R
Original file line number Diff line number Diff line change
@@ -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)
})

0 comments on commit ddaebc7

Please sign in to comment.