-
Notifications
You must be signed in to change notification settings - Fork 42
/
test-clm.R
150 lines (135 loc) · 3.99 KB
/
test-clm.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
test_that("colorizing works", {
ordinal_ologit <- ordinal::clm(rating ~ temp * contact,
data = ordinal::wine,
link = "logit"
)
greek_col <- c("#1B9E77", "#D95F02", "#7570B3", "#E7298A", "#66A61E",
"#E6AB02", "#A6761D", "#666666")
expect_snapshot_output(
extract_eq(
ordinal_ologit,
wrap = TRUE,
terms_per_line = 2,
var_colors = c(temp = "blue"),
greek_colors = greek_col,
subscript_colors = rev(greek_col)
)
)
})
test_that("Renaming Variables works", {
df <- data.frame(
outcome = factor(rep(LETTERS[1:3], 100),
levels = LETTERS[1:3],
ordered = TRUE
),
categorical = rep(letters[1:5], 20),
continuous = rnorm(300, 100, 1)
)
expect_warning(
m3 <- ordinal::clm(outcome ~ categorical*continuous,
data = df,
link = "logit"
)
)
expect_snapshot_output(
extract_eq(
m3,
swap_var_names = c("categorical" = "cat"),
swap_subscript_names = c(
"a" = "Hey look! A new subscript",
"d" = "Don't look at me though"
),
wrap = TRUE,
terms_per_line = 2
)
)
})
test_that("Math extraction works", {
df <- data.frame(
outcome = factor(rep(LETTERS[1:3], 100),
levels = LETTERS[1:3],
ordered = TRUE
),
continuous = rnorm(300, 100, 1)
)
expect_warning(
model_logit <- ordinal::clm(
outcome ~ poly(continuous, 3) + exp(continuous) + log(continuous),
data = df,
link = "logit"
)
)
expect_warning(
model_probit <- ordinal::clm(
outcome ~ poly(continuous, 3) + exp(continuous) + log(continuous),
data = df,
link = "probit"
)
)
expect_snapshot_output(extract_eq(model_logit))
expect_snapshot_output(extract_eq(model_probit))
})
test_that("Collapsing clm factors works", {
df <- data.frame(
outcome = factor(rep(LETTERS[1:3], 100),
levels = LETTERS[1:3],
ordered = TRUE
),
categorical = rep(letters[1:5], 20),
continuous = rnorm(300, 100, 1)
)
expect_warning(
model_logit <- ordinal::clm(outcome ~ categorical*continuous,
data = df, link = "logit"
)
)
expect_warning(
model_probit <- ordinal::clm(outcome ~ categorical*continuous,
data = df, link = "probit"
)
)
# no collapsing
expect_snapshot(extract_eq(model_logit))
expect_snapshot(extract_eq(model_probit))
# collapsing
expect_snapshot(extract_eq(model_logit, index_factors = TRUE))
expect_snapshot(extract_eq(model_probit, index_factors = TRUE))
})
test_that("Ordered models with clm work", {
set.seed(1234)
df <- data.frame(
outcome = factor(rep(LETTERS[1:3], 100),
levels = LETTERS[1:3],
ordered = TRUE
),
continuous_1 = rnorm(300, 1, 1),
continuous_2 = rnorm(300, 5, 5)
)
model_logit <- ordinal::clm(outcome ~ continuous_1 + continuous_2,
data = df, link = "logit"
)
model_probit <- ordinal::clm(outcome ~ continuous_1 + continuous_2,
data = df, link = "probit"
)
expect_snapshot_output(extract_eq(model_logit, wrap = FALSE))
expect_snapshot_output(extract_eq(model_logit, wrap = TRUE, terms_per_line = 2))
expect_snapshot_output(extract_eq(model_probit, wrap = FALSE))
expect_snapshot_output(extract_eq(model_probit, wrap = TRUE, terms_per_line = 2))
# Coefficients instead of letters
expect_snapshot_output(extract_eq(model_logit, use_coefs = TRUE))
})
test_that("Unsupported CLMs create a message", {
set.seed(1234)
df <- data.frame(
outcome = factor(rep(LETTERS[1:3], 100),
levels = LETTERS[1:3],
ordered = TRUE
),
continuous_1 = rnorm(300, 1, 1),
continuous_2 = rnorm(300, 5, 5)
)
model <- ordinal::clm(outcome ~ continuous_1 + continuous_2,
data = df, link = "cauchit"
)
expect_message(extract_eq(model))
})