forked from brycemcd/appliedpredictivemodeling
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch18.R
More file actions
125 lines (104 loc) · 3.37 KB
/
Copy pathch18.R
File metadata and controls
125 lines (104 loc) · 3.37 KB
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
# Ch 18 - measuring predictor importance
library('AppliedPredictiveModeling')
library('minerva')
library("CORElearn")
data(solubility)
cor(solTrainXtrans$NumCarbon, solTrainY)
# columns with "FP" in them are categorical
fpCols <- grepl("FP", names(solTrainXtrans))
numericPreds <- names(solTrainXtrans)[!fpCols]
catPreds <- names(solTrainXtrans)[fpCols]
corrValues <- apply(solTrainXtrans[, numericPreds],
MARGIN = 2,
FUN = function(x, y) cor(x, y),
y = solTrainY)
head(corrValues)
plot(corrValues)
corrValuesSpearman <- apply(solTrainXtrans[, numericPreds],
MARGIN = 2,
FUN = function(x, y) cor(x, y, method = 'spearman'),
y = solTrainY)
head(corrValuesSpearman)
plot(corrValuesSpearman)
smoother <- loess(solTrainY ~ solTrainXtrans$NumCarbon)
smoother
xyplot(solTrainY ~ solTrainXtrans$NumCarbon,
type = c('p', 'smooth'),
xlab ='# Carbons',
ylab = 'Solubility'
)
loessResults <- filterVarImp(x = solTrainXtrans[, numericPreds],
y = solTrainY,
nonpara = TRUE)
head(loessResults)
# MIC
micValues <- mine(solTrainXtrans[, numericPreds], solTrainY)
names(micValues)
head(micValues$MIC)
head(micValues$MICR2)
# categorical outcomes can be t-tested easily:
t.test(solTrainY ~ solTrainXtrans$FP044)
getTstats <- function(x, y) {
tTest <- t.test(y~x)
out <- c(tStat = tTest$statistic, p = tTest$p.value)
out
}
tVals <- apply(solTrainXtrans[, fpCols],
MARGIN = 2,
FUN = getTstats,
y = solTrainY)
tVals
dim(tVals)
# switch dimensions
tVals <- t(tVals)
head(tVals)
## TODO volccn plpoot?
# Categorical Outcomes
data(segmentationData)
cellData <- subset(segmentationData, Case == "Train")
cellData$Case <- cellData$Cell <- NULL
head(names(cellData))
rocValues <- filterVarImp(x = cellData[, -1],
y = cellData$Class)
head(rocValues)
reliefValues <- attrEval(Class ~ .,
data = cellData,
estimator = "ReliefFequalK",
ReliefIterations = 50)
?attrEval # for estimator functions above
head(reliefValues)
perm <- permuteRelief(x = cellData[, -1],
y = cellData[, 1],
nperm = 500,
estimator = 'ReliefFequalK',
ReliefIterations = 50)
head(perm$permutations)
histogram( ~value|Predictor,
data = perm$permutations)
head(perm$standardized)
micValues <- mine(x = cellData[, -1],
y = ifelse(cellData$Class == "PS", 1, 0))
head(micValues$MIC)
# Odds Ratio
View(solTrainXtrans[, catPreds])
head(catPreds)
class(solTrainXtrans$FP004)
# 2x2 tables produce different output with the fisher test
filterTrain <- subset(training, AWAOREG %in% c(0, 1))
spTable <- table(filterTrain$AWAOREG,
filterTrain$CARAVAN)
spTable
fisher.test(spTable)
# 2xN tables just produce brief output, but p value can still be used
ciTable <- table(training$ABRAND, # 86 = Caravan
training$CARAVAN)
ciTable
fisher.test(ciTable)
chisq.test(ciTable)
# caret has a varImp function:
library('randomForest')
rfImp <- randomForest(Class ~ .,
data = cellData,
ntree = 200,
importance = TRUE)
head(varImp(rfImp))