-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUseful functions for assessing fit.R
167 lines (115 loc) · 4.67 KB
/
Useful functions for assessing fit.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
library(pROC)
################################################################################
## ROC Analysis
################################################################################
ROCAnalysis = function ( bin.var, cont.var, last = TRUE,
ci = FALSE, plot = TRUE, ... )
{
## Checking dichotomous dependent variables
if ( length( unique (bin.var ) ) != 2 )
{ print ( bin.var ) ; stop ( "Variable is not binomial!" ) }
## ROC Analysis
if ( last == TRUE ) { roc.out <- roc( bin.var , cont.var, direction="<" ) }
else { roc.out <- roc( bin.var , cont.var, direction=">" ) }
## 95% IC AUC
w.auc = pROC::auc (roc.out)
if ( ci == TRUE ) { w.ci = ci.auc (roc.out) }
## Plot ROC (opcional)
if ( plot == TRUE )
{
dev.new()
plot( roc.out , legacy.axes = TRUE, col="red" , ... )
text (0.2, 0.2, paste("AUC = ", round(w.auc ,3 )))
}
## Output
if ( ci == TRUE ) { list ( AUC = w.auc[1] , CI.AUC = c( w.ci[1] , w.ci[3] ) ) }
else { list ( AUC = w.auc[1] ) }
}
################################################################################
## Error Rate
################################################################################
ErrRate = function ( obs, pred )
{
if ( length(obs) != length(pred) )
{ stop ( "Vectors must have the same length!" ) }
## Table
tt = table( obs, pred )
## Proportion of correct and incorrect classifications
accuracy = sum ( as.character(obs) == as.character(pred) )
accuracy = accuracy / sum(tt)
err.rate = 1 - accuracy
list( accuracy = accuracy, err.rate = err.rate )
}
################################################################################
## Evaluations methods for regression models (continous DV)
################################################################################
EvalRegr = function ( obs, pred )
{
if ( length(obs) != length(pred) )
{ stop ( "Vectors must have the same length!" ) }
## MSE and RMSE
mse = mean ( ( obs - pred ) ** 2 )
rmse = sqrt ( mse)
## Standard Error of the MSE
se.mse = sd ( ( obs - pred ) ** 2 ) / sqrt(length(obs))
## MAE
mae = mean ( abs( obs - pred ) )
## R2
R2 = cor ( obs, pred ) ** 2
## Outout
list( MSE = mse, RMSE = rmse, MAE = mae, R2 = R2, SE.MSE = se.mse )
}
################################################################################
## Diagnistic plots for regression models (continous DV)
################################################################################
DiagPlot = function ( obs, pred, ... )
{
if ( length(obs) != length(pred) )
{ stop ( "Vectors must have the same length!" ) }
## Residuals
res = obs - pred
## Graph of the observed versus predicted values
w.min = min ( pred, obs )
w.max = max ( pred, obs )
dev.new()
plot ( pred, obs,
xlim = c( w.min, w.max ), ylim = c( w.min, w.max ),
xlab="Predicted", ylab="Observed" , pch=16, ... )
abline ( b=1, a=0 )
## Graph of the residuals versus predicted values
dev.new()
plot ( pred, res,
xlab="Predicted", ylab="Residuals" , pch=16, ... )
abline ( h=0 )
}
################################################################################
## 2D graph of the region of decision of a classifier
################################################################################
DecisionPlot = function ( w.points, w.grid, w.pred, ... )
{
dev.new()
plot ( w.grid [w.pred == 0, 1 ] , w.grid [w.pred == 0, 2 ] , pch=16, cex=0.3,
xlab="X1", ylab="X2", col= "cadetblue1", xlim=c(-3, 4.5 ), ylim=c (-2.5, 3 ), ... )
points ( w.grid [w.pred == 1, 1 ] , w.grid [w.pred == 1, 2 ] , pch=16, cex=0.3,
col= "tan1" )
points ( w.points [ w.points$y == 0 , 1] , w.points [ w.points$y == 0 , 2] , col= "blue", pch=16, cex=0.8 )
points ( w.points [ w.points$y == 1 , 1] , w.points [ w.points$y == 1 , 2] , col= "red2", pch=16, cex=0.8 )
}
################################################################################
## Variable selection filter based on p-values with binary DV
################################################################################
Pval.class = function ( x.var , y.var )
{
if ( length(unique(y.var)) != 2 ) stop ( "DV must be binary" )
p.val = rep ( NA, ncol(x.var) )
for ( i in 1:ncol(x.var) )
{
## Continous predictors
if ( length(unique( x.var[ ,i] )) > 10 & is.factor( x.var[ ,i] ) == FALSE )
{ p.val [i] = t.test ( x.var[ ,i] ~ y.var )$p.value }
else
{ p.val [i] = chisq.test ( y.var , x.var[ ,i] )$p.value }
}
## Output
list ( p.val = p.val )
}