Debian Codegraph @

tests/testthat/test-stats-factanal.R

raw
1 skip_on_cran()
2
3 skip_if_not_installed("modeltests")
4 library(modeltests)
5
6 n_factors <- 3
7 n_factors2 <- 3
8
9 fit <- factanal(mtcars, n_factors)
10 fit2 <- factanal(mtcars, n_factors2)
11
12 test_that("factanal tidier arguments", {
13 check_arguments(tidy.factanal)
14 check_arguments(glance.factanal)
15 check_arguments(augment.factanal, strict = FALSE)
16 })
17
18 test_that("tidy.factanal", {
19 td <- tidy(fit)
20 td2 <- tidy(fit2)
21
22 modeltests::check_tidy_output(td, strict = FALSE)
23 modeltests::check_tidy_output(td2, strict = FALSE)
24 check_dims(td, ncol(mtcars), 2 + n_factors)
25
26 expect_equal(td$variable, colnames(mtcars))
27 expect_equal(ncol(td2), 2 + n_factors2)
28 })
29
30 test_that("glance.factanal works", {
31 gl <- glance(fit)
32 gl2 <- glance(fit2)
33
34 check_glance_outputs(gl, gl2)
35 check_dims(gl, 1, 9)
36
37 expect_equal(gl$n.factors, n_factors)
38 })
39
40 test_that("augment.factanal works", {
41 fit_reg <- factanal(mtcars, n_factors, scores = "regression")
42 fit_bart <- factanal(mtcars, n_factors, scores = "Bartlett")
43
44 check_augment_function(
45 aug = augment.factanal,
46 model = fit_reg,
47 data = mtcars,
48 strict = FALSE
49 )
50
51 check_augment_function(
52 aug = augment.factanal,
53 model = fit_bart,
54 data = mtcars,
55 strict = FALSE
56 )
57
58 # errors for `scores = "none"`
59 fit_none <- factanal(mtcars, n_factors, scores = "none")
60 expect_snapshot(error = TRUE, augment(fit_none))
61 })
62
63 test_that("augment.factanal works with matrix", {
64 suppressPackageStartupMessages(library(broom))
65 set.seed(123)
66
67 # data
68 v1 <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 4, 5, 6)
69 v2 <- c(1, 2, 1, 1, 1, 1, 2, 1, 2, 1, 3, 4, 3, 3, 3, 4, 6, 5)
70 v3 <- c(3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 4, 6)
71 v4 <- c(3, 3, 4, 3, 3, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 5, 6, 4)
72 v5 <- c(1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 6, 4, 5)
73 v6 <- c(1, 1, 1, 2, 1, 3, 3, 3, 4, 3, 1, 1, 1, 2, 1, 6, 5, 4)
74 m1 <- cbind(v1, v2, v3, v4, v5, v6)
75
76 # new data
77 m2 <-
78 cbind(
79 x1 = rev(v1),
80 x2 = rev(v2),
81 x3 = rev(v3),
82 x4 = rev(v4),
83 x5 = rev(v5),
84 x6 = rev(v6)
85 )
86
87 # objects
88 fit1 <- stats::factanal(m1, factors = 3, scores = "Bartlett")
89 fit2 <- stats::factanal(m1, factors = 3, scores = "regression")
90
91 # augmented dataframe
92 df1 <- augment(fit1)
93 df2 <- augment(fit2)
94
95 # augmented dataframe (with new data)
96 df3 <- augment(fit1, data = m2)
97 df4 <- augment(fit2, data = m2)
98
99 # checking dataframe dimensions
100 modeltests::check_tibble(df1, "augment", strict = FALSE)
101 modeltests::check_dims(df1, 18L, 4L)
102 modeltests::check_dims(df2, 18L, 4L)
103 modeltests::check_dims(df3, 18L, 10L)
104 modeltests::check_dims(df4, 18L, 10L)
105 expect_identical(names(df1), c(".rownames", ".fs1", ".fs2", ".fs3"))
106 expect_identical(
107 names(df3),
108 c(
109 ".rownames",
110 "x1",
111 "x2",
112 "x3",
113 "x4",
114 "x5",
115 "x6",
116 ".fs1",
117 ".fs2",
118 ".fs3"
119 )
120 )
121 })