Commit a11dd34c authored by Walmes Marques Zeviani's avatar Walmes Marques Zeviani
Browse files

Conclui tutorial de regressão logistica.

parent 45a87f32
...@@ -21,6 +21,7 @@ opts_chunk$set( ...@@ -21,6 +21,7 @@ opts_chunk$set(
* <https://machinelearningmastery.com/linear-classification-in-r/>; * <https://machinelearningmastery.com/linear-classification-in-r/>;
```{r} ```{r}
# Pacotes.
library(lattice) library(lattice)
library(latticeExtra) library(latticeExtra)
...@@ -46,6 +47,20 @@ xyplot(as.formula(f), ...@@ -46,6 +47,20 @@ xyplot(as.formula(f),
scales = list(x = list(relation = "free", log = FALSE))) + scales = list(x = list(relation = "free", log = FALSE))) +
latticeExtra::layer(panel.smoother(x, y, method = lm)) latticeExtra::layer(panel.smoother(x, y, method = lm))
xyplot(as.formula(f),
outer = TRUE,
data = na.omit(cre),
as.table = TRUE,
jitter.y = TRUE,
amount = 0.025,
scales = list(x = list(relation = "free", log = FALSE))) +
latticeExtra::layer({
mod <- glm(y ~ x, family = binomial)
xp <- seq(min(x), max(x), length.out = 101)
yp <- predict(mod, newdata = list(x = xp), type = "response")
panel.lines(x = xp, y = yp + 1)
})
# Visualiza contra as preditoras categóricas. # Visualiza contra as preditoras categóricas.
v <- names(cre)[1:15][sapply(cre[, -16], is.factor)] v <- names(cre)[1:15][sapply(cre[, -16], is.factor)]
length(v) length(v)
...@@ -68,9 +83,7 @@ layout(1) ...@@ -68,9 +83,7 @@ layout(1)
cre <- subset(cre, cre <- subset(cre,
select = setdiff(names(cre), select = setdiff(names(cre),
names(which(!keep)))) names(which(!keep))))
```
```{r}
# Casos completos. # Casos completos.
cc <- complete.cases(cre) cc <- complete.cases(cre)
table(cc) table(cc)
...@@ -100,6 +113,8 @@ tb ...@@ -100,6 +113,8 @@ tb
sum(diag(tb))/sum(tb) sum(diag(tb))/sum(tb)
``` ```
## Usando o pacote `caret`
* <https://topepo.github.io/caret/available-models.html> * <https://topepo.github.io/caret/available-models.html>
```{r, eval = FALSE} ```{r, eval = FALSE}
...@@ -144,6 +159,42 @@ confusionMatrix(yp, cre_test$y) ...@@ -144,6 +159,42 @@ confusionMatrix(yp, cre_test$y)
# Carrega o pacote. # Carrega o pacote.
library(VGAM) library(VGAM)
#-----------------------------------------------------------------------
# Usando um par de preditoras para visualizar a fronteira.
# Ajuste do modelo.
fit <- vglm(Species ~ Sepal.Length + Sepal.Width,
family = multinomial,
data = iris)
summary(fit)
grid <- with(iris,
expand.grid(
Sepal.Length = seq(min(Sepal.Length),
max(Sepal.Length),
length.out = 61),
Sepal.Width = seq(min(Sepal.Width),
max(Sepal.Width),
length.out = 61),
KEEP.OUT.ATTRS = FALSE))
prob <- predict(fit, newdata = grid, type = "response")
grid$pred <- apply(prob, MARGIN = 1, FUN = which.max)
grid$pred <- factor(grid$pred, labels = levels(iris$Species))
# Gráfico com pontos, classficações, fronteira e vetores de suporte.
plot(Sepal.Width ~ Sepal.Length,
data = grid,
col = as.integer(grid$pred),
pch = 3)
points(Sepal.Width ~ Sepal.Length,
data = iris,
col = as.integer(iris$Species),
pch = 19)
#-----------------------------------------------------------------------
# Usando todas as preditoras.
# Ajusta o modelo. # Ajusta o modelo.
fit <- vglm(Species ~ ., family = multinomial, data = iris) fit <- vglm(Species ~ ., family = multinomial, data = iris)
...@@ -156,7 +207,7 @@ pred <- apply(prob, MARGIN = 1, FUN = which.max) ...@@ -156,7 +207,7 @@ pred <- apply(prob, MARGIN = 1, FUN = which.max)
pred <- factor(pred, labels = levels(iris$Species)) pred <- factor(pred, labels = levels(iris$Species))
# Acurácia. # Acurácia.
confusionMatrix(pred, iris$Species) caret::confusionMatrix(pred, iris$Species)
``` ```
## Usando o `caret` ## Usando o `caret`
...@@ -173,7 +224,9 @@ data_train <- iris[intrain, ] ...@@ -173,7 +224,9 @@ data_train <- iris[intrain, ]
data_test <- iris[-intrain, ] data_test <- iris[-intrain, ]
# Parametriza a valiação cruzada. # Parametriza a valiação cruzada.
trctrl <- trainControl(method = "repeatedcv", number = 10, repeats = 3) trctrl <- trainControl(method = "repeatedcv",
number = 10,
repeats = 3)
# Penalized Multinomial Regression, usa a nnet::multinom(). # Penalized Multinomial Regression, usa a nnet::multinom().
set.seed(159) set.seed(159)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment