-
Notifications
You must be signed in to change notification settings - Fork 449
/
mlces.Rmd
287 lines (204 loc) · 8.26 KB
/
mlces.Rmd
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# Medical Large Claims Experience Study (MLCES) {-}
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) <a href="https://github.com/asdfree/mlces/actions"><img src="https://github.com/asdfree/mlces/actions/workflows/r.yml/badge.svg" alt="Github Actions Badge"></a>
A high quality dataset of medical claims from seven private health insurance companies.
* One table with one row per individual with nonzero total paid charges.
* A convenience sample of group (employer-sponsored) health insurers in the United States.
* 1997 thru 1999 with no expected updates in the future.
* Provided by the [Society of Actuaries (SOA)](http://www.soa.org/).
---
## Recommended Reading {-}
Two Methodology Documents:
> [Group Medical Insurance Claims Database Collection and Analysis Report](https://www.soa.org/4937d6/globalassets/assets/files/research/exp-study/large_claims_report.pdf)
> [Claim Severities, Claim Relativities, and Age: Evidence from SOA Group Health Data](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=1412243)
<br>
One Haiku:
```{r}
# skewed by black swan tails
# means, medians sing adieu
# claims distribution
```
---
## Download, Import, Preparation {-}
Download and import the 1999 medical claims file:
```{r eval = FALSE , results = "hide" }
tf <- tempfile()
this_url <- "https://www.soa.org/Files/Research/1999.zip"
download.file( this_url , tf , mode = 'wb' )
unzipped_file <- unzip( tf , exdir = tempdir() )
mlces_df <- read.csv( unzipped_file )
names( mlces_df ) <- tolower( names( mlces_df ) )
```
### Save Locally \ {-}
Save the object at any point:
```{r eval = FALSE , results = "hide" }
# mlces_fn <- file.path( path.expand( "~" ) , "MLCES" , "this_file.rds" )
# saveRDS( mlces_df , file = mlces_fn , compress = FALSE )
```
Load the same object:
```{r eval = FALSE , results = "hide" }
# mlces_df <- readRDS( mlces_fn )
```
### Variable Recoding {-}
Add new columns to the data set:
```{r eval = FALSE , results = "hide" }
mlces_df <-
transform(
mlces_df ,
one = 1 ,
claimant_relationship_to_policyholder =
ifelse( relation == "E" , "covered employee" ,
ifelse( relation == "S" , "spouse of covered employee" ,
ifelse( relation == "D" , "dependent of covered employee" , NA ) ) ) ,
ppo_plan = as.numeric( ppo == 'Y' )
)
```
---
## Analysis Examples with base R \ {-}
### Unweighted Counts {-}
Count the unweighted number of records in the table, overall and by groups:
```{r eval = FALSE , results = "hide" }
nrow( mlces_df )
table( mlces_df[ , "claimant_relationship_to_policyholder" ] , useNA = "always" )
```
### Descriptive Statistics {-}
Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
mean( mlces_df[ , "totpdchg" ] )
tapply(
mlces_df[ , "totpdchg" ] ,
mlces_df[ , "claimant_relationship_to_policyholder" ] ,
mean
)
```
Calculate the distribution of a categorical variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
prop.table( table( mlces_df[ , "patsex" ] ) )
prop.table(
table( mlces_df[ , c( "patsex" , "claimant_relationship_to_policyholder" ) ] ) ,
margin = 2
)
```
Calculate the sum of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
sum( mlces_df[ , "totpdchg" ] )
tapply(
mlces_df[ , "totpdchg" ] ,
mlces_df[ , "claimant_relationship_to_policyholder" ] ,
sum
)
```
Calculate the median (50th percentile) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
quantile( mlces_df[ , "totpdchg" ] , 0.5 )
tapply(
mlces_df[ , "totpdchg" ] ,
mlces_df[ , "claimant_relationship_to_policyholder" ] ,
quantile ,
0.5
)
```
### Subsetting {-}
Limit your `data.frame` to persons under 18:
```{r eval = FALSE , results = "hide" }
sub_mlces_df <- subset( mlces_df , ( ( claimyr - patbrtyr ) < 18 ) )
```
Calculate the mean (average) of this subset:
```{r eval = FALSE , results = "hide" }
mean( sub_mlces_df[ , "totpdchg" ] )
```
### Measures of Uncertainty {-}
Calculate the variance, overall and by groups:
```{r eval = FALSE , results = "hide" }
var( mlces_df[ , "totpdchg" ] )
tapply(
mlces_df[ , "totpdchg" ] ,
mlces_df[ , "claimant_relationship_to_policyholder" ] ,
var
)
```
### Regression Models and Tests of Association {-}
Perform a t-test:
```{r eval = FALSE , results = "hide" }
t.test( totpdchg ~ ppo_plan , mlces_df )
```
Perform a chi-squared test of association:
```{r eval = FALSE , results = "hide" }
this_table <- table( mlces_df[ , c( "ppo_plan" , "patsex" ) ] )
chisq.test( this_table )
```
Perform a generalized linear model:
```{r eval = FALSE , results = "hide" }
glm_result <-
glm(
totpdchg ~ ppo_plan + patsex ,
data = mlces_df
)
summary( glm_result )
```
---
## Replication Example {-}
This example matches statistics in Table II-A's 1999 row numbers 52 and 53 from the [Database](https://www.soa.org/4937cc/globalassets/assets/files/research/tables.zip):
Match Claimants Exceeding Deductible:
```{r eval = FALSE , results = "hide" }
# $0 deductible
stopifnot( nrow( mlces_df ) == 1591738 )
# $1,000 deductible
mlces_above_1000_df <- subset( mlces_df , totpdchg > 1000 )
stopifnot( nrow( mlces_above_1000_df ) == 402550 )
```
Match the Excess Charges Above Deductible:
```{r eval = FALSE , results = "hide" }
# $0 deductible
stopifnot( round( sum( mlces_df[ , 'totpdchg' ] ) , 0 ) == 2599356658 )
# $1,000 deductible
stopifnot( round( sum( mlces_above_1000_df[ , 'totpdchg' ] - 1000 ) , 0 ) == 1883768786 )
```
---
## Analysis Examples with `dplyr` \ {-}
The R `dplyr` library offers an alternative grammar of data manipulation to base R and SQL syntax. [dplyr](https://github.com/tidyverse/dplyr/) offers many verbs, such as `summarize`, `group_by`, and `mutate`, the convenience of pipe-able functions, and the `tidyverse` style of non-standard evaluation. [This vignette](https://cran.r-project.org/web/packages/dplyr/vignettes/dplyr.html) details the available features. As a starting point for MLCES users, this code replicates previously-presented examples:
```{r eval = FALSE , results = "hide" }
library(dplyr)
mlces_tbl <- as_tibble( mlces_df )
```
Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
mlces_tbl %>%
summarize( mean = mean( totpdchg ) )
mlces_tbl %>%
group_by( claimant_relationship_to_policyholder ) %>%
summarize( mean = mean( totpdchg ) )
```
---
## Analysis Examples with `data.table` \ {-}
The R `data.table` library provides a high-performance version of base R's data.frame with syntax and feature enhancements for ease of use, convenience and programming speed. [data.table](https://r-datatable.com) offers concise syntax: fast to type, fast to read, fast speed, memory efficiency, a careful API lifecycle management, an active community, and a rich set of features. [This vignette](https://cran.r-project.org/web/packages/data.table/vignettes/datatable-intro.html) details the available features. As a starting point for MLCES users, this code replicates previously-presented examples:
```{r eval = FALSE , results = 'hide' }
library(data.table)
mlces_dt <- data.table( mlces_df )
```
Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = 'hide' }
mlces_dt[ , mean( totpdchg ) ]
mlces_dt[ , mean( totpdchg ) , by = claimant_relationship_to_policyholder ]
```
---
## Analysis Examples with `duckdb` \ {-}
The R `duckdb` library provides an embedded analytical data management system with support for the Structured Query Language (SQL). [duckdb](https://duckdb.org) offers a simple, feature-rich, fast, and free SQL OLAP management system. [This vignette](https://duckdb.org/docs/api/r) details the available features. As a starting point for MLCES users, this code replicates previously-presented examples:
```{r eval = FALSE , results = 'hide' }
library(duckdb)
con <- dbConnect( duckdb::duckdb() , dbdir = 'my-db.duckdb' )
dbWriteTable( con , 'mlces' , mlces_df )
```
Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = 'hide' }
dbGetQuery( con , 'SELECT AVG( totpdchg ) FROM mlces' )
dbGetQuery(
con ,
'SELECT
claimant_relationship_to_policyholder ,
AVG( totpdchg )
FROM
mlces
GROUP BY
claimant_relationship_to_policyholder'
)
```