data("mtcars")
Z helpu:
The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models).
A data frame with 32 observations on 11 (numeric) variables.
[, 1] mpg Miles/(US) gallon
[, 2] cyl Number of cylinders
[, 3] disp Displacement (cu.in.)
[, 4] hp Gross horsepower
[, 5] drat Rear axle ratio
[, 6] wt Weight (1000 lbs)
[, 7] qsec 1/4 mile time
[, 8] vs Engine (0 = V-shaped, 1 =
straight)
[, 9] am Transmission (0 = automatic, 1 =
manual)
[,10] gear Number of forward gears
[,11] carb Number of carburetors
str(mtcars)
## 'data.frame': 32 obs. of 11 variables:
## $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
## $ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
## $ disp: num 160 160 108 258 360 ...
## $ hp : num 110 110 93 110 175 105 245 62 95 123 ...
## $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
## $ wt : num 2.62 2.88 2.32 3.21 3.44 ...
## $ qsec: num 16.5 17 18.6 19.4 17 ...
## $ vs : num 0 0 1 1 0 1 0 1 1 1 ...
## $ am : num 1 1 1 0 0 0 0 0 0 0 ...
## $ gear: num 4 4 4 3 3 3 3 4 4 4 ...
## $ carb: num 4 4 1 1 2 1 4 2 2 4 ...
Momentálne majú všetky čísla typ numeric. My však
potrebujeme, aby bolo napríklad jasné, že am môže nadobúdať
len hodnoty 0 a 1 (nie ľubovoľné reálne - model by zbehol, chybu by
nezahlásil, ale odhadovalo by sa niečo iné) v závislosti od toho, či je
prevodovka automatická alebo manuálna. Upravíme premenné, ktoré budú
vystupovať v našich modeloch: cyl, vs a
am:
mtcars$cyl <- factor(mtcars$cyl)
mtcars$vs <- factor(mtcars$vs)
mtcars$am <- factor(mtcars$am)
Vytvoríme novú premennú, ktorú sa budeme snažiť predikovať pomocou
ostatných - či má auto napriemernú hodnotu mpg, teda
nadpriemerne dobrú spotrebu.
mtcars$mpg_nadpriemer <- (mtcars$mpg > mean(mtcars$mpg))
Rozdelíme dáta na trénovaciu a testovaciu časť rovnako ako na prednáške a až do zostavenia finálneho modelu sa budeme pozerať len na trénovacie dáta:
library(caret)
set.seed(123)
mtcars_index <- createDataPartition(mtcars$mpg_nadpriemer,
p = 0.8, # podiel dat v trenovacej casti
list = FALSE)
mtcars_train <- mtcars[mtcars_index, ]
mtcars_test <- mtcars[-mtcars_index, ]
nrow(mtcars_train) # pocet dat v trenovacej casti
## [1] 27
nrow(mtcars_test) # pocet dat v testovacej casti
## [1] 5
table(mtcars_train$mpg_nadpriemer)
##
## FALSE TRUE
## 15 12
# balik na pracu s naivnym Bayesovym modelom
library(e1071)
Použijeme nasledovné dve premenné - typ prevodovky a motora:
table(mtcars_train[, c("mpg_nadpriemer", "am")])
## am
## mpg_nadpriemer 0 1
## FALSE 13 2
## TRUE 2 10
table(mtcars_train[, c("mpg_nadpriemer", "vs")])
## vs
## mpg_nadpriemer 0 1
## FALSE 12 3
## TRUE 3 9
Úlohy.
naiveBayes z balíka
e1071.bayes_1 <- naiveBayes(mpg_nadpriemer ~ am + vs, data = mtcars_train)
mtcars_test[1, c("am", "vs")]
## am vs
## Hornet 4 Drive 0 1
p_false_citatel <- (15/27) * (13/15) * (3/15)
p_true_citatel <- (12/27) * (2/12) * (9/12)
p <- c(p_false_citatel, p_true_citatel)
p <- p/sum(p)
names(p) <- c("p_false", "p_true")
p
## p_false p_true
## 0.6341463 0.3658537
Na kontrolu:
predict(bayes_1, newdata = mtcars_test[1, ], type = "raw")
## FALSE TRUE
## [1,] 0.6341463 0.3658537
TRUE/FALSE pre celú testovaciu časť
dát a porovnajte predikcie so skutočnými hodnotami.predikcie <- predict(bayes_1, newdata = mtcars_test)
table(predikcie, mtcars_test$mpg_nadpriemer)
##
## predikcie FALSE TRUE
## FALSE 2 2
## TRUE 1 0
table(predikcie = predikcie, skutocnost = mtcars_test$mpg_nadpriemer)
## skutocnost
## predikcie FALSE TRUE
## FALSE 2 2
## TRUE 1 0
Spravte model, v ktorom použijete typ prevodovky (premenná
am) a počet valcov motora (premenná cyl).
Odhadnite ho na trénovacích dátach a spravte predikciu pre testovacie
dáta.
bayes_2 <- naiveBayes(mpg_nadpriemer ~ am + cyl, data = mtcars_train)
predikcie_2 <- predict(bayes_2, newdata = mtcars_test)
table(predikcie = predikcie_2, skutocnost = mtcars_test$mpg_nadpriemer)
## skutocnost
## predikcie FALSE TRUE
## FALSE 3 1
## TRUE 0 1