-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinsample.R
150 lines (139 loc) · 5.44 KB
/
insample.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
#' ---
#' title: "Model comparison and evalution, in-sample, for predicting successful community discharge from rehab by RSAs"
#' author: 'Timothy Reistetter ^1^, Alex Bokov ^1^, Susanne Schmidt ^1^, Mei-Lin Min ^1^'
#' abstract: |
#' | Comparing various proposed predictive models
#' documentclass: article
#' description: 'Manuscript'
#' clean: false
#' self_contained: true
#' number_sections: false
#' keep_md: true
#' fig_caption: true
#' css: 'production.css'
#' output:
#' html_document:
#' toc: true
#' toc_float: true
#' ---
#+ init, echo=FALSE, message=FALSE, warning=FALSE,results='hide'
# Init ----
debug <- 0;
knitr::opts_chunk$set(echo=debug>0, warning=debug>0, message=debug>0);
inputdata <- c(); source('config.R',local=T,echo = debug>0);
# inputdata <- c(dat0='data/SIM_SDOH_ZCTA.xlsx' # census data by ZCTA
# ,cx0='data/SIM_ALLCMS.csv' # RSA-ZCTA crosswalk
# ,rsa0='data/SIM_RSAv4 SCD RSRs.csv' # outcomes (RSR)
# ,dct0='data/data_dictionary.csv' # data dictionary
# ,dat1='SDOH_RSR_2013_prelim.csv' # the dat1 dataset
# ,dat2='SDOH_RSR_2013_scaled_prelim.csv' # the scaled version of
# );
# Load libraries ----
library(rio); library(dplyr); library(tidbits); # data handling
library(pander); library(broom); # formatting
#library(GGally);
#library(mice);
library(psych); # factor analysis
library(caret); # cross-validation
library(Boruta); # variable selection
library(nFactors); # optimal number of factors
# Make tables never split
panderOptions('table.split.table',Inf);
panderOptions('table.split.cells',Inf);
source('project_functions.R',local=T,echo = debug>0);
# Local project settings ----
# tweak base plot settings to avoid captions going off-screen
.par_default <- par(no.readonly = TRUE);
.par_borutaplot <- list(mar=c(0.5, 6, 1, 0.5), mgp=c(0, 0.2, 0), cex=0.9,
tcl=0.2);
# overwrite previously set values if needed
if(file.exists('local.config.R')){
source('local.config.R',local=TRUE,echo = debug>0);
if(exists('.local.inputdata')){
inputdata <- replace(inputdata,names(.local.inputdata),.local.inputdata)};
};
# Import data ----
# if variable selection not already done, run the script that performs it
if(!file.exists('variableselection.R.rdata')){
system('R --vanilla -q -s -f variableselection.R',ignore.stdout = debug==0
,ignore.stderr = debug==0,wait=TRUE,intern=TRUE)};
load('variableselection.R.rdata');
colorizeVars <- cbind(paste0('\\b',dct0$column,'\\b'),colorByList(dct0$column));
cGroupRename <- cbind(c('c_AHRQsocial','c_AHRQecon','c_AHRQedu','c_AHRQphysinfr'
,'c_AHRQhealth','c_AHRQgeo')
,c('Social','Economic','Education'
,'Physical Infrastructure','Health','Geography'));
#+ legend
formals(colorByList)$colorList %>% eval %>%
sprintf("[%s]{.%stext}",.,names(.)) %>% submulti(cGroupRename) %>%
cbind() %>%
pander(col.names='AHRQ Domain Key',justify='left');
# Model performance ----
#'
#' # SDI
#'
set.seed(project_seed);
sdi3tr <- train(RSR~sdi_score,data=dat3tr,method='lm'
,trControl=trainControl(method='repeatedcv',number=5,repeats=10));
#+ sdi3tr, comment=''
sdi3tr;
plot(dat3tr$RSR~predict(sdi3tr,dat3tr),xlab='Predicted',ylab='Observed');
#'
pander(sdi3tr$finalModel,caption='RSR ~ sdi_score');
#' # Stepwise
#'
set.seed(project_seed);
aic3tr <- train(aicdat3$call$formula,data=dat3tr,method='lm'
,trControl=trainControl(method='repeatedcv',number=5,repeats=10));
#+ aic3tr, comment=''
aic3tr;
plot(dat3tr$RSR~predict(aic3tr,dat3tr),xlab='Predicted',ylab='Observed');
#'
pander(aic3tr$finalModel
,caption = paste0('RSR ~ '
,submulti(as.character(terms(aic3tr$finalModel))[3] %>% gsub('`','',.)
,colorizeVars)));
# ,caption=aic3tr$call$form %>% eval %>%
# deparse(width.cutoff = 400));
#' # Manually Chosen
#'
set.seed(project_seed);
exp3tr <- train(lmstartdat3$call$formula,data=dat3tr,method='lm'
,trControl=trainControl(method='repeatedcv',number=5,repeats=10));
#+ exp3tr, comment=''
exp3tr;
plot(dat3tr$RSR~predict(exp3tr,dat3tr),xlab='Predicted',ylab='Observed');
#'
pander(exp3tr$finalModel
,caption = paste0('RSR ~ '
,submulti(as.character(terms(exp3tr$finalModel))[3] %>% gsub('`','',.)
,colorizeVars)));
# ,caption=exp3tr$call$form %>% eval %>% deparse %>%
# paste0(collapse=''));
#' # Using Factor Analysis
dat3trfa <- data.frame(RSR=dat3tr$RSR,fa3$scores);
.fa3form <- colnames(dat3trfa)[-1] %>% paste0(collapse='+') %>%
paste0('RSR~',.) %>% formula;
set.seed(project_seed);
fa3tr <- train(RSR~.,data=dat3trfa,method='lm'
,trControl=trainControl(method='repeatedcv',number=5,repeats=10));
#+ fa3tr, comment=''
fa3tr;
plot(dat3trfa$RSR~predict(fa3tr,dat3trfa),xlab='Predicted',ylab='Observed');
#'
pander(fa3tr$finalModel,caption=reformulate(labels(terms(fa3tr)),'RSR') %>%
deparse(width.cutoff=400));
#'
#' \
#' \
#' \
#' \
#' \
#' \
#'
#'
#' # Summary
#'
bind_rows(SDI=sdi3tr$results,Stepwise=aic3tr$results,`Manually Selected`=exp3tr$results
,`Factor Analysis`=fa3tr$results,.id='Model') %>%
select(-c('intercept')) %>% arrange(MAE) %>% pander()