-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
144 lines (144 loc) · 6.58 KB
/
.Rhistory
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
keepcols.quad <- c("donated", "CNDOL1", "CNTRLIF", "CONLARG", "CONTRFST", "CNDOL2", "CNDOL3",
"CNTMLIF", "SEX", "CNMON1","CNMONF", "CNMONL", "ContType1",
"SolType1", "avg", "avgTime", "don2", "don3", "incr_don", "Region", "sq_CNDOL1",
"sq_CNTRLIF", "sq_CONLARG", "sq_CONTRFST",
"sq_CNDOL2", "sq_CNDOL3", "sq_CNTMLIF", "sq_CNMON1",
"sq_CNMONF", "sq_CNMONL", "sq_avg", "sq_avgTime")
paste(keepcols.quad,collapse='+')
paste(keepcols.quad,collapse=' + ')
#install.packages('ROCR')
library(ROCR)
library(pROC)
who = "dylan"
# Set appropriate file address
if(who=="kristin"){
address <- '/Users/kmeier92/Documents/Northwestern/fall2016/Predictive_Analytics/PredictiveProjectADDK/'
}
if(who=="adit"){
address <- ''
}
if(who=="dylan"){
address <- 'C:/Users/Dylan/Documents/Northwestern/Predictive Analytics I/PROJECT/'
}
if(who=="dustin"){
address <- 'C:/Users/Dustin/Documents/Northwestern/Predictive Analytics 1/PredictiveProjectADDK/'
}
# source R code that cleans the data as needed
setwd(address)
source(paste(address,'R/cleandata.R',sep=""))
# functions auc & ccr
source(paste(address,'R/helper.R',sep=""))
# call function to create the 2 datasets
thedata <- cleandata(dropNA=F)
donTRAINING_orig <- thedata$train
donTEST_orig <- thedata$test
#add quadratic and interaction terms to data
donTRAINING_orig <- addSecondDegree(donTRAINING_orig)
donTEST_orig <- addSecondDegree(donTEST_orig)
#####################################################
#####################################################
######################################################
# LOGISTIC REGRESSION MODEL
######################################################
#####################################################
#####################################################
# fit basic logistic regression model
# https://www.r-bloggers.com/evaluating-logistic-regression-models/
# what to do regression on
keepcols <- c("donated", "CNDOL1", "CNTRLIF", "CONLARG", "CONTRFST",
"CNDOL2", "CNDOL3", "CNTMLIF", "SEX", "CNMON1",
"CNMONF", "CNMONL", "ContType1", "SolType1",
"avg", "avgTime", "don2", "don3","incr_don", "Region")
#filter data sets to appropriate columns
donTRAINING <- donTRAINING_orig[,(names(donTRAINING_orig) %in% keepcols)]
donTEST <- donTEST_orig[,(names(donTEST_orig) %in% keepcols)]
### WHAT THE AUC IS
### CORRECT RATE
### # SIG PREDICTORS
### # TOTAL PREDICTORS
### ASSUME WE ARE USING THIS AS A MEASURE OF 'BEST MODEL'
logModel <- glm(donated ~ . , data = donTRAINING, family=binomial)
sum.mod <- summary(logModel)
num.pred <- nrow(sum.mod$coefficients)
auc(model=logModel) # 0.716036
ccr(model=logModel) # 0.7530394
##########################
# BACKWARDS STEPWISE
##########################
logModel <- glm(donated ~ . , data = donTRAINING, family=binomial)
backwards = step(logModel)
summary(backwards)
auc(model=backwards) # 0.716004
ccr(model=backwards) # 0.7531906
# final with NA
'donated ~ CNDOL1 + CNTRLIF + CONLARG + CONTRFST + CNDOL2 + CNTMLIF +
CNMON1 + CNMONF + CNMONL + avgTime + don2 + don3 + incr_don +
SEX + ContType1 + SolType1 + Region'
##########################
# FORWARD STEPWISE
##########################
nothing <- glm(donated ~ 1, data = donTRAINING, family=binomial)
forwards = step(nothing,
scope=list(lower=formula(nothing),upper=formula(logModel)),
direction="forward")
summary(forwards)
# final w/ NA
'
donated ~ CNMON1 + CNMONL + CNTMLIF + CNMONF + ContType1 + CONTRFST +
SolType1 + incr_don + SEX + don2 + avgTime + CNTRLIF + CNDOL2 +
CNDOL1 + CONLARG + don3 + Region
'
auc(model=forwards) # 0.716004
ccr(model=forwards) # 0.7531906
######################################################
# QUADRATIC TERMS ADDED
# http://stats.stackexchange.com/questions/28730/does-it-make-sense-to-add-a-quadratic-term-but-not-the-linear-term-to-a-model
######################################################
# USE OUTPUT FROM STEPWISE REGRESSION + SQUARE TERMS.
#"donated", "CNDOL1", "CNTRLIF", "CONLARG", "CONTRFST",
#"CNDOL2", "CNDOL3", "CNTMLIF", "SEX", "CNMON1",
#"CNMONF", "CNMONL", "ContType1", "SolType1",
#"avg", "avgTime", "don2", "don3","incr_don"
keepcols.quad <- c("donated", "CNDOL1", "CNTRLIF", "CONLARG", "CONTRFST", "CNDOL2", "CNDOL3",
"CNTMLIF", "SEX", "CNMON1","CNMONF", "CNMONL", "ContType1",
"SolType1", "avg", "avgTime", "don2", "don3", "incr_don", "Region", "sq_CNDOL1",
"sq_CNTRLIF", "sq_CONLARG", "sq_CONTRFST",
"sq_CNDOL2", "sq_CNDOL3", "sq_CNTMLIF", "sq_CNMON1",
"sq_CNMONF", "sq_CNMONL", "sq_avg", "sq_avgTime")
donTRAINING.quad <- donTRAINING_orig[,(names(donTRAINING_orig) %in% keepcols.quad)]
donTEST.quad <- donTEST_orig[,(names(donTEST_orig) %in% keepcols.quad)]
logModel.quad <- glm(donated ~ . , data = donTRAINING.quad, family=binomial)
sum.mod.quad <- summary(logModel.quad)
num.pred.quad <- nrow(sum.mod.quad$coefficients)
auc(model=logModel.quad, testdata = donTEST.quad) # 0.7194766
ccr(model=logModel.quad, testdata = donTEST.quad) # 0.7550656
###################################
# BACKWARDS STEPWISE - W/ QUADRATIC
###################################
logModel.quad <- glm(donated ~ . , data = donTRAINING.quad, family=binomial)
backwards.quad = backwards
backwards.quad = step(logModel.quad)
summary(backwards.quad)
'final w/ NA
donated ~ sq_CNDOL1 + sq_CONLARG + sq_CNDOL2 + sq_CNTMLIF + sq_CNMON1 +
sq_CNMONL + sq_avg + sq_avgTime + CNDOL1 + CNTRLIF + CONLARG +
CONTRFST + CNDOL2 + CNTMLIF + CNMON1 + CNMONF + CNMONL +
avg + avgTime + don2 + don3 + incr_don + SEX + ContType1 +
SolType1 + Region
'
auc(model=backwards.quad, testdata = donTEST.quad) # 0.7194993
ccr(model=backwards.quad, testdata = donTEST.quad) # 0.7550051
###################################
# ALL SECOND DEGREE TERMS ADDED (interaction and quadratic)
###################################
donTRAINING2 <- donTRAINING_orig
donTEST2 <- donTEST_orig
logModelInter <- glm(donated ~ . -targdol, data = donTRAINING2, family=binomial)
sum.mod2 <- summary(logModelInter)
num.pred2 <- nrow(sum.mod2$coefficients)
auc(model=logModelInter, testdata=donTEST2, testresponse = donTEST2$donated) # 0.7939676
ccr(model=logModelInter, testdata=donTEST2, testresponse = donTEST2$donated) # 0.7392989
paste(colnames(donTRAINING_orig),collapse = ' + ')
logModel.quad.log <- glm(donated ~ log(CNDOL1)+CNTRLIF+log(CONLARG)+log(CONTRFST+1)+log(CNDOL2+1)+CNDOL3+CNTMLIF+SEX+CNMON1+CNMONF+CNMONL+ContType1+SolType1+avg+avgTime+don2+don3+incr_don+Region+log(sq_CNDOL1)+sq_CNTRLIF+log(sq_CONLARG)+log(sq_CONTRFST+1)+log(sq_CNDOL2+1)+sq_CNDOL3+sq_CNTMLIF+sq_CNMON1+sq_CNMONF+sq_CNMONL+log(sq_avg)+sq_avgTime, data = donTRAINING.quad, family=binomial)
auc(model=logModel.quad.log, testdata=donTEST_orig, testresponse = donTEST_orig$donated) # 0.7939676
ccr(model=logModel.quad.log, testdata=donTEST_orig, testresponse = donTEST_orig$donated) # 0.720162