@@ -17,3 +17,36 @@ test_that("r2_nagelkerke", {
1717 }
1818 )
1919})
20+
21+ test_that(" r2_nagelkerke, multinom, correct base-model with NA" , {
22+ skip_on_cran()
23+ skip_if_not_installed(" nnet" )
24+
25+ n_obs <- 1000
26+ softmax <- function (x ) {
27+ exp(x - max(x )) / sum(exp(x - max(x )))
28+ }
29+ sample_y <- function (x ) {
30+ sample(1 : 3 , size = 1 , prob = softmax(c(0.25 * x , - 0.1 * x , 0 * x )))
31+ }
32+ set.seed(123 )
33+ sim_df <- data.frame (x = rnorm(n_obs , 0 , 1 ), y = NA )
34+
35+ for (i in 1 : nrow(sim_df )) {
36+ sim_df $ y [i ] <- sample_y(sim_df $ x [i ])
37+ }
38+
39+ sim_df $ x [1 : 500 ] <- NA
40+ sim_df2 <- sim_df [! is.na(sim_df $ x ), ]
41+
42+ m1 <- nnet :: multinom(y ~ x , data = sim_df , trace = FALSE )
43+ m2 <- nnet :: multinom(y ~ x , data = sim_df2 , trace = FALSE )
44+
45+ out1 <- r2_nagelkerke(m1 )
46+ out2 <- r2_nagelkerke(m2 )
47+ expect_equal(out1 , out2 , tolerance = 1e-4 , ignore_attr = TRUE )
48+
49+ out1 <- r2_mcfadden(m1 )
50+ out2 <- r2_mcfadden(m2 )
51+ expect_equal(out1 $ R2 , out2 $ R2 , tolerance = 1e-4 , ignore_attr = TRUE )
52+ })
0 commit comments