-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautoload.R
135 lines (110 loc) · 3.72 KB
/
autoload.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
library(MASS)
library(fmsb)
library(caret)
library(doParallel)
library(Hmisc)
db <- read.csv('./data.csv')
genderDb <- split(db, db$cinsiyet)
bfpCols <- names(db)[grepl('yagyuz', names(db))]
totalFatCols <- names(db)[grepl('totyag', names(db))]
indexCols <- names(db)[grepl('indeks_', names(db))]
inputCols <- names(db)[! (names(db) %in% c(bfpCols, totalFatCols, indexCols,
'gozlemno', 'X')) ]
toFormula <- function(features, resp= 'DEXAyagyuz') {
return(as.formula(paste(resp, '~', paste(features, collapse='+'))))
}
scaleDb <- function(dataset, inputCols) {
return(as.data.frame(sapply(colnames(dataset), function(x) {
if(is.numeric(dataset[, x]) && (x %in% inputCols)) {
output <- scale(dataset[, x], center = TRUE, scale = TRUE)
} else {
output <- dataset[, x]
}
return(output)
})))
}
trTest <- function(dataset, p = 0.75, scale.data=F) {
dataset[, 'train'] <- ifelse(runif(nrow(dataset)) <= p, 1, 0)
trData <- dataset[dataset[, 'train'] == 1,]
tData <- dataset[dataset[, 'train'] == 0,]
trData[,'train'] <- NULL
tData[,'train'] <- NULL
if(scale.data) {
scaledtrain <- as.data.frame(lapply(trData, function(x) rep.int(NA, length(x))))
scaledtest <- as.data.frame(lapply(tData, function(x) rep.int(NA, length(x))))
for(feature in names(trData)) {
if(!(feature %in% inputCols[-2])) {
scaledtrain[, feature] <- trData[, feature]
scaledtest[, feature] <- tData[, feature]
next
}
x <- trData[, feature]
newRow <- c(sd(x), mean(x))
scaledtrain[, feature] <- (trData[, feature] - newRow[2]) / newRow[1]
scaledtest[, feature] <- (tData[, feature] - newRow[2]) / newRow[1]
}
trData <- scaledtrain
tData <- scaledtest
}
return(list(train=trData, test=tData))
}
rmse <- function(error){
sqrt(mean(error^2))
}
mae <- function(error){
mean(abs(error))
}
mape <- function(actual, predicted) {
mean(abs((actual - predicted)/actual))
}
rSquared <- function(truth, prediction) {
mTruth <- mean(truth)
SStot <- sum((truth-mTruth)^2)
SSres <- sum((truth-prediction)^2)
return(1 - SSres/SStot)
#return(cor(truth, predicted, method='pearsossn')^2)
}
fitResult <- function(predicted, truth, ...) {
error <- truth - predicted
data.frame(
...,
RMSE=rmse(error),
MAE=mae(error)
)
}
getTrControlSeeds <- function() {
seeds <- vector(mode = "list", length = 51)
for(i in 1:50) {
set.seed(1+i*100)
seeds[[i]]<- sample.int(n=1000, 81)
}
set.seed(1)
seeds[[51]]<-sample.int(1000, 1)
return(seeds)
}
seed <- 5
load('rfGA30-100.RData')
load('rf_sa.RData')
fms <- list(
`1`=list(
`All`=toFormula(inputCols[-2]),
`SimulatedAnnealing`= toFormula(rf_sa_groups$`1`$optVariables),
`GeneticAlgorithm`=toFormula(rf_groups$`1`$optVariables),
`forward`=toFormula(c('kilo', 'cev_kalca', 'dkk_biceps',
'dkk_quadriceps')),
`backward`=toFormula(c('cev_kalca', 'cev_uyluk', 'cev_elblegi',
'cev_kulacuzun', 'dkk_triceps', 'dkk_sscapula',
'dkk_quadriceps', 'yas'), resp='DEXAtotyag')
),
`2`=list(
`All`=toFormula(inputCols[-2]),
`SimulatedAnnealing`=toFormula(rf_sa_groups$`2`$optVariables),
`GeneticAlgorithm`=toFormula(rf_groups$`2`$optVariables),
`forward`=toFormula(c('kilo', 'cev_bel', 'dkk_triceps',
'dkk_sscapula', 'dkk_quadriceps', 'dkk_gogus')),
`backward`=toFormula(c('cev_boyun', 'cev_bel', 'cev_kulacuzun',
'dkk_sscapula', 'dkk_silliak',
'dkk_abdomen', 'dkk_quadriceps',
'dkk_gogus'), resp='DEXAtotyag')
)
)