Post

DP05 Comparison and Changes between Two Periods

Description

The present example shows a comparison between two periods: ‘previous’ and ‘current’. In an enrolled population, there can be new patients (present in the current period but not in the previous), unenrolled patients (present in the previous period but not in the current), or patients present in both periods. Using the R Dplyr functions, it’s possible to tally the number of patients in these three groups and further categorize these groups based on another characteristic, such as the practices they were linked to.

R Script - Comparison and Changes between Two Periods

Rename the Periods as Previous and Current. Join both tables.

1
2
3
4
5
6
7

dfMonth1 <- dfMonth1 %>% rename(Practice_Previous_Month = Practice_Name)
dfMonth2 <- dfMonth2 %>% rename(Practice_Current_Month = Practice_Name)

# Join using the common fields
comparison <- dfMonth1 %>% full_join(dfMonth2, #by=c('Medicaid.ID'), 
                    by = c('Medicaid.ID','Name','DOB','Age','Gender','Race'))

Rename the Periods as Previous and Current. Join both tables.

Add Fields to Identify groups ‘New’ or ‘Unenrolled’

1
2
3
4
comparison$Prev_Month <- ifelse(is.na(comparison$Practice_Previous_Month), 'NewPt', 'Practice')
comparison$Curr_Month <- ifelse(is.na(comparison$Practice_Current_Month), 'Unenrolled', 'Practice')

comparison %>% group_by(Prev_Month, Curr_Month) %>% tally()

Resulting Table

The next recap is obtained from comparing these two periods:

Prev_MonthCurr_Monthn
NewPtPractice541
PracticePractice4459
PracticeUnenrolled534

Comparison and Changes of Practices between Two Periods

For the patients that were in both periods, it is possible to identify who of them had a change of Practice, and group by combination of practices previous and current.

The following code will achieve this outcome, sorted by the most frequent combination at the top:

1
2
3
4
5
comparison %>% 
  filter(Practice_Previous_Month != Practice_Current_Month) %>% 
  group_by(Practice_Previous_Month, Practice_Current_Month) %>% 
  tally() %>% arrange(desc(n)) %>% filter(n>0)

Example Top Results by Groups of Changes

05 Main Results

Result: Comparison of Differences

Detail of the Main Combination of Changed Practices

05 Results

Result: Comparison of Differences

__

End of Post

This post is licensed under CC BY 4.0 by the author.