-
Notifications
You must be signed in to change notification settings - Fork 0
/
coding.R
37 lines (31 loc) · 898 Bytes
/
coding.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
library(caret)
training<-ns
#removing zero variance
nsv<-nearZeroVar(training,saveMetrics=TRUE)
tmp<-training[,!nsv[,4]]
#removing the participant name and time (6 first columns)
tmp<-tmp[,-c(1:6)]
#removing sparcity of NA
removeNullColumn<-function(data){
loop<-dim(data)[2]
index<-logical(loop)
for(i in 1:loop){
if(sum(is.na(data[,i]))>(length(data[,i])/2)){
index[i]<-TRUE
}
}
data[,!index]
}
tmp<-removeNullColumn(tmp)
#create data partition
sepIndex<-createDataPartition(tmp$classe,p=0.60,list=FALSE)
#create test and training set
trainingset<-tmp[sepIndex,]
testingset<-tmp[-sepIndex,]
#cross validation
ctrl<-trainControl(method="cv",number=10)
#create the model fit
modelFit<-train(classe~.,method="rf",data=trainingset,trControl=ctrl)
print(modelFit$finalModel)
prediction<-predict(modelFit,newdata=testingset)
confusionMatrix(prediction, testingset$classe)