@@ -162,3 +162,124 @@ test_that("half siblings nonsignificant is as expected", {
162162 expect_gt(object = get_p_value(results_ram ), expected = signif_threshold )
163163 expect_equal(get_p_value(results_fast ), get_p_value(results_ram ), tolerance = 0.005 )
164164})
165+
166+
167+ default_setup <- function () {
168+ set.seed(2023 )
169+ library(NlsyLinks )
170+ library(dplyr )
171+ data(data_flu_ses )
172+ link_pairs <- Links79PairExpanded %> %
173+ filter(RelationshipPath == " Gen1Housemates" & RFull == 0.5 )
174+ df_link <- CreatePairLinksSingleEntered(
175+ outcomeDataset = data_flu_ses ,
176+ linksPairDataset = link_pairs ,
177+ outcomeNames = c(" S00_H40" , " RACE" , " SEX" )
178+ ) %> %
179+ filter(! is.na(S00_H40_S1 ) & ! is.na(S00_H40_S2 )) %> %
180+ mutate(
181+ SEX_S1 = ifelse(SEX_S1 == 0 , " MALE" , " FEMALE" ),
182+ SEX_S2 = ifelse(SEX_S2 == 0 , " MALE" , " FEMALE" ),
183+ RACE_S1 = ifelse(RACE_S1 == 0 , " NONMINORITY" , " MINORITY" ),
184+ RACE_S2 = ifelse(RACE_S2 == 0 , " NONMINORITY" , " MINORITY" )
185+ ) %> %
186+ filter(RACE_S1 == RACE_S2 ) %> %
187+ group_by(ExtendedID ) %> %
188+ slice_sample() %> %
189+ ungroup()
190+ return (df_link )
191+ }
192+
193+
194+
195+
196+
197+
198+ test_that(" discord_data with sex coding returns expected columns and values" , {
199+
200+ set.seed(2023 )
201+ data(data_flu_ses )
202+
203+ df_link <- default_setup()
204+
205+ cat_sex <- discord_data(
206+ data = df_link ,
207+ outcome = " S00_H40" ,
208+ sex = " SEX" ,
209+ race = " RACE" ,
210+ demographics = " sex" ,
211+ predictors = NULL ,
212+ pair_identifiers = c(" _S1" , " _S2" ),
213+ coding_method = " both"
214+ )
215+
216+ expect_true(" SEX_binarymatch" %in% names(cat_sex ))
217+ expect_true(all(cat_sex $ SEX_binarymatch %in% c(0 , 1 )))
218+ expect_true(" SEX_multimatch" %in% names(cat_sex ))
219+ expect_true(all(cat_sex $ SEX_multimatch %in% c(" MALE" , " FEMALE" , " mixed" )))
220+ })
221+
222+ test_that(" discord_data with race coding returns expected columns and values" , {
223+ set.seed(2023 )
224+ data(data_flu_ses )
225+ df_link <- default_setup()
226+ # reuse df_link from above
227+ cat_race <- discord_data(
228+ data = df_link ,
229+ outcome = " S00_H40" ,
230+ sex = " SEX" ,
231+ race = " RACE" ,
232+ demographics = " race" ,
233+ predictors = NULL ,
234+ pair_identifiers = c(" _S1" , " _S2" ),
235+ coding_method = " both"
236+ )
237+
238+ expect_true(" RACE_binarymatch" %in% names(cat_race ))
239+ expect_true(all(cat_race $ RACE_binarymatch %in% c(0 , 1 )))
240+ expect_true(" RACE_multimatch" %in% names(cat_race ))
241+ # sample the distinct levels
242+ expect_setequal(unique(cat_race $ RACE_multimatch ),
243+ c(" NONMINORITY" , " MINORITY" ))
244+ })
245+
246+ test_that(" discord_data 'both' coding returns binary and multi columns" , {
247+ df_link <- default_setup()
248+ cd <- discord_data(
249+ data = df_link ,
250+ outcome = " S00_H40" ,
251+ sex = " SEX" ,
252+ race = " RACE" ,
253+ demographics = " both" ,
254+ predictors = NULL ,
255+ pair_identifiers = c(" _S1" , " _S2" ),
256+ coding_method = " both"
257+ )
258+ expect_true(all(c(" SEX_binarymatch" , " SEX_multimatch" ,
259+ " RACE_binarymatch" , " RACE_multimatch" ) %in%
260+ names(cd )))
261+ })
262+
263+ test_that(" discord_regression returns a model with coefficients" , {
264+ set.seed(2023 )
265+ data(data_flu_ses )
266+ df_link <- default_setup()
267+
268+ dr_mod <- discord_regression(
269+ data = df_link ,
270+ outcome = " S00_H40" ,
271+ sex = " SEX" ,
272+ race = " RACE" ,
273+ demographics = " both" ,
274+ predictors = NULL ,
275+ pair_identifiers = c(" _S1" , " _S2" ),
276+ coding_method = " multi"
277+ )
278+
279+ # class check
280+ expect_s3_class(dr_mod , c(" lm" , " discord_regression" ))
281+ # coefficient table is not empty
282+ coefs <- broom :: tidy(dr_mod )
283+ expect_true(nrow(coefs ) > 0 )
284+ })
285+
0 commit comments