-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththing3.R
More file actions
55 lines (44 loc) · 1.2 KB
/
thing3.R
File metadata and controls
55 lines (44 loc) · 1.2 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
library(data.table)
library(ElemStatLearn)
library(randomForest)
library(caret)
otrn <- data.table(read.csv("pml-training.csv"))
otst <- data.table(read.csv("pml-testing.csv"))
cnstst <- colnames(otrn)[7:59]
cnstrn <- c()
lntrn <- length(otrn)
lntst <- length(otst)
ntrn <- data.table(user_name=otrn[["user_name"]],classe=otrn[["classe"]])
ntst <- data.table(user_name=otst[["user_name"]])
for (i in 1:length(cnstst))
{
cn <- cnstst[[i]]
if (cn=="num_window") next
clstst <- class(otst[[cn]])
clstrn <- class(otrn[[cn]])
if (clstst!="numeric" && clstst !="integer")
{
print(sprintf("%d tst %s is not numeric/integer but %s",i,cn,clstst))
next
}
if (clstrn!="numeric" && clstrn !="integer")
{
print(sprintf("%d trn %s is not numeric/integer but %s",i,cn,clstrn))
next
}
cnstrn <- c(cnstrn,cn)
ntrn[[cn]] <- otrn[[cn]]
ntst[[cn]] <- otst[[cn]]
}
summary(ntrn)
ona <- sum(is.na(otrn))
nna <- sum(is.na(ntrn))
msg <- sprintf("Original training na count:%d - After processing:%d",ona,nna)
print(msg)
set.seed(2718)
fit <- randomForest(classe ~ ., ntrn, importance=T)
ptrn <- predict(fit, ntrn)
confusionMatrix(ptrn, ntrn$classe)
varImpPlot(fit)
ptst <- predict(fit, ntst)
ptst