@@ -300,3 +300,113 @@ test_that("check_collinearity, validate adjusted vif against car", {
300300 expect_equal(out1 [, 1 ], out2 $ VIF , tolerance = 1e-3 , ignore_attr = TRUE )
301301 expect_equal(out1 [, 3 ], out2 $ SE_factor , tolerance = 1e-3 , ignore_attr = TRUE )
302302})
303+
304+ test_that(" check_collinearity, ordinal clmm models" , {
305+ skip_if_not_installed(" ordinal" )
306+ set.seed(999 )
307+ n <- 500
308+ x_continuous <- rnorm(n , mean = 0 , sd = 1 )
309+ x_binary <- sample(c(- 0.5 , 0.5 ), size = n , replace = TRUE , prob = c(0.85 , 0.15 ))
310+ subject_id <- factor (rep(1 : 50 , each = 10 ))
311+ random_intercepts <- rnorm(50 , 0 , 1 )
312+ latent_y <- 2 *
313+ x_continuous +
314+ 3 * x_binary +
315+ random_intercepts [as.numeric(subject_id )] +
316+ rlogis(n )
317+ y_ordinal <- cut(
318+ latent_y ,
319+ breaks = 15 ,
320+ ordered_result = TRUE
321+ )
322+ dat <- data.frame (y_ordinal , x_continuous , x_binary , subject_id )
323+ mod_clmm <- ordinal :: clmm(
324+ y_ordinal ~ x_continuous + x_binary + (1 | subject_id ),
325+ data = dat
326+ )
327+ out <- check_collinearity(mod_clmm )
328+ expect_s3_class(out , " check_collinearity" )
329+ expect_identical(out $ Term , c(" x_continuous" , " x_binary" ))
330+ expect_equal(out $ VIF , c(1.12 , 1.12 ), tolerance = 0.05 )
331+ })
332+
333+ test_that(" check_collinearity, ordinal clm models" , {
334+ skip_if_not_installed(" ordinal" )
335+ set.seed(999 )
336+ n <- 500
337+ x_continuous <- rnorm(n , mean = 0 , sd = 1 )
338+ x_binary <- sample(c(- 0.5 , 0.5 ), size = n , replace = TRUE , prob = c(0.85 , 0.15 ))
339+ latent_y <- 2 * x_continuous + 3 * x_binary + rlogis(n )
340+ y_ordinal <- cut(
341+ latent_y ,
342+ breaks = 15 ,
343+ ordered_result = TRUE
344+ )
345+ dat <- data.frame (y_ordinal , x_continuous , x_binary )
346+ mod_clm <- ordinal :: clm(
347+ y_ordinal ~ x_continuous + x_binary ,
348+ data = dat
349+ )
350+ out <- check_collinearity(mod_clm )
351+ expect_s3_class(out , " check_collinearity" )
352+ expect_identical(out $ Term , c(" x_continuous" , " x_binary" ))
353+ expect_equal(out $ VIF , c(1.11 , 1.11 ), tolerance = 0.05 )
354+ })
355+
356+ test_that(" check_collinearity, ordinal clmm models with offset" , {
357+ skip_if_not_installed(" ordinal" )
358+ set.seed(999 )
359+ n <- 500
360+ x_continuous <- rnorm(n , mean = 0 , sd = 1 )
361+ x_binary <- sample(c(- 0.5 , 0.5 ), size = n , replace = TRUE , prob = c(0.85 , 0.15 ))
362+ x_offset <- rnorm(n , mean = 0 , sd = 0.5 )
363+ subject_id <- factor (rep(1 : 50 , each = 10 ))
364+ random_intercepts <- rnorm(50 , 0 , 1 )
365+
366+ latent_y <- 2 *
367+ x_continuous +
368+ 3 * x_binary +
369+ random_intercepts [as.numeric(subject_id )] +
370+ x_offset +
371+ rlogis(n )
372+ y_ordinal <- cut(latent_y , breaks = 15 , ordered_result = TRUE )
373+ dat <- data.frame (y_ordinal , x_continuous , x_binary , x_offset , subject_id )
374+ mod_clmm_offset <- ordinal :: clmm(
375+ y_ordinal ~ x_continuous + x_binary + offset(x_offset ) + (1 | subject_id ),
376+ data = dat
377+ )
378+ out <- check_collinearity(mod_clmm_offset )
379+ expect_s3_class(out , " check_collinearity" )
380+ expect_identical(out $ Term , c(" x_continuous" , " x_binary" ))
381+ expect_equal(out $ VIF , c(1.12 , 1.12 ), tolerance = 0.05 )
382+ })
383+
384+ test_that(" check_collinearity, ordinal clm models with offset" , {
385+ skip_if_not_installed(" ordinal" )
386+ set.seed(999 )
387+ n <- 500
388+ x_continuous <- rnorm(n , mean = 0 , sd = 1 )
389+ x_binary <- sample(c(- 0.5 , 0.5 ), size = n , replace = TRUE , prob = c(0.85 , 0.15 ))
390+ x_offset <- rnorm(n , mean = 0 , sd = 0.5 )
391+ latent_y <- 2 * x_continuous + 3 * x_binary + x_offset + rlogis(n )
392+ y_ordinal <- cut(latent_y , breaks = 15 , ordered_result = TRUE )
393+ dat <- data.frame (y_ordinal , x_continuous , x_binary , x_offset )
394+ mod_clm_offset <- ordinal :: clm(
395+ y_ordinal ~ x_continuous + x_binary + offset(x_offset ),
396+ data = dat
397+ )
398+ out <- check_collinearity(mod_clm_offset )
399+ expect_s3_class(out , " check_collinearity" )
400+ expect_identical(out $ Term , c(" x_continuous" , " x_binary" ))
401+ expect_equal(out $ VIF , c(1.11 , 1.11 ), tolerance = 0.05 )
402+ })
403+
404+ test_that(" check_collinearity, standard lm models with offset" , {
405+ # Standard linear model with an offset
406+ m_lm_offset <- lm(mpg ~ wt + cyl + offset(disp ), data = mtcars )
407+ out <- check_collinearity(m_lm_offset )
408+ expect_s3_class(out , " check_collinearity" )
409+ # The offset should not be evaluated for collinearity
410+ expect_identical(out $ Term , c(" wt" , " cyl" ))
411+ expect_false(" disp" %in% out $ Term )
412+ })
0 commit comments