-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelperfuncs.R
214 lines (148 loc) · 4.19 KB
/
helperfuncs.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
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
preclean_explore <- function(traindata,testdata)
{
#sink(file = "preclean-exploration.txt")
cat("=============================\n")
str(traindata)
cat("\n")
str(testdata)
trainfeatures <- names(traindata)
testfeatures <- names(testdata)
cat("\nFeatures in traindata that is not in testdata:")
print(trainfeatures[which(trainfeatures != testfeatures)])
cat("\nFeatures in testdata that is not in traindata:")
print(testfeatures[which(trainfeatures != testfeatures)])
#sink(NULL)
}
postclean_explore <- function(traindata,testdata)
{
# str(train, test)
# summary(train, test)
# feature plot
# classe with every variable
#sink(file = "preclean-exploration.txt")
cat("=============================\n")
str(traindata)
cat("\n")
str(testdata)
cat("=============================\n")
summary(traindata)
cat("=============================\n")
summary(testdata)
trainfeatures <- names(traindata)
testfeatures <- names(testdata)
cat("\nFeatures in traindata that is not in testdata:")
print(trainfeatures[which(trainfeatures != testfeatures)])
cat("\nFeatures in testdata that is not in traindata:")
print(testfeatures[which(trainfeatures != testfeatures)])
#sink(NULL)
}
eliminate_NAs <- function(tdata)
{
#if 80% of a feature variable is NA , eliminate it
features <- names(tdata)
cleant <- tdata
for (f in features)
{
nasum <- sum(is.na(tdata[f]))
if (nasum > 0.2*nrow(tdata[f]))
{
# remove it
colnum = which( colnames(cleant)==f )
cleant <- cleant[-colnum]
}
}
return (cleant)
}
eliminate_Nulls <- function(tdata)
{
#if 80% of a feature variable is Null , eliminate it
features <- names(tdata)
cleant <- tdata
for (f in features)
{
nasum <- sum(tdata[f] == "")
if (nasum > 0.2*nrow(tdata[f]))
{
# remove it
colnum = which( colnames(cleant)==f )
cleant <- cleant[-colnum]
}
}
return (cleant)
}
eliminate_zeroVarFactors <- function(tdata)
{
nzv <- nearZeroVar(tdata)
filteredtrain <- tdata[, -nzv]
return (filteredtrain)
}
printmodel_diagnostics <- function(modelObj, predicted, expected)
{
print(modelObj)
print("======out of sample error:======")
print(sum(predicted != expected)/length(expected))
print(confusionMatrix(predicted, expected))
}
#**************************
#return the rules of a tree
#**************************
getConds<-function(tree){
#store all conditions into a list
conds<-list()
#start by the terminal nodes and find previous conditions
id.leafs<-which(tree$status==-1)
j<-0
for(i in id.leafs){
j<-j+1
prevConds<-prevCond(tree,i)
conds[[j]]<-prevConds$cond
while(prevConds$id>1){
prevConds<-prevCond(tree,prevConds$id)
conds[[j]]<-paste(conds[[j]]," & ",prevConds$cond)
if(prevConds$id==1){
conds[[j]]<-paste(conds[[j]]," => ",tree$prediction[i])
break()
}
}
}
return(conds)
}
#**************************
#find the previous conditions in the tree
#**************************
prevCond<-function(tree,i){
if(i %in% tree$right_daughter){
id<-which(tree$right_daughter==i)
cond<-paste(tree$split_var[id],">",tree$split_point[id])
}
if(i %in% tree$left_daughter){
id<-which(tree$left_daughter==i)
cond<-paste(tree$split_var[id],"<",tree$split_point[id])
}
return(list(cond=cond,id=id))
}
#remove spaces in a word
collapse<-function(x){
x<-sub(" ","_",x)
return(x)
}
rforestmodel <- function(train, test) {
subDir="rforestmodel"
dir.create(file.path(mainDir, subDir), showWarnings = FALSE)
setwd(file.path(mainDir, subDir))
set.seed(33833)
model <- randomForest(classe ~ ., train)
pred_training <- predict(model, train)
printmodel_diagnostics(rforestmodel, pred_training, train$classe)
tree <- getTree(model,1,labelVar=TRUE)
#rename the name of the column
colnames(tree)<-sapply(colnames(tree),collapse)
rules<-getConds(tree)
print(rules)
}
do_some_visualisation <- function(tdata)
{
#pdf("mygraph.pdf", width=7, height=7)
featurePlot(x=tdata[,1:7], y=tdata$classe, plot="pairs")
#dev.off()
}