harrelfe / rms

Regression Modeling Strategies
https://hbiostat.org/R/rms
Other
170 stars 48 forks source link

lrtest() accepts models fit on different number of observations #150

Closed proshano closed 1 month ago

proshano commented 2 months ago

I would have expected that, if models are fit on different sample sizes, lrtest() would throw an error, but it does not:


library(rms)

set.seed(123)

simulate_data <- function(n) {
  data <- data.frame(
    x1 = rnorm(n),
    x2 = rnorm(n),
    y = rbinom(n, 1, prob = 0.5)
  )
  return(data)
}

data_small <- simulate_data(100)
data_large <- simulate_data(500)

model_small <- lrm(y ~ x1 + x2, data = data_small)
model_large <- lrm(y ~ x1, data = data_large)

tryCatch(
  {
    lrtest(model_large, model_small)
  },
  error = function(e) {
    message("Error: ", e$message)
  }
)

Model 1: y ~ x1 Model 2: y ~ x1 + x2

L.R. Chisq d.f. P 2.3160658 1.0000000 0.1280435

harrelfe commented 1 month ago

Right, lrtest is not smart about this.