-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCARET.R
196 lines (143 loc) · 5.5 KB
/
CARET.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
#todo lo que puede hacer caret
library(caret)
str(iris)
library(AppliedPredictiveModeling)
transparentTheme(trans = .4)
featurePlot(x = iris[, 1:4],
y = iris$Species,
plot = "pairs",
## agrega la clave para cada tipo de flor
auto.key = list(columns = 3))
featurePlot(x = iris[, 1:4],
y = iris$Species,
plot = "ellipse", #encierra las clases
auto.key = list(columns = 3))
transparentTheme(trans = .9)
featurePlot(x = iris[, 1:4],
y = iris$Species,
plot = "density",
## Pass in options to xyplot() to
## make it prettier
scales = list(x = list(relation="free"),
y = list(relation="free")),
adjust = 1.5,
pch = "|",
layout = c(4, 1),
auto.key = list(columns = 3))
#gráficas de densidad superpuestas
transparentTheme(trans = .9)
featurePlot(x = iris[, 1:4],
y = iris$Species,
plot = "density", #indica el tipo de gráfica
scales = list(x = list(relation="free"),
y = list(relation="free")),
adjust = 1.5,
pch = "|",
layout = c(4, 1),
auto.key = list(columns = 3))
#graficos de caja
featurePlot(x = iris[, 1:4],
y = iris$Species,
plot = "box",
## Pass in options to bwplot()
scales = list(y = list(relation="free"),
x = list(rot = 90)),
layout = c(4,1 ),
#ahora se utiliza la base de Boston Housing para
#hacer regresion
library(mlbench)
data(BostonHousing)
regVar <- c("age", "lstat", "tax")
str(BostonHousing[, regVar])
#en este caso los predictores son continuos
#por lo que se puede utilizar diagrama de dispersión
#para cada uno de los predictos
theme1 <- trellis.par.get()
theme1$plot.symbol$col = rgb(.2, .2, .2, .4)
theme1$plot.symbol$pch = 16
theme1$plot.line$col = rgb(1, 0, 0, .7)
theme1$plot.line$lwd <- 2
trellis.par.set(theme1)
featurePlot(x = BostonHousing[, regVar],
y = BostonHousing$medv,
plot = "scatter",
layout = c(3, 1))
featurePlot(x = BostonHousing[, regVar],
y = BostonHousing$medv,
plot = "scatter",
type = c("p", "smooth"),
span = .5,
layout = c(3, 1))
library(earth)
data(etitanic)
head(model.matrix(survived ~ ., data = etitanic))
dummies <- dummyVars(survived ~ ., data = etitanic)
head(predict(dummies, newdata = etitanic))
data(mdrr)
data.frame(table(mdrrDescr$nR11))
#se aprecia que estos predictores pueden tener
#varianza cercana a cero y eso puede ser un problema
#cuando los conjntos de datos se parten para hacer
#cross validation o bootstrap
#la función nearZeroVar se utiliza para identificar
#variables con esta caracteristica
#el argumento saveMetrics se utiliza para mostrar detalles
#por default es false
nzv <- nearZeroVar(mdrrDescr, saveMetrics= TRUE)
nzv[nzv$nzv,][1:10,]
#La funcion dim muestra la dimensión de un objeto
dim(mdrrDescr)
nzv <- nearZeroVar(mdrrDescr)
filteredDescr <- mdrrDescr[, -nzv]
dim(filteredDescr)
#identificación de predictores correlacionados
#dada una matriz de correlación
#otros modelos se pueden beneficiar de la reducción del nivel
#de correlación entre los predictores
#la función findCorrelation marca predictores para a ser eliminados
descrCor <- cor(filteredDescr)
highCorr <- sum(abs(descrCor[upper.tri(descrCor)]) > .999)
descrCor <- cor(filteredDescr)
summary(descrCor[upper.tri(descrCor)])
highlyCorDescr <- findCorrelation(descrCor, cutoff = .75)
filteredDescr <- filteredDescr[,-highlyCorDescr]
descrCor2 <- cor(filteredDescr)
summary(descrCor2[upper.tri(descrCor2)])
#centralización y escalamiento
set.seed(96)
inTrain <- sample(seq(along = mdrrClass), length(mdrrClass)/2)
training <- filteredDescr[inTrain,]
test <- filteredDescr[-inTrain,]
trainMDRR <- mdrrClass[inTrain]
testMDRR <- mdrrClass[-inTrain]
preProcValues <- preProcess(training, method = c("center", "scale"))
trainTransformed <- predict(preProcValues, training)
testTransformed <- predict(preProcValues, test)
#TRANSFORMACIÓN DE PREDICTORES
#PCA
#EN ALGUNOS CASOS ES NECESARIO UTILIZAR EL ANALISIS DE COMPO
#NENTES PRINCIPALES PARA TRANSFORMAR LOS DATOS EN UN
#SUBESPACIO MÁS PEQUEÑO DONDE LAS VARIABLES NO ESTÉN CORRELACIONADAS
#preProcess puede aplicar la transformación incluyendo "pca" en el argumento
#method
#de manera similar, ICA analisis de componentes independientes
#también puede ser utilizado para encontrar nuevas variables
#que son combinaciones lineales del conjunto original
#tal que sus componentes son independientes
library(AppliedPredictiveModeling)
transparentTheme(trans = .4)
plotSubset <- data.frame(scale(mdrrDescr[, c("nC", "X4v")]))
xyplot(nC ~ X4v,
data = plotSubset,
groups = mdrrClass,
auto.key = list(columns = 2))
transformed <- spatialSign(plotSubset)
transformed <- as.data.frame(transformed)
xyplot(nC ~ X4v,
data = transformed,
groups = mdrrClass,
auto.key = list(columns = 2))
preProcValues2 <- preProcess(training, method = "BoxCox")
trainBC <- predict(preProcValues2, training)
testBC <- predict(preProcValues2, test)
preProcValues2