-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyFun.R
191 lines (175 loc) · 7.76 KB
/
myFun.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#### My Functions
####
## Hongfei Li ####
## [email protected] ####
## Script create date: 7/29/2018
##
## Total time spent on writing this script: 3 hour 00 min
##
########## Get Medical Claim ##########
#### Please notice, read all column for both 2016 and 2017 year will EXCEED MEMORY!!!!
#### Please notice, read all column for both 2016 and 2017 year will EXCEED MEMORY!!!!
#### Please notice, read all column for both 2016 and 2017 year will EXCEED MEMORY!!!!
GetMC <- function(year = 2017,
select = NULL,
class = 'measure',
dir.raw = 'E:/CT APCD/Hongfei/output/APCD by month/'){
## get MC for measurement/calendar year
## e.g. measurement year: 2017 refers to Oct.2016-Sep.2017
## e.g. calendar year: 2017 refers to Jan.2017-Dec.2017
##
require(data.table)
if (class == 'measure'){
pathchr <- paste0(dir.raw, 'MEDICAL_',
c(paste0(year-1, "_", sprintf("%02d", 10:12)), paste0(year, "_", sprintf("%02d", 1:9))),
".csv")
} else if (class == 'calendar'){
pathchr <- paste0(dir.raw, 'MEDICAL_',
c(paste0(year, "_", sprintf("%02d", 1:12))),
".csv")
} else if (!class %in% 'calendar'){
stop ('"class" can only be "measure" or "calendar"')
}
data <- data.table()
cat ('start reading data: total', num.chunk <- length(pathchr), 'chunks. \n', as.character(Sys.time()), '\n')
for (i in 1:num.chunk) {
data <- rbind(data, datachunk <- fread(pathchr[i],
header = TRUE,
select = select,
colClasses = 'character',
encoding = 'UTF-8'))
cat(i, '/', num.chunk, 'finished,' , as.character(Sys.time()), '\n')
}
return( data )
}
### Example ###
# col.keep <- c('MEDICAL_CLAIM_SERVICE_LINE_ID',
# 'INTERNAL_MEMBER_ID',
# 'PROCEDURE_CODE',
# 'MEDICAL_CLAIM_HEADER_ID',
# 'ORIG_NPI')
# MC_2016 <- GetMC(year = 2016, select = col.keep) #### Please notice, read all column for both 2016 and 2017 year will EXCEED MEMORY!!!!
# MC_2017 <- GetMC(year = 2017, select = col.keep) #### Please notice, read all column for both 2016 and 2017 year will EXCEED MEMORY!!!!
### End of Example ###
## aggreate MC and PC
GetClaim <- function(year = 2017,
type = NULL,
select = NULL,
class = 'measure',
dir.raw = 'E:/CT APCD/Hongfei/output/APCD by month'){
## get Claim data for measurement/calendar year
## e.g. measurement year: 2017 refers to Oct.2016-Sep.2017
## e.g. calendar year: 2017 refers to Jan.2017-Dec.2017
##
require(data.table)
if (! type %in% c('medical', 'pharmacy')) stop('"type" argument can only be "medical" or "pharmacy"')
type <- toupper(type)
if (class == 'measure'){
pathchr <- paste0(dir.raw, '/', type, '_',
c(paste0(year-1, "_", sprintf("%02d", 10:12)), paste0(year, "_", sprintf("%02d", 1:9))),
".csv")
} else if (class == 'calendar'){
pathchr <- paste0(dir.raw, '/', type, '_',
c(paste0(year, "_", sprintf("%02d", 1:12))),
".csv")
} else if (!class %in% 'calendar'){
stop ('"class" can only be "measure" or "calendar"')
}
data <- data.table()
cat ('start reading data: total', num.chunk <- length(pathchr), 'chunks. \n', as.character(Sys.time()), '\n')
for (i in 1:num.chunk) {
data <- rbind(data, datachunk <- fread(pathchr[i],
header = TRUE,
select = select,
colClasses = 'character',
encoding = 'UTF-8'))
cat(i, '/', num.chunk, 'finished,' , as.character(Sys.time()), '\n')
}
return(data)
}
################## Get Value Set ######################
GetMC_VS <- function(vsname,
year = 2017,
select = NULL,
class = 'measure',
dir.raw = 'E:/CT APCD/Hongfei/output/value_set/',
takeUnique = TRUE){
## get MC for specific Value Set for measurement/calendar year
## e.g. measurement year: 2017 refers to Oct.2016-Sep.2017
## e.g. calendar year: 2017 refers to Jan.2017-Dec.2017
##
dir.vs <- paste0(dir.raw, '#', vsname, '/')
if (!dir.exists(dir.vs)){
stop('"vsname" does not exists, please check it!! ')
}
dir.vs.procedure <- paste0(dir.vs, '#PROCEDURE_CODE/')
dir.vs.revenue <- paste0(dir.vs, '#REVENUE_CODE/')
pathchr <- character()
if (class == 'measure'){
if (dir.exists(dir.vs.procedure)){
pathchr <- c(pathchr, paste0(dir.vs.procedure, 'MEDICAL_',
c(paste0(year-1, "_", sprintf("%02d", 10:12)), paste0(year, "_", sprintf("%02d", 1:9))),
".csv"))
}
if (dir.exists(dir.vs.revenue)){
pathchr <- c(pathchr, paste0(dir.vs.revenue, 'MEDICAL_',
c(paste0(year-1, "_", sprintf("%02d", 10:12)), paste0(year, "_", sprintf("%02d", 1:9))),
".csv"))
}
if (length(pathchr) == 0){
stop('The "pathchr" is empty, please examine')
}
} else if (class == 'calendar'){
if (dir.exists(dir.vs.procedure)){
pathchr <- c(pathchr, paste0(dir.vs.procedure, 'MEDICAL_',
c(paste0(year, "_", sprintf("%02d", 1:12))),
".csv"))
}
if (dir.exists(dir.vs.revenue)){
pathchr <- c(pathchr, paste0(dir.vs.revenue, 'MEDICAL_',
c(paste0(year, "_", sprintf("%02d", 1:12))),
".csv"))
}
}
if (length(pathchr) == 0){
stop('The "pathchr" is empty, please examine')
} else if (!class %in% c('measure', 'calendar')){
stop ('"class" can only be "measure" or "calendar"')
}
data <- data.table()
cat ('start reading data: total', num.chunk <- length(pathchr), 'chunks. \n', as.character(Sys.time()), '\n')
if (takeUnique == TRUE){
for (i in 1:num.chunk) {
data <- unique(rbind(data, datachunk <- fread(pathchr[i],
header = TRUE,
select = select,
colClasses = 'character',
encoding = 'UTF-8')))
cat(i, '/', num.chunk, 'finished,' , as.character(Sys.time()), '\n')
}
} else if (takeUnique == FALSE){
for (i in 1:num.chunk) {
data <- rbind(data, datachunk <- fread(pathchr[i],
header = TRUE,
select = select,
colClasses = 'character',
encoding = 'UTF-8'))
cat(i, '/', num.chunk, 'finished,' , as.character(Sys.time()), '\n')
}
} else if (!takeUnique %in% c(TRUE, FALSE)){
stop('"takeUnique" option can only be TRUE or FALSE!! Please check!! ')
}
return(data)
}
### split NPI by specialty and give the number ###
#' nppes needs to get loaded
split_by_specialty <- function(dat, # data.table format with `NPI` column
row.name){
dat1 <- nppes[dat[!is.na(NPI), .(NPI, internal_member_id)], on = "NPI"]
dat2 <- taxo_info[, .(taxo, spec1)][dat1, on = c("taxo" = "Taxonomy1")]
freq <- table(factor(dat2$spec1, levels = spec.order))
freq1 <- matrix(freq, nrow = 1)
freq.tb <- data.table(AN = row.name, freq1)
colnames(freq.tb) <- c("AN", names(freq))
return(freq.tb)
}