-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathThreadNet_Batch.R
More file actions
139 lines (98 loc) · 4.17 KB
/
Copy pathThreadNet_Batch.R
File metadata and controls
139 lines (98 loc) · 4.17 KB
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
##########################################################################################################
# THREADNET: Batch processing for larger data sets
# (c) 2017 Michigan State University. This software may be used according to the terms provided in the
# GNU General Public License (GPL-3.0) https://opensource.org/licenses/GPL-3.0?
# Absolutely no warranty!
##########################################################################################################
# Take a large number of patient visits and create a data frame where each row contains
# a set of variables that describes a set of one or more visits.
# This is just a test
#' Batch processing for larger numbers of threads
#'
#' ACHR stands for Antecedents of Complexity in Healthcare Routines. This is function is set up to compute process parameters on thousands of patient visits.
#'
#' @param inFileName name of file (CSV format) containing the raw thread data.
#'
#' @return data frame ready for further analysis
#'
#' @export
ACHR_batch_V1 <- function(inFileName){
# first read in the csv
rawOcc = fread(inFileName)
# HARD-CODED COLUMNS!! Should probably pass in as a parameters
TN = "threadID"
CFs = c("role","workstn","action")
DV= newColName(CFs)
# clean up the ocurrences, add week and month columns
occ = cleanOcc(rawOcc,CFs)
# make threads - this will also make a new column that combines the CFs
threadedOcc <- ThreadOccByPOV(occ,TN,CFs)
# may want to make threads with and without different CFs to define events, as well
# pick subsets -- typically just one thread at a time, but could be more
# write a function for this
criteria <-"threadID"
bucket_list <- make_buckets(threadedOcc, criteria)
# get the size (number of buckets)
N = length(bucket_list)
# pre-allocate the data.table. Tables are supposed to be faster.
ACHR = data.table(bucket=integer(N),
NEvents = integer(N),
NDiagnoses = integer(N),
NProcedures = integer(N),
visitStartTime = numeric(N), # might need special data type for time
VisitDuration=numeric(N), # might need special data type for time
NetComplexity=double(N),
CompressRatio = double(N),
Clinic = character(N),
PrimaryDiagnosis = character(N),
PayerType = character(N),
Provider = character(N) # might not be available
)
# Now add columns for the IVs. There will be three for each IV
# Add the IV columns
for (cf in CFs){
ACHR[, paste0(cf,"_count"):= double(N)]
ACHR[, paste0(cf,"_compression"):= double(N)]
ACHR[, paste0(cf,"_entropy"):= double(N)]
}
# loop through the buckets. Result will be data frame with one row per bucket
for (i in 1:N){
b = i # as.integer(bucket_list[i])
# select the threads that go in this bucket
df = threadedOcc[threadedOcc[[TN]] ==bucket_list[i],]
# bucket number
ACHR[b,bucket := b]
# length of the thread (number of rows)
ACHR[b,NEvents := nrow(df)]
# only do the computations if there are more than two occurrences
if (nrow(df) > 2) {
# compressibility of DV
ACHR[b,CompressRatio := compression_index(df,DV)]
# NetComplexity of DV
# First get the network
n = threads_to_network(df,TN, DV)
ACHR[b,NetComplexity := estimate_network_complexity( n )]
# compute stuff on each context factor
for (cf in CFs){
# Count the unique elements in each cf
ACHR[b, paste0(cf,"_count") := length(unique(df[[cf]])) ]
# get the compression
ACHR[b, paste0(cf,"_compression") := compression_index(df,cf) ]
# get the entropy
ACHR[b, paste0(cf,"_entropy") := compute_entropy(table(df[[cf]])[table(df[[cf]])>0]) ]
}
} # kf nrows > 2
} # loop thru buckets
# return the table
return(ACHR)
}
# Each bucket is a list of thread numbers that can be used to subset the list of occurrences
make_buckets <- function(o, criteria){
return( levels(o[[criteria]]) )
}
make_box_plots <- function(){
library("ggpubr")
ggboxplot(ACHR_test[NEvents>100 & Clinic=='DRH'], x = "VisitMonth", y = "NetComplexity",
color = "VisitDay",
ylab = "Complexity", xlab = "Month (DRH)")
}