Вы находитесь на странице: 1из 24

add=T)

> legend("bottomright", inset=0.05, legend=c("Simple tree","Tree with priors"),


col=c("blue", "red"), lwd=3)
> ?legend
> str(perf1)
Formal class 'performance' [package "ROCR"] with 6 slots
..@ x.name
: chr "False positive rate"
..@ y.name
: chr "True positive rate"
..@ alpha.name : chr "Cutoff"
..@ x.values
:List of 1
.. ..$ : num [1:18] 0 0 0.0446 0.0706 0.0855 ...
..@ y.values
:List of 1
.. ..$ : num [1:18] 0 0.0153 0.1679 0.2443 0.2748 ...
..@ alpha.values:List of 1
.. ..$ : num [1:18] Inf 0.857 0.85 0.833 0.818 ...
> #Calcolo dell'AUC
> #Da notare che il calcolo approssimato
> mean(sample(fit2.prob[,2],1000,replace=T) > sample(fit2.prob[,1],1000,replace=
T))
[1] 0.089
> mean(sample(fit2.prob[,1],1000,replace=T) > sample(fit2.prob[,2],1000,replace=
T))
[1] 0.888
> #----------------------------------------------------------------------------> data = read.csv("/Users/michelesummo/Desktop/GestioneCSVs/credit.csv")
> set.seed(12345)
> rand.seq = runif(1000)
> str(rand.seq)
num [1:1000] 0.721 0.876 0.761 0.886 0.456 ...
> rand.seq.ordering = order(rand.seq)
> str(rand.seq.ordering)
int [1:1000] 14 448 697 32 196 83 119 602 443 945 ...
> credit.rand = data[rand.seq.ordering]
Errore in `[.data.frame`(data, rand.seq.ordering) :
undefined columns selected
> credit.rand = data[rand.seq.ordering,]
> credit.train = credit.rand[1:900,]
> credit.test = credit.rand[901:1000,]
> library(C50)
> credit.model = C5.0(credit.train[,-17],credit.train$default)
> summary(credit.model)
Call:
C5.0.default(x = credit.train[, -17], y = credit.train$default)
C5.0 [Release 2.07 GPL Edition]
-------------------------------

Fri Jan 3 22:37:22 2014

Class specified by attribute `outcome'


Read 900 cases (17 attributes) from undefined.data
Decision tree:
checking_balance = unknown: no (358/44)
checking_balance in {< 0 DM,> 200 DM,1 - 200 DM}:
:...credit_history in {perfect,very good}:

:...dependents > 1: yes (10/1)


: dependents <= 1:
: :...savings_balance = < 100 DM: yes (39/11)
:
savings_balance in {> 1000 DM,500 - 1000 DM,unknown}: no (8/1)
:
savings_balance = 100 - 500 DM:
:
:...checking_balance = < 0 DM: no (1)
:
checking_balance in {> 200 DM,1 - 200 DM}: yes (5/1)
credit_history in {critical,good,poor}:
:...months_loan_duration <= 11: no (87/14)
months_loan_duration > 11:
:...savings_balance = > 1000 DM: no (13)
savings_balance in {< 100 DM,100 - 500 DM,500 - 1000 DM,unknown}:
:...checking_balance = > 200 DM:
:...dependents > 1: yes (3)
: dependents <= 1:
: :...credit_history in {good,poor}: no (23/3)
:
credit_history = critical:
:
:...amount <= 2337: yes (3)
:
amount > 2337: no (6)
checking_balance = 1 - 200 DM:
:...savings_balance = unknown: no (34/6)
: savings_balance in {< 100 DM,100 - 500 DM,500 - 1000 DM}:
: :...months_loan_duration > 45: yes (11/1)
:
months_loan_duration <= 45:
:
:...other_credit = store:
:
:...age <= 35: yes (4)
:
: age > 35: no (2)
:
other_credit = bank:
:
:...years_at_residence <= 1: no (3)
:
: years_at_residence > 1:
:
: :...existing_loans_count <= 1: yes (5)
:
:
existing_loans_count > 1:
:
:
:...percent_of_income <= 2: no (4/1)
:
:
percent_of_income > 2: yes (3)
:
other_credit = none:
:
:...job = unemployed: no (1)
:
job = management:
:
:...amount <= 7511: no (10/3)
:
: amount > 7511: yes (7)
:
job = unskilled: [S1]
:
job = skilled:
:
:...dependents <= 1: no (55/15)
:
dependents > 1:
:
:...age <= 34: no (3)
:
age > 34: yes (4)
checking_balance = < 0 DM:
:...job = management: no (26/6)
job = unemployed: yes (4/1)
job = unskilled:
:...employment_duration in {4 - 7 years,
: :
unemployed}: no (4)
: employment_duration = < 1 year:
: :...other_credit = bank: no (1)
: : other_credit in {none,store}: yes (11/2)
: employment_duration = > 7 years:
: :...other_credit in {bank,none}: no (5/1)
: : other_credit = store: yes (2)
: employment_duration = 1 - 4 years:
: :...age <= 39: no (14/3)
:
age > 39:

:
:...credit_history in {critical,good}: yes (3)
:
credit_history = poor: no (1)
job = skilled:
:...credit_history = poor:
:...savings_balance in {< 100 DM,100 - 500 DM,
: :
500 - 1000 DM}: yes (8)
: savings_balance = unknown: no (1)
credit_history = critical:
:...other_credit = store: no (0)
: other_credit = bank: yes (4)
: other_credit = none:
: :...savings_balance in {100 - 500 DM,
:
:
unknown}: no (1)
:
savings_balance = 500 - 1000 DM: yes (1)
:
savings_balance = < 100 DM:
:
:...months_loan_duration <= 13:
:
:...percent_of_income <= 3: yes (3)
:
: percent_of_income > 3: no (3/1)
:
months_loan_duration > 13:
:
:...amount <= 5293: no (10/1)
:
amount > 5293: yes (2)
credit_history = good:
:...existing_loans_count > 1: yes (5)
existing_loans_count <= 1:
:...other_credit = store: no (2)
other_credit = bank:
:...percent_of_income <= 2: yes (2)
: percent_of_income > 2: no (6/1)
other_credit = none: [S2]
SubTree [S1]
employment_duration in {< 1 year,1 - 4 years}: yes (11/3)
employment_duration in {> 7 years,4 - 7 years,unemployed}: no (8)
SubTree [S2]
savings_balance = 100 - 500 DM: yes (3)
savings_balance = 500 - 1000 DM: no (1)
savings_balance = unknown:
:...phone = no: yes (9/1)
: phone = yes: no (3/1)
savings_balance = < 100 DM:
:...percent_of_income <= 1: no (4)
percent_of_income > 1:
:...phone = yes: yes (10/1)
phone = no:
:...purpose in {business,car0,education,renovations}: yes (3)
purpose = car:
:...percent_of_income <= 3: no (2)
: percent_of_income > 3: yes (6/1)
purpose = furniture/appliances:
:...years_at_residence <= 1: no (4)
years_at_residence > 1:
:...housing = other: no (1)
housing = rent: yes (2)
housing = own:
:...amount <= 1778: no (3)
amount > 1778:
:...years_at_residence <= 3: yes (6)

years_at_residence > 3: no (3/1)


Evaluation on training data (900 cases):
Decision Tree
---------------Size
Errors
66 125(13.9%)
(a) (b)
---- ---609
23
102 166

<<

<-classified as
(a): class no
(b): class yes

Attribute usage:
100.00%
60.22%
53.22%
49.44%
30.89%
25.89%
17.78%
9.67%
7.22%
6.67%
5.78%
5.56%
3.78%
3.44%
3.33%
1.67%

checking_balance
credit_history
months_loan_duration
savings_balance
job
other_credit
dependents
existing_loans_count
percent_of_income
employment_duration
phone
amount
years_at_residence
age
purpose
housing

Time: 0.0 secs


> ?summary
> credit.pred = predict(credit.model, credit.test)
> CrossTable(credit.test$default, credit.pred, prop.chisq=FALSE, prop.c=FALSE, p
rop.r=FALSE, dnn = c("actual default", "predicted default"))
Cell Contents
|-------------------------|
|
N |
|
N / Table Total |
|-------------------------|
Total Observations in Table: 100
| predicted default
actual default |
no |
yes | Row Total |
---------------|-----------|-----------|-----------|
no |
57 |
11 |
68 |

|
0.570 |
0.110 |
|
---------------|-----------|-----------|-----------|
yes |
16 |
16 |
32 |
|
0.160 |
0.160 |
|
---------------|-----------|-----------|-----------|
Column Total |
73 |
27 |
100 |
---------------|-----------|-----------|-----------|
> credit.model.rules = C5.0(credit.train[,-17], credit.train$default, rules=T)
> str(credit.model.rules)
List of 16
$ names
: chr "| Generated using R version 3.0.2 (2013-09-25)\n| on Ven G
en 03 22:41:52 2014\n| function call: makeNamesFile(x = x, y = y, lab"| __trunca
ted__
$ cost
: chr ""
$ costMatrix : NULL
$ caseWeights : logi FALSE
$ control
:List of 11
..$ subset
: logi TRUE
..$ bands
: num 0
..$ winnow
: logi FALSE
..$ noGlobalPruning: logi FALSE
..$ CF
: num 0.25
..$ minCases
: num 2
..$ fuzzyThreshold : logi FALSE
..$ sample
: num 0
..$ earlyStopping : logi TRUE
..$ label
: chr "outcome"
..$ seed
: int 3455
$ trials
: Named num [1:2] 1 1
..- attr(*, "names")= chr [1:2] "Requested" "Actual"
$ rbm
: logi TRUE
$ boostResults: NULL
$ size
: int 21
$ dims
: int [1:2] 900 16
$ call
: language C5.0.default(x = credit.train[, -17], y = credit.train
$default, rules = T)
$ levels
: chr [1:2] "no" "yes"
$ output
: chr "\nC5.0 [Release 2.07 GPL Edition] \tFri Jan 3 22:41:52 2
014\n-------------------------------\n\nClass specified by attribute "| __trunca
ted__
$ tree
: chr ""
$ predictors : chr [1:16] "checking_balance" "months_loan_duration" "credit_hi
story" "purpose" ...
$ rules
: chr "id=\"See5/C5.0 2.07 GPL Edition 2014-01-03\"\nentries=\"1\
"\nrules=\"21\" default=\"no\"\nconds=\"3\" cover=\"15\" ok=\"15\" li"| __trunca
ted__
- attr(*, "class")= chr "C5.0"
> library(hmeasure)
>
> pred = predict(credit.model.rules, credit.test)
> misclassCounts(pred, credit.true)
Errore in as.array(true.class) : oggetto "credit.true" non trovato
> misclassCounts(pred, credit.test$default)
$conf.matrix
pred.1 pred.0
actual.1
0
0
actual.0
0
0

$metrics
ER Sens Spec Precision Recall TPR FPR F Youden
1 NaN NaN NaN
NaN
NaN NaN NaN NaN
NaN
> misclassCounts(pred, credit.test$Default)
Errore in as.array.default(true.class) :
tentativo di impostare un attributo a NULL
> str(pred)
Factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 1 2 2 ...
> str(credit.test$default)
Factor w/ 2 levels "no","yes": 1 2 1 1 2 1 1 2 2 2 ...
> pred.unclass = unclass(pred) - 1
> true.unclass = unclass(credit.test$default) - 1
> misclassCounts(pred.unclass, true.unclass)
$conf.matrix
pred.1 pred.0
actual.1
15
17
actual.0
7
61
$metrics
ER
Sens
Spec Precision Recall
TPR
FPR
F
Youden
1 0.24 0.46875 0.8970588 0.6818182 0.46875 0.46875 0.1029412 0.5555556 0.3658088
> #------------------------------------------------------------------------> ?classAgreement
No documentation for classAgreement
in specified packages and libraries:
you could try ??classAgreement
> library(class)
> ?class
> class(iris3)
[1] "array"
> ?iris3
> train = rbind(iris3[1:25,,1],iris3[1:25,,2],iris3[1:25,,3])
> str(iris3)
num [1:50, 1:4, 1:3] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
- attr(*, "dimnames")=List of 3
..$ : NULL
..$ : chr [1:4] "Sepal L." "Sepal W." "Petal L." "Petal W."
..$ : chr [1:3] "Setosa" "Versicolor" "Virginica"
> str(iris3[1:25,,1])
num [1:25, 1:4] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:4] "Sepal L." "Sepal W." "Petal L." "Petal W."
> iris3[1:10,,1]
Sepal L. Sepal W. Petal L. Petal W.
[1,]
5.1
3.5
1.4
0.2
[2,]
4.9
3.0
1.4
0.2
[3,]
4.7
3.2
1.3
0.2
[4,]
4.6
3.1
1.5
0.2
[5,]
5.0
3.6
1.4
0.2
[6,]
5.4
3.9
1.7
0.4
[7,]
4.6
3.4
1.4
0.3
[8,]
5.0
3.4
1.5
0.2
[9,]
4.4
2.9
1.4
0.2
[10,]
4.9
3.1
1.5
0.1
> iris3[1:10,,1:""]
Errore in 1:"" : argomento NA/NaN
> iris3[1:10,,1:2]
, , Setosa

[1,]
[2,]
[3,]
[4,]
[5,]
[6,]
[7,]
[8,]
[9,]
[10,]

Sepal L. Sepal W. Petal L. Petal W.


5.1
3.5
1.4
0.2
4.9
3.0
1.4
0.2
4.7
3.2
1.3
0.2
4.6
3.1
1.5
0.2
5.0
3.6
1.4
0.2
5.4
3.9
1.7
0.4
4.6
3.4
1.4
0.3
5.0
3.4
1.5
0.2
4.4
2.9
1.4
0.2
4.9
3.1
1.5
0.1

, , Versicolor
[1,]
[2,]
[3,]
[4,]
[5,]
[6,]
[7,]
[8,]
[9,]
[10,]

Sepal L. Sepal W. Petal L. Petal W.


7.0
3.2
4.7
1.4
6.4
3.2
4.5
1.5
6.9
3.1
4.9
1.5
5.5
2.3
4.0
1.3
6.5
2.8
4.6
1.5
5.7
2.8
4.5
1.3
6.3
3.3
4.7
1.6
4.9
2.4
3.3
1.0
6.6
2.9
4.6
1.3
5.2
2.7
3.9
1.4

> str(train)
num [1:75, 1:4] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:4] "Sepal L." "Sepal W." "Petal L." "Petal W."
> test = rbind(iris3[26:50,,1],iris3[26:50,,2],iris3[26:50,,3])
> dim(train)
[1] 75 4
> dim(test)
[1] 75 4
> ?factor
> cl = factor(c(rep("S",25),rep("C",25),rep("V",25)))
> ?rep
> str(cl)
Factor w/ 3 levels "C","S","V": 2 2 2 2 2 2 2 2 2 2 ...
> ?knn
> pred_k3 = knn(train, test, cl, k=3, prob=F)
> pred_k3
[1] S S S S S S S S S S S S S S S S S S S S S S S S S C C V C C
C
[40] C C C C C C C C C C C V C C V V V V V C V V V V C V V V V V
Levels: C S V
> table(cl, pred_k3)
pred_k3
cl C S V
C 23 0 2
S 0 25 0
V 4 0 21
> pred_k1 = knn(train, test, cl, k=1, prob=F)
> pred_k1
[1] S S S S S S S S S S S S S S S S S S S S S S S S S C C C C C
C
[40] C C C C C C C C C C C V V C V V V V V C V V V V C V V V V V

C C C V C C C C
V V V V V V

C C C V C C C C
V V V V V V

Levels: C S V
> table(cl,pred_k1)
pred_k1
cl C S V
C 24 0 1
S 0 25 0
V 3 0 22
> ?diag
> #Accuratezza
> sum(diag(table(cl,pred_k1))) / sum(table(cl,pred_k1))
[1] 0.9466667
> accuracy.vector = numeric(20)
> accuracy.vector
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
> for(i in 1:20) {}
> for(i in 1:20) {
+
temp.pred = knn(train, test, cl, k=i, prob=F)
+
accuracy.vector[i] = sum(diag(table(cl,temp.pred))) / sum(table(cl,temp.pre
d))
+ }
> ?ts
> plot(ts(accuracy.vector), lwd=2)
> plot(ts(accuracy.vector), lwd=2)
> which.max(accuracy.vector)
[1] 2
> #------------------RITORNIAMO INDIETRO---------------------------------------> ts
function (data = NA, start = 1, end = numeric(), frequency = 1,
deltat = 1, ts.eps = getOption("ts.eps"), class = if (nseries >
1) c("mts", "ts", "matrix") else "ts", names = if (!is.null(dimnames(dat
a))) colnames(data) else paste("Series",
seq(nseries)))
{
if (is.data.frame(data))
data <- data.matrix(data)
if (is.matrix(data)) {
nseries <- ncol(data)
ndata <- nrow(data)
dimnames(data) <- list(NULL, names)
}
else {
nseries <- 1
ndata <- length(data)
}
if (ndata == 0)
stop("'ts' object must have one or more observations")
if (missing(frequency))
frequency <- 1/deltat
else if (missing(deltat))
deltat <- 1/frequency
if (frequency > 1 && abs(frequency - round(frequency)) <
ts.eps)
frequency <- round(frequency)
if (length(start) > 1L) {
start <- start[1L] + (start[2L] - 1)/frequency
}
if (length(end) > 1L) {
end <- end[1L] + (end[2L] - 1)/frequency
}
if (missing(end))

end <- start + (ndata - 1)/frequency


else if (missing(start))
start <- end - (ndata - 1)/frequency
if (start > end)
stop("'start' cannot be after 'end'")
nobs <- floor((end - start) * frequency + 1.01)
if (nobs != ndata)
data <- if (NCOL(data) == 1) {
if (ndata < nobs)
rep_len(data, nobs)
else if (ndata > nobs)
data[1L:nobs]
}
else {
if (ndata < nobs)
data[rep_len(1L:ndata, nobs), ]
else if (ndata > nobs)
data[1L:nobs, ]
}
attr(data, "tsp") <- c(start, end, frequency)
if (!is.null(class) && class != "none")
attr(data, "class") <- class
data
}
<bytecode: 0x10b5c5b10>
<environment: namespace:stats>
> ts(1:20)
Time Series:
Start = 1
End = 20
Frequency = 1
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
> ts(runif(20))
Time Series:
Start = 1
End = 20
Frequency = 1
[1] 0.0775995 0.9502470 0.1208655 0.7739790 0.7299722 0.8535475 0.6928949
[8] 0.1873828 0.4865965 0.7373482 0.8571920 0.9304711 0.1391016 0.7624440
[15] 0.7142200 0.6411454 0.3912392 0.0866840 0.2044846 0.1862390
> #----------------------------------------------------------------------> library(e1071)
> ?e1071
No documentation for e1071
in specified packages and libraries:
you could try ??e1071
> library(mlbench)
> ?HouseVotes884
No documentation for HouseVotes884
in specified packages and libraries:
you could try ??HouseVotes884
> ?HouseVotes84
> data(HouseVotes84)
> str(HouseVotes84)
'data.frame': 435 obs. of 17 variables:
$ Class: Factor w/ 2 levels "democrat","republican": 2 2 1 1 1 1 1 2 2 1 ...
$ V1 : Factor w/ 2 levels "n","y": 1 1 NA 1 2 1 1 1 1 2 ...
$ V2 : Factor w/ 2 levels "n","y": 2 2 2 2 2 2 2 2 2 2 ...
$ V3 : Factor w/ 2 levels "n","y": 1 1 2 2 2 2 1 1 1 2 ...
$ V4 : Factor w/ 2 levels "n","y": 2 2 NA 1 1 1 2 2 2 1 ...
$ V5 : Factor w/ 2 levels "n","y": 2 2 2 NA 2 2 2 2 2 1 ...
$ V6 : Factor w/ 2 levels "n","y": 2 2 2 2 2 2 2 2 2 1 ...

$
$
$
$
$
$
$
$
$
$

V7 : Factor w/ 2 levels "n","y": 1 1 1 1 1 1 1 1 1 2 ...


V8 : Factor w/ 2 levels "n","y": 1 1 1 1 1 1 1 1 1 2 ...
V9 : Factor w/ 2 levels "n","y": 1 1 1 1 1 1 1 1 1 2 ...
V10 : Factor w/ 2 levels "n","y": 2 1 1 1 1 1 1 1 1 1 ...
V11 : Factor w/ 2 levels "n","y": NA 1 2 2 2 1 1 1 1 1 ...
V12 : Factor w/ 2 levels "n","y": 2 2 1 1 NA 1 1 1 2 1 ...
V13 : Factor w/ 2 levels "n","y": 2 2 2 2 2 2 NA 2 2 1 ...
V14 : Factor w/ 2 levels "n","y": 2 2 2 1 2 2 2 2 2 1 ...
V15 : Factor w/ 2 levels "n","y": 1 1 1 1 2 2 2 NA 1 NA ...
V16 : Factor w/ 2 levels "n","y": 2 NA 1 2 2 2 2 2 2 NA ...
> model.nb = naiveBayes(Class ~ ., data=HouseVotes84)
> ?naiveBayes
> summary(model.nb)
Length Class Mode
apriori 2
table numeric
tables 16
-none- list
levels 2
-none- character
call
4
-none- call
> str(model.nb)
List of 4
$ apriori: 'table' int [1:2(1d)] 267 168
..- attr(*, "dimnames")=List of 1
.. ..$ Y: chr [1:2] "democrat" "republican"
$ tables :List of 16
..$ V1 : table [1:2, 1:2] 0.395 0.812 0.605 0.188
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ Y : chr [1:2] "democrat" "republican"
.. .. ..$ V1: chr [1:2] "n" "y"
..$ V2 : table [1:2, 1:2] 0.498 0.493 0.502 0.507
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ Y : chr [1:2] "democrat" "republican"
.. .. ..$ V2: chr [1:2] "n" "y"
..$ V3 : table [1:2, 1:2] 0.112 0.866 0.888 0.134
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ Y : chr [1:2] "democrat" "republican"
.. .. ..$ V3: chr [1:2] "n" "y"
..$ V4 : table [1:2, 1:2] 0.9459 0.0121 0.0541 0.9879
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ Y : chr [1:2] "democrat" "republican"
.. .. ..$ V4: chr [1:2] "n" "y"
..$ V5 : table [1:2, 1:2] 0.7843 0.0485 0.2157 0.9515
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ Y : chr [1:2] "democrat" "republican"
.. .. ..$ V5: chr [1:2] "n" "y"
..$ V6 : table [1:2, 1:2] 0.523 0.102 0.477 0.898
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ Y : chr [1:2] "democrat" "republican"
.. .. ..$ V6: chr [1:2] "n" "y"
..$ V7 : table [1:2, 1:2] 0.228 0.759 0.772 0.241
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ Y : chr [1:2] "democrat" "republican"
.. .. ..$ V7: chr [1:2] "n" "y"
..$ V8 : table [1:2, 1:2] 0.171 0.847 0.829 0.153
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ Y : chr [1:2] "democrat" "republican"
.. .. ..$ V8: chr [1:2] "n" "y"
..$ V9 : table [1:2, 1:2] 0.242 0.885 0.758 0.115
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ Y : chr [1:2] "democrat" "republican"
.. .. ..$ V9: chr [1:2] "n" "y"

..$ V10: table [1:2, 1:2] 0.529 0.442 0.471 0.558


.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ Y : chr [1:2] "democrat" "republican"
.. .. ..$ V10: chr [1:2] "n" "y"
..$ V11: table [1:2, 1:2] 0.494 0.868 0.506 0.132
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ Y : chr [1:2] "democrat" "republican"
.. .. ..$ V11: chr [1:2] "n" "y"
..$ V12: table [1:2, 1:2] 0.855 0.129 0.145 0.871
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ Y : chr [1:2] "democrat" "republican"
.. .. ..$ V12: chr [1:2] "n" "y"
..$ V13: table [1:2, 1:2] 0.71 0.139 0.29 0.861
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ Y : chr [1:2] "democrat" "republican"
.. .. ..$ V13: chr [1:2] "n" "y"
..$ V14: table [1:2, 1:2] 0.6498 0.0186 0.3502 0.9814
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ Y : chr [1:2] "democrat" "republican"
.. .. ..$ V14: chr [1:2] "n" "y"
..$ V15: table [1:2, 1:2] 0.3625 0.9103 0.6375 0.0897
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ Y : chr [1:2] "democrat" "republican"
.. .. ..$ V15: chr [1:2] "n" "y"
..$ V16: table [1:2, 1:2] 0.0649 0.3425 0.9351 0.6575
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ Y : chr [1:2] "democrat" "republican"
.. .. ..$ V16: chr [1:2] "n" "y"
$ levels : chr [1:2] "democrat" "republican"
$ call : language naiveBayes.default(x = X, y = Y, laplace = laplace)
- attr(*, "class")= chr "naiveBayes"
> str(model.nb$apriori)
'table' int [1:2(1d)] 267 168
- attr(*, "dimnames")=List of 1
..$ Y: chr [1:2] "democrat" "republican"
> model.nb$apriori
Y
democrat republican
267
168
> model.nb$apriori/dim(HouseVotes84)[1]
Y
democrat republican
0.6137931 0.3862069
> model.nb$apriori/dim(HouseVotes84)
Y
democrat republican
0.6137931 9.8823529
> str(model.nb$tables)
List of 16
$ V1 : table [1:2, 1:2] 0.395 0.812 0.605 0.188
..- attr(*, "dimnames")=List of 2
.. ..$ Y : chr [1:2] "democrat" "republican"
.. ..$ V1: chr [1:2] "n" "y"
$ V2 : table [1:2, 1:2] 0.498 0.493 0.502 0.507
..- attr(*, "dimnames")=List of 2
.. ..$ Y : chr [1:2] "democrat" "republican"
.. ..$ V2: chr [1:2] "n" "y"
$ V3 : table [1:2, 1:2] 0.112 0.866 0.888 0.134
..- attr(*, "dimnames")=List of 2
.. ..$ Y : chr [1:2] "democrat" "republican"

.. ..$ V3: chr [1:2] "n" "y"


$ V4 : table [1:2, 1:2] 0.9459 0.0121 0.0541 0.9879
..- attr(*, "dimnames")=List of 2
.. ..$ Y : chr [1:2] "democrat" "republican"
.. ..$ V4: chr [1:2] "n" "y"
$ V5 : table [1:2, 1:2] 0.7843 0.0485 0.2157 0.9515
..- attr(*, "dimnames")=List of 2
.. ..$ Y : chr [1:2] "democrat" "republican"
.. ..$ V5: chr [1:2] "n" "y"
$ V6 : table [1:2, 1:2] 0.523 0.102 0.477 0.898
..- attr(*, "dimnames")=List of 2
.. ..$ Y : chr [1:2] "democrat" "republican"
.. ..$ V6: chr [1:2] "n" "y"
$ V7 : table [1:2, 1:2] 0.228 0.759 0.772 0.241
..- attr(*, "dimnames")=List of 2
.. ..$ Y : chr [1:2] "democrat" "republican"
.. ..$ V7: chr [1:2] "n" "y"
$ V8 : table [1:2, 1:2] 0.171 0.847 0.829 0.153
..- attr(*, "dimnames")=List of 2
.. ..$ Y : chr [1:2] "democrat" "republican"
.. ..$ V8: chr [1:2] "n" "y"
$ V9 : table [1:2, 1:2] 0.242 0.885 0.758 0.115
..- attr(*, "dimnames")=List of 2
.. ..$ Y : chr [1:2] "democrat" "republican"
.. ..$ V9: chr [1:2] "n" "y"
$ V10: table [1:2, 1:2] 0.529 0.442 0.471 0.558
..- attr(*, "dimnames")=List of 2
.. ..$ Y : chr [1:2] "democrat" "republican"
.. ..$ V10: chr [1:2] "n" "y"
$ V11: table [1:2, 1:2] 0.494 0.868 0.506 0.132
..- attr(*, "dimnames")=List of 2
.. ..$ Y : chr [1:2] "democrat" "republican"
.. ..$ V11: chr [1:2] "n" "y"
$ V12: table [1:2, 1:2] 0.855 0.129 0.145 0.871
..- attr(*, "dimnames")=List of 2
.. ..$ Y : chr [1:2] "democrat" "republican"
.. ..$ V12: chr [1:2] "n" "y"
$ V13: table [1:2, 1:2] 0.71 0.139 0.29 0.861
..- attr(*, "dimnames")=List of 2
.. ..$ Y : chr [1:2] "democrat" "republican"
.. ..$ V13: chr [1:2] "n" "y"
$ V14: table [1:2, 1:2] 0.6498 0.0186 0.3502 0.9814
..- attr(*, "dimnames")=List of 2
.. ..$ Y : chr [1:2] "democrat" "republican"
.. ..$ V14: chr [1:2] "n" "y"
$ V15: table [1:2, 1:2] 0.3625 0.9103 0.6375 0.0897
..- attr(*, "dimnames")=List of 2
.. ..$ Y : chr [1:2] "democrat" "republican"
.. ..$ V15: chr [1:2] "n" "y"
$ V16: table [1:2, 1:2] 0.0649 0.3425 0.9351 0.6575
..- attr(*, "dimnames")=List of 2
.. ..$ Y : chr [1:2] "democrat" "republican"
.. ..$ V16: chr [1:2] "n" "y"
> model.nb$tables$V1
V1
Y
n
y
democrat 0.3953488 0.6046512
republican 0.8121212 0.1878788
> predict(model.nb, HouseVotes84[1:5,])
[1] republican republican republican democrat democrat

Levels: democrat republican


> predict(model.nb, HouseVotes84[1:5,], type="raw")
democrat republican
[1,] 1.029209e-07 0.999999897
[2,] 5.820415e-08 0.999999942
[3,] 5.684937e-03 0.994315063
[4,] 9.985798e-01 0.001420152
[5,] 9.666720e-01 0.033328022
> pred = predict(model.nb, HouseVotes84)
> table(pred, HouseVotes84$Class)
pred
democrat republican
democrat
238
13
republican
29
155
> pred.posteriori = predict(model.nb, HouseVotes84, type="raw")
> ?HMeasure
> temp = HMeasure(HouseVotes84$Class, pred.posteriori)
Class labels have been switched from (democrat,republican) to (0,1)
Warning message:
In HMeasure.single(y = true.class, s = s, classifier.name = name.now, :
ROC curve of democrat mostly lying under the diagonal. Switching scores.
> temp$metrics
H
Gini
AUC
AUCH
KS
MER
M
WL
democrat 0.7864808 0.9485019 0.9742509 0.9783307 0.8400214 0.08735632 0.075846
21
republican 0.7864808 0.9485019 0.9742509 0.9783307 0.8400214 0.08735632 0.075846
21
Spec.Sens95 Sens.Spec95
ER
Sens
Spec Precision
democrat
0.8876404 0.8333333 0.09655172 0.922619 0.8913858 0.8423913
republican 0.8876404 0.8333333 0.09655172 0.922619 0.8913858 0.8423913
Recall
TPR
FPR
F
Youden TP FP TN FN
democrat 0.922619 0.922619 0.1086142 0.8806818 0.8140048 155 29 238 13
republican 0.922619 0.922619 0.1086142 0.8806818 0.8140048 155 29 238 13
> plotROC(temp)
> #Smoothing di laplace
> model.nb.laplaceA = naiveBayes(Class~., data=HouseVotes84, laplace=1)
> model.nb.laplaceA$tables$V1
V1
Y
n
y
democrat 0.3961538 0.6038462
republican 0.8083832 0.1916168
> model.nb.laplaceB = naiveBayes(Class~., data=HouseVotes84, laplace=3)
> model.nb.laplaceB$tables$V1
V1
Y
n
y
democrat 0.3977273 0.6022727
republican 0.8011696 0.1988304
> predA = predict( model.nb.laplaceA, HouseVotes84)
> predB = predict( model.nb.laplaceB, HouseVotes84)
> table(predA, HouseVotes84$Class)
predA
democrat republican
democrat
238
13
republican
29
155
> table(predB, HouseVotes84$Class)
predB
democrat republican
democrat
237
12
republican
30
156

> predA.priori = predict( model.nb.laplaceA,


> predA.prior
Errore: oggetto "predA.prior" non trovato
> predA.priori
[1] 1.291869e-07 7.331147e-08 5.970803e-03
-01
[7] 1.911189e-04 9.703059e-06 1.040805e-07
-06
[13] 1.000000e+00 1.000000e+00 5.981942e-07
+00
[19] 8.691134e-08 1.000000e+00 1.000000e+00
+00
[25] 1.000000e+00 1.000000e+00 1.000000e+00
+00
[31] 1.478990e-08 1.000000e+00 1.000000e+00
-08
[37] 1.410271e-06 2.506897e-05 1.050742e-08
+00
[43] 1.000000e+00 1.000000e+00 1.000000e+00
+00
[49] 9.999996e-01 1.492517e-08 1.000000e+00
-08
[55] 9.999007e-01 6.795599e-08 4.782246e-07
-08
[61] 1.000000e+00 1.478990e-08 1.000000e+00
-06
[67] 6.990445e-06 1.050742e-08 1.000000e+00
-01
[73] 1.000000e+00 6.534220e-01 1.000000e+00
-02
[79] 8.323657e-01 9.781595e-08 9.999877e-01
-08
[85] 1.060352e-08 2.056491e-02 1.070222e-08
-07
[91] 1.000000e+00 1.000000e+00 1.000000e+00
-01
[97] 4.603093e-03 9.405711e-01 1.000000e+00
-01
[103] 9.996810e-01 5.819238e-01 9.970940e-01
-01
[109] 1.000000e+00 1.000000e+00 1.000000e+00
-08
[115] 1.000000e+00 1.000000e+00 1.000000e+00
-08
[121] 1.397248e-06 7.461984e-08 1.091217e-06
-08
[127] 1.492517e-08 1.000000e+00 1.000000e+00
-01
[133] 5.304939e-08 7.531440e-08 7.467061e-08
-01
[139] 9.999985e-01 1.000000e+00 9.074498e-01
+00
[145] 9.998972e-01 9.999994e-01 1.040805e-07
+00
[151] 4.754954e-07 6.188892e-04 1.000000e+00
-06
[157] 9.876829e-07 9.999982e-01 1.050742e-08
-04
[163] 2.315582e-01 1.080875e-06 1.938227e-03
-01

HouseVotes84, type='raw')[,1]

9.971207e-01 9.481675e-01 7.370954e


1.000000e+00 1.387811e-06 6.934635e
1.446007e-07 9.999941e-01 1.000000e
1.000000e+00 1.000000e+00 1.000000e
1.000000e+00 3.623896e-02 1.000000e
1.040805e-07 1.000000e+00 1.478990e
1.000000e+00 1.000000e+00 1.000000e
1.000000e+00 1.000000e+00 1.000000e
5.053184e-07 1.000000e+00 6.756817e
7.394355e-08 7.394355e-08 5.208380e
1.000000e+00 1.000000e+00 6.950552e
1.000000e+00 1.000000e+00 9.999964e
1.223718e-04 1.431249e-02 1.318599e
9.999991e-01 7.531440e-08 1.506409e
1.506409e-08 9.945538e-01 3.117393e
1.000000e+00 9.991463e-01 9.996246e
1.869787e-08 5.416758e-02 9.990673e
1.000000e+00 1.040805e-07 2.190739e
7.461984e-08 1.000000e+00 1.060352e
3.045150e-03 1.000000e+00 1.506409e
1.723508e-05 1.000000e+00 7.461984e
9.999987e-01 1.000000e+00 9.999845e
1.070222e-08 3.509311e-05 9.999999e
1.129993e-03 4.543156e-04 1.000000e
9.997447e-01 4.782246e-07 1.000000e
9.999967e-01 1.478990e-08 2.986329e
9.999983e-01 1.554330e-03 9.860339e
1.000000e+00 5.986630e-01 9.060793e

[169]
-04
[175]
+00
[181]
+00
[187]
-07
[193]
-03
[199]
+00
[205]
+00
[211]
-04
[217]
-01
[223]
+00
[229]
-08
[235]
-03
[241]
+00
[247]
-08
[253]
-05
[259]
+00
[265]
+00
[271]
-02
[277]
-01
[283]
-01
[289]
+00
[295]
-01
[301]
-08
[307]
-01
[313]
+00
[319]
-01
[325]
+00
[331]
-07
[337]
-01
[343]
-08

4.860908e-02 1.000000e+00 1.000000e+00 7.461984e-08 1.000000e+00 4.365024e


1.000000e+00 1.000000e+00 9.999912e-01 1.000000e+00 1.000000e+00 1.000000e
1.000000e+00 1.000000e+00 1.000000e+00 9.093589e-01 1.000000e+00 1.000000e
1.000000e+00 1.000000e+00 9.213903e-04 1.000000e+00 7.398198e-08 2.528706e
9.999995e-01 1.000000e+00 1.000000e+00 7.467061e-08 9.999995e-01 1.943344e
1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e
5.293047e-05 9.999992e-01 1.091217e-06 4.983161e-06 1.000000e+00 1.000000e
1.000000e+00 7.394355e-08 1.000000e+00 1.000000e+00 4.754954e-07 2.701313e
9.978839e-01 1.050742e-08 1.000000e+00 1.000000e+00 1.000000e+00 9.999264e
1.000000e+00 9.689088e-07 6.174577e-08 1.050325e-07 1.000000e+00 1.000000e
2.889597e-07 8.401098e-04 1.040805e-07 1.683910e-06 1.000000e+00 7.394355e
9.999952e-01 1.091217e-06 1.000000e+00 9.999722e-01 1.000000e+00 7.031823e
7.611551e-03 1.000000e+00 5.836802e-01 1.000000e+00 1.000000e+00 1.000000e
9.999971e-01 2.858535e-07 6.137931e-01 1.000000e+00 1.478990e-08 1.478990e
1.000000e+00 1.040805e-07 1.000000e+00 1.000000e+00 1.994856e-06 3.493923e
1.000000e+00 1.000000e+00 1.000000e+00 9.999999e-01 1.000000e+00 1.000000e
1.000000e+00 1.000000e+00 1.091217e-06 9.999988e-01 1.000000e+00 1.000000e
1.000000e+00 1.000000e+00 1.000000e+00 4.538226e-04 2.938911e-01 4.076247e
2.513192e-07 3.424538e-01 1.060101e-07 1.070222e-08 9.999998e-01 7.685413e
4.044185e-07 1.060101e-07 1.000000e+00 1.000000e+00 9.999853e-01 9.999999e
9.995272e-01 9.889196e-01 9.998459e-01 9.999985e-01 9.999993e-01 1.000000e
9.998899e-01 1.689041e-05 6.041011e-05 1.000000e+00 1.000000e+00 9.999987e
4.137048e-03 1.000000e+00 7.821395e-07 1.506409e-08 5.208380e-08 1.070222e
1.506409e-08 9.999998e-01 1.070222e-08 9.999993e-01 1.785486e-07 9.999997e
1.000000e+00 3.555279e-06 2.967230e-03 4.754018e-01 9.816042e-01 1.000000e
1.000000e+00 1.000000e+00 9.997713e-01 1.000000e+00 9.904560e-01 9.803085e
9.565264e-08 3.365123e-04 9.998775e-01 1.040805e-07 1.000000e+00 1.000000e
1.050742e-08 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 3.430932e
9.999985e-01 1.000000e+00 1.000000e+00 6.264994e-07 7.057328e-06 9.999987e
9.999456e-01 8.117767e-02 1.000000e+00 1.890248e-04 7.531440e-08 6.882083e

[349] 1.000000e+00 2.931564e-02 9.999998e-01 2.671827e-01 9.998791e-01 2.431564e


-04
[355] 1.000000e+00 9.999858e-01 1.506409e-08 1.506409e-08 9.999718e-01 1.315820e
-07
[361] 1.000000e+00 9.923818e-01 9.999997e-01 1.199746e-03 1.621096e-04 2.276858e
-03
[367] 1.000000e+00 9.999999e-01 1.000000e+00 3.898518e-06 9.999998e-01 1.000000e
+00
[373] 1.351664e-05 2.015980e-01 1.040805e-07 1.385894e-06 1.000000e+00 8.304392e
-05
[379] 1.060101e-07 1.060101e-07 1.000000e+00 9.999526e-01 1.141910e-04 9.999997e
-01
[385] 2.080808e-04 9.116228e-03 9.999523e-01 9.999759e-01 3.524888e-06 1.000000e
+00
[391] 1.533452e-01 1.000000e+00 4.328582e-06 9.811857e-01 8.691326e-01 9.999998e
-01
[397] 9.999954e-01 2.139179e-01 9.997807e-01 4.562277e-08 1.073977e-05 6.795599e
-08
[403] 9.760270e-01 3.088151e-08 4.754954e-07 1.060101e-07 9.468313e-01 9.742596e
-08
[409] 9.999982e-01 1.506409e-08 6.856126e-07 1.000000e+00 7.531440e-08 1.545591e
-01
[415] 1.000000e+00 9.999999e-01 1.781027e-05 1.000000e+00 1.000000e+00 1.000000e
+00
[421] 1.849641e-01 9.999972e-01 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e
+00
[427] 1.000000e+00 1.995846e-05 1.000000e+00 9.999997e-01 8.245870e-05 1.000000e
+00
[433] 6.792897e-07 5.122147e-06 2.615216e-08
> predB.priori = predict( model.nb.laplaceB, HouseVotes84, type='raw')[,1]
> prioriBoth = cbind(predA.priori, predB.priori)
> temp = HMeasure(HouseVotes84$Class, predBoth)
Class labels have been switched from (democrat,republican) to (0,1)
Errore in rownames(scores) <- NULL : oggetto "predBoth" non trovato
> temp = HMeasure(HouseVotes84$Class, prioriBoth)
Class labels have been switched from (democrat,republican) to (0,1)
Warning messages:
1: In HMeasure.single(y = true.class, s = s, classifier.name = name.now, :
ROC curve of predA.priori mostly lying under the diagonal. Switching scores.
2: In HMeasure.single(y = true.class, s = s, classifier.name = name.now, :
ROC curve of predB.priori mostly lying under the diagonal. Switching scores.
> temp$Measures
NULL
> temp$Measure
NULL
> temp$measures
NULL
> temp$metrics
H
Gini
AUC
AUCH
KS
MER
predA.priori 0.7847836 0.9474764 0.9737382 0.9777956 0.8406902 0.08965517
predB.priori 0.7841794 0.9465400 0.9732700 0.9777622 0.8406902 0.08965517
MWL Spec.Sens95 Sens.Spec95
ER
Sens
Spec
predA.priori 0.07552913 0.8838951 0.8273810 0.09655172 0.9226190 0.8913858
predB.priori 0.07552913 0.8801498 0.8214286 0.09655172 0.9285714 0.8876404
Precision
Recall
TPR
FPR
F
Youden TP FP
predA.priori 0.8423913 0.9226190 0.9226190 0.1086142 0.8806818 0.8140048 155 29
predB.priori 0.8387097 0.9285714 0.9285714 0.1123596 0.8813559 0.8162119 156 30
TN FN
predA.priori 238 13
predB.priori 237 12

> plotROC(temp, lwd=2)


Errore in plotROC(temp, lwd = 2) : unused argument (lwd = 2)
> plotROC(temp)
> #Esercizio
> nrow(HouseVotes84)
[1] 435
> ?sample
> set.seed(1234)
> randList = sample(nrow(HouseVotes84),round(nrow(HouseVotes84)*0.75), replace=T
)
> #Si fa il sampling col replacement in modo che ciascuna estrazione
> #sia probabilisticamente indipendente una con l'altra
> randList
[1] 50 271 266 272 375 279 5 102 290 224 302 238 123 402 128 365 125 117 8
2
[20] 102 138 132 70 18 96 353 229 398 362 20 199 116 133 221 79 331 88 11
3
[39] 432 352 241 282 136 271 144 219 295 211 107 333 33 135 313 220 67 220 21
5
[58] 327 76 370 377 19 138 6 104 308 135 222 23 246 53 389 7 341 40 22
6
[77] 168 31 140 291 403 206 63 237 86 391 170 136 70 390 73 392 59 58 4
6
[96] 223 131 12 135 323 16 246 122 89 59 142 68 57 190 17 311 44 414 5
3
[115] 96 398 412 122 54 347 324 399 433 410 212 124 110 219 217 139 419 276 5
6
[134] 185 398 204 396 261 275 379 219 428 142 210 156 273 323 247 427 251 191 10
0
[153] 36 370 103 430 262 435 164 242 187 251 189 98 37 278 188 32 350 142 33
0
[172] 255 309 186 150 331 185 244 51 132 209 151 262 34 416 10 367 276 135 32
4
[191] 278 432 56 385 353 358 364 319 428 279 288 230 139 335 229 319 134 176 8
9
[210] 429 247 122 81 330 247 406 278 305 209 370 184 14 113 146 59 218 349 14
7
[229] 222 216 347 247 47 352 247 93 327 134 213 431 185 107 95 300 427 208 33
7
[248] 250 421 347 232 260 115 122 29 245 115 2 257 227 368 13 261 117 53 4
4
[267] 326 7 22 326 156 331 164 348 12 221 358 237 117 150 161 187 400 342 32
1
[286] 123 199 126 303 358 285 180 415 106 265 330 302 51 277 135 154 427 235 19
4
[305] 413 197 83 432 239 335 398 297 178 178 64 86 84 178 152 364 87 375 17
3
[324] 67 148 160
> randList = sample(nrow(HouseVotes84),round(nrow(HouseVotes84)*0.75))
> randList
[1] 186 81 285 398 317 380 409 84 202 165 160 12 394 174 403 115 217 429 15
5
[20] 130 15 277 381 19 83 305 54 290 407 384 241 296 197 309 2 224 184 13
2
[39] 332 388 261 93 322 433 382 104 342 190 119 153 293 39 162 252 113 79
1
[58] 41 82 44 258 73 368 353 257 274 271 405 62 338 207 196 7 133 248 15
0
[77] 272 278 205 68 270 33 220 147 273 283 366 76 301 145 177 267 46 141 22
6

[96] 402 117 36 315 356 412 143 413 92 372 259 346 306 90 215 124 304
1
[115] 262 350 323 425 314 176 358 198 192 11 80 373 340 404 231 209 348
2
[134] 415 333 269 118 417 53 58 383 116 399 168 378 428 379 276 420 280
2
[153] 357 291 148 157 324 390 38 21 210 193 328 43 163 365 246 411 316
1
[172] 23 318 286 175 351 222 13 303 419 393 211 354 201 266 319 232 352
0
[191] 281 22 263 434 427 60 87 221 134 255 10 235 245 56 59 144 423
7
[210] 225 361 341 167 287 85 135 212 48 369 94 179 396 260 194 55 47
5
[229] 371 391 102 9 127 292 32 199 136 172 218 170 298 387 282 424 120
4
[248] 126 410 72 313 78 101 149 152 3 103 418 363 64 275 364 426 106
0
[267] 288 228 240 347 431 330 320 20 343 247 243 89 336 61 131 253 385
6
[286] 121 265 279 140 249 421 96 112 69 432 345 216 114 395 203 66 200
1
[305] 339 430 57 435 236 171 349 105 206 284 321 264 181 110 125 122 400
8
[324] 91 227 187
> sample(2,round(nrow(HouseVotes84)*0.75))
Errore in sample.int(x, size, replace, prob) :
cannot take a sample larger than the population when 'replace = FALSE'
> #Se replace == FALSE allora ciascuna estrazione del numero avviene
> #senza reimbussolamento
> train = HouseVotes84[randList,]
> dim(train)
[1] 326 17
> test = HouseVotes84[-randList,]
> model.nb.laplace = naiveBayes(Class~., data=train, laplace=1)
> model.nb = naiveBayes(Class~., data=train)
> predL = predict(model.nb.laplace, test)
> pred = predict(model.nb, test)
> table(predL, test$Class)

238 19
185 42
146 36
327 16
28 37
183 2
166 3
386 41
173 36
156 40
42 31
310 30

predL
democrat republican
democrat
65
2
republican
9
33
> table(pred, test$Class)
pred
democrat republican
democrat
65
2
republican
9
33
> predL.post = predict(model.nb, test, type='raw')[,2]
> predL.post = predict(model.nb.laplace, test, type='raw')[,2]
> pred.post = predict(model.nb, test, type='raw')[,2]
> predBoth = cbind(pred.post, predL.post)
> HMeasure(test$Class, predBoth)
Class labels have been switched from (democrat,republican) to (0,1)
$metrics
H
Gini
AUC
AUCH
KS
MER
M
WL
pred.post 0.7762976 0.9212355 0.9606178 0.9714286 0.8498069 0.08256881 0.065482
70
predL.post 0.7672269 0.9204633 0.9602317 0.9704633 0.8362934 0.08256881 0.071374

46
Spec.Sens95 Sens.Spec95
ER
Sens
Spec Precision
0.8783784 0.7714286 0.1009174 0.9428571 0.8783784 0.7857143
0.8648649 0.7714286 0.1009174 0.9428571 0.8783784 0.7857143
Recall
TPR
FPR
F
Youden TP FP TN FN
pred.post 0.9428571 0.9428571 0.1216216 0.8571429 0.8212355 33 9 65 2
predL.post 0.9428571 0.9428571 0.1216216 0.8571429 0.8212355 33 9 65 2
pred.post
predL.post

attr(,"data")
attr(,"data")[[1]]
attr(,"data")[[1]]$F0
[1] 0.00000000 0.02702703
1
[8] 0.12162162 0.13513514
4
[15] 0.25675676 0.28378378
5
[22] 0.36486486 0.37837838
5
[29] 0.45945946 0.47297297
4
[36] 0.55405405 0.56756757
4
[43] 0.64864865 0.66216216
2
[50] 0.72972973 0.74324324
1
[57] 0.82432432 0.83783784
8
[64] 0.89189189 0.89189189
5
[71] 0.94594595 0.94594595
6
[78] 0.95945946 0.95945946
9
[85] 0.98648649 0.98648649
9
[92] 1.00000000 1.00000000
0
[99] 1.00000000
attr(,"data")[[1]]$F1
[1] 0.00000000 0.00000000
0
[8] 0.00000000 0.00000000
0
[15] 0.00000000 0.00000000
0
[22] 0.00000000 0.00000000
0
[29] 0.00000000 0.00000000
0
[36] 0.00000000 0.00000000
0
[43] 0.00000000 0.00000000
3
[50] 0.02857143 0.02857143
3
[57] 0.02857143 0.02857143
9

0.04054054 0.05405405 0.06756757 0.09459459 0.1081081


0.16216216 0.20270270 0.21621622 0.22972973 0.2432432
0.29729730 0.31081081 0.32432432 0.33783784 0.3513513
0.39189189 0.40540541 0.41891892 0.43243243 0.4459459
0.48648649 0.50000000 0.51351351 0.52702703 0.5405405
0.58108108 0.59459459 0.60810811 0.62162162 0.6351351
0.67567568 0.67567568 0.68918919 0.70270270 0.7162162
0.75675676 0.77027027 0.78378378 0.79729730 0.8108108
0.85135135 0.86486486 0.87837838 0.87837838 0.8783783
0.89189189 0.90540541 0.91891892 0.93243243 0.9459459
0.94594595 0.95945946 0.95945946 0.95945946 0.9594594
0.95945946 0.97297297 0.97297297 0.97297297 0.9864864
0.98648649 0.98648649 0.98648649 0.98648649 0.9864864
1.00000000 1.00000000 1.00000000 1.00000000 1.0000000

0.00000000 0.00000000 0.00000000 0.00000000 0.0000000


0.00000000 0.00000000 0.00000000 0.00000000 0.0000000
0.00000000 0.00000000 0.00000000 0.00000000 0.0000000
0.00000000 0.00000000 0.00000000 0.00000000 0.0000000
0.00000000 0.00000000 0.00000000 0.00000000 0.0000000
0.00000000 0.00000000 0.00000000 0.00000000 0.0000000
0.00000000 0.02857143 0.02857143 0.02857143 0.0285714
0.02857143 0.02857143 0.02857143 0.02857143 0.0285714
0.02857143 0.02857143 0.02857143 0.05714286 0.0857142

[64]
4
[71]
1
[78]
6
[85]
4
[92]
0
[99]

0.08571429 0.11428571 0.14285714 0.14285714 0.14285714 0.14285714 0.1428571


0.17142857 0.20000000 0.22857143 0.22857143 0.25714286 0.28571429 0.3142857
0.34285714 0.37142857 0.40000000 0.40000000 0.42857143 0.45714286 0.4571428
0.48571429 0.51428571 0.54285714 0.57142857 0.60000000 0.71428571 0.7428571
0.74285714 0.77142857 0.82857143 0.85714286 0.88571429 0.94285714 1.0000000
1.00000000

attr(,"data")[[1]]$G0
[1] 0.00000000 0.00000000 0.01351351 0.04054054 0.05405405 0.12162162 0.32432432
[8] 1.00000000
attr(,"data")[[1]]$G1
[1] 0.0000000 0.2571429 0.5428571 0.7714286 0.8571429 0.9714286 1.0000000 1.0000
000
attr(,"data")[[1]]$cost
[1] 0.0000000 1.0000000 0.9090909 0.8000000 0.7500000 0.4444444 0.0625000 0.0000
000
[9] 1.0000000
attr(,"data")[[1]]$pi1
[1] 0.3211009
attr(,"data")[[1]]$pi0
[1] 0.6788991
attr(,"data")[[1]]$n0
[1] 74
attr(,"data")[[1]]$n1
[1] 35
attr(,"data")[[1]]$n
[1] 109
attr(,"data")[[1]]$hc
[1] 8
attr(,"data")[[1]]$s.class0
[1] 4.525125e-13 4.525125e-13
12
[7] 2.538304e-12 3.890551e-12
12
[13] 4.410442e-12 4.410442e-12
12
[19] 1.194853e-11 2.243127e-11
11
[25] 4.289404e-11 4.479269e-11
10
[31] 2.325143e-10 3.366018e-10
10
[37] 6.948256e-10 9.688177e-10
09
[43] 7.238779e-09 1.149933e-08
08

4.797781e-13 7.320922e-13 2.353211e-12 2.538304e3.981558e-12 3.998903e-12 4.106566e-12 4.106566e4.410442e-12 6.208715e-12 6.575199e-12 9.505316e2.243127e-11 3.482690e-11 4.050324e-11 4.089769e5.331871e-11 6.240569e-11 9.287757e-11 1.664516e5.209834e-10 5.321965e-10 5.433921e-10 5.973297e1.405080e-09 1.790492e-09 2.117662e-09 3.718155e1.315558e-08 2.526723e-08 3.708444e-08 4.065557e-

[49]
07
[55]
04
[61]
01
[67]
01
[73]

8.021760e-08 1.403882e-07 5.827140e-07 6.235422e-07 8.439995e-07 9.584138e1.689265e-06 1.857594e-06 2.285757e-06 1.084362e-05 7.030967e-05 2.889456e3.405700e-04 1.281181e-03 3.005914e-02 3.577871e-02 2.012428e-01 8.722791e9.666022e-01 9.780211e-01 9.829004e-01 9.942232e-01 9.993197e-01 9.999979e9.999992e-01 1.000000e+00

attr(,"data")[[1]]$s.class1
[1] 3.172690e-07 3.002413e-01
01
[7] 9.990692e-01 9.991937e-01
01
[13] 9.999948e-01 9.999955e-01
01
[19] 9.999996e-01 9.999998e-01
01
[25] 9.999999e-01 9.999999e-01
00
[31] 1.000000e+00 1.000000e+00

8.159505e-01 9.033260e-01 9.460419e-01 9.989276e9.999690e-01 9.999746e-01 9.999922e-01 9.999931e9.999989e-01 9.999991e-01 9.999994e-01 9.999996e9.999999e-01 9.999999e-01 9.999999e-01 9.999999e1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+
1.000000e+00 1.000000e+00 1.000000e+00

attr(,"data")[[1]]$severity.ratio
[1] 0.472973
attr(,"data")[[2]]
attr(,"data")[[2]]$F0
[1] 0.00000000 0.02702703
1
[8] 0.12162162 0.13513514
4
[15] 0.25675676 0.28378378
5
[22] 0.36486486 0.37837838
5
[29] 0.45945946 0.47297297
4
[36] 0.55405405 0.56756757
4
[43] 0.64864865 0.66216216
2
[50] 0.72972973 0.74324324
1
[57] 0.82432432 0.83783784
8
[64] 0.89189189 0.89189189
5
[71] 0.94594595 0.94594595
6
[78] 0.95945946 0.95945946
9
[85] 0.98648649 0.98648649
9
[92] 1.00000000 1.00000000
0
[99] 1.00000000
attr(,"data")[[2]]$F1

0.04054054 0.05405405 0.06756757 0.09459459 0.1081081


0.16216216 0.20270270 0.21621622 0.22972973 0.2432432
0.29729730 0.31081081 0.32432432 0.33783784 0.3513513
0.39189189 0.40540541 0.41891892 0.43243243 0.4459459
0.48648649 0.50000000 0.51351351 0.52702703 0.5405405
0.58108108 0.59459459 0.60810811 0.62162162 0.6351351
0.67567568 0.67567568 0.68918919 0.70270270 0.7162162
0.75675676 0.77027027 0.78378378 0.79729730 0.8108108
0.85135135 0.86486486 0.86486486 0.87837838 0.8783783
0.89189189 0.90540541 0.91891892 0.93243243 0.9459459
0.94594595 0.95945946 0.95945946 0.95945946 0.9594594
0.95945946 0.97297297 0.97297297 0.97297297 0.9864864
0.98648649 0.98648649 0.98648649 0.98648649 0.9864864
1.00000000 1.00000000 1.00000000 1.00000000 1.0000000

[1] 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.0000000


0
[8]
0
[15]
0
[22]
0
[29]
0
[36]
0
[43]
3
[50]
3
[57]
9
[64]
4
[71]
1
[78]
6
[85]
4
[92]
0
[99]

0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.0000000


0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.0000000
0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.0000000
0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.0000000
0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.0000000
0.00000000 0.00000000 0.00000000 0.02857143 0.02857143 0.02857143 0.0285714
0.02857143 0.02857143 0.02857143 0.02857143 0.02857143 0.02857143 0.0285714
0.02857143 0.02857143 0.02857143 0.02857143 0.05714286 0.05714286 0.0857142
0.08571429 0.11428571 0.14285714 0.14285714 0.14285714 0.14285714 0.1428571
0.17142857 0.20000000 0.22857143 0.22857143 0.25714286 0.28571429 0.3142857
0.34285714 0.37142857 0.40000000 0.40000000 0.42857143 0.45714286 0.4571428
0.48571429 0.51428571 0.54285714 0.57142857 0.60000000 0.71428571 0.7428571
0.74285714 0.77142857 0.82857143 0.85714286 0.88571429 0.94285714 1.0000000
1.00000000

attr(,"data")[[2]]$G0
[1] 0.00000000 0.00000000 0.01351351 0.04054054 0.05405405 0.13513514 0.32432432
[8] 1.00000000
attr(,"data")[[2]]$G1
[1] 0.0000000 0.2571429 0.5428571 0.7714286 0.8571429 0.9714286 1.0000000 1.0000
000
attr(,"data")[[2]]$cost
[1] 0.00000000 1.00000000 0.90909091 0.80000000 0.75000000 0.40000000 0.06666667
[8] 0.00000000 1.00000000
attr(,"data")[[2]]$pi1
[1] 0.3211009
attr(,"data")[[2]]$pi0
[1] 0.6788991
attr(,"data")[[2]]$n0
[1] 74
attr(,"data")[[2]]$n1
[1] 35
attr(,"data")[[2]]$n
[1] 109
attr(,"data")[[2]]$hc
[1] 8

attr(,"data")[[2]]$s.class0
[1] 2.338869e-12 2.338869e-12
11
[7] 1.255347e-11 1.915777e-11
11
[13] 2.163481e-11 2.163481e-11
11
[19] 5.799568e-11 1.065829e-10
10
[25] 1.966008e-10 2.060753e-10
10
[31] 1.062645e-09 1.496789e-09
09
[37] 2.556589e-09 3.098267e-09
08
[43] 3.056522e-08 3.991274e-08
07
[49] 3.123092e-07 3.968584e-07
06
[55] 4.585425e-06 6.496091e-06
04
[61] 8.899171e-04 3.838173e-03
01
[67] 9.667372e-01 9.876162e-01
01
[73] 9.999989e-01 9.999999e-01
attr(,"data")[[2]]$s.class1
[1] 8.935902e-07 3.167905e-01
01
[7] 9.989291e-01 9.991017e-01
01
[13] 9.999935e-01 9.999944e-01
01
[19] 9.999995e-01 9.999997e-01
01
[25] 9.999999e-01 9.999999e-01
00
[31] 1.000000e+00 1.000000e+00

2.477128e-12 3.764024e-12 1.165847e-11 1.255347e1.928659e-11 1.985773e-11 2.020274e-11 2.020274e2.163481e-11 3.064158e-11 3.164844e-11 4.560274e1.065829e-10 1.644633e-10 1.903462e-10 1.912963e2.447647e-10 2.906770e-10 3.176506e-10 7.595068e1.704934e-09 2.289815e-09 2.326324e-09 2.329976e5.994529e-09 7.519696e-09 8.922027e-09 1.564241e4.626319e-08 1.029253e-07 1.198746e-07 1.434890e1.628488e-06 1.710560e-06 2.293669e-06 2.571179e8.132989e-06 2.811818e-05 1.753507e-04 7.893108e5.997919e-02 7.178916e-02 3.368645e-01 8.746494e9.903742e-01 9.966558e-01 9.995828e-01 9.999973e-

8.125632e-01 9.046617e-01 9.453324e-01 9.987978e9.999700e-01 9.999730e-01 9.999902e-01 9.999915e9.999986e-01 9.999988e-01 9.999992e-01 9.999995e9.999999e-01 9.999999e-01 9.999999e-01 9.999999e9.999999e-01 1.000000e+00 1.000000e+00 1.000000e+
1.000000e+00 1.000000e+00 1.000000e+00

attr(,"data")[[2]]$severity.ratio
[1] 0.472973
attr(,"class")
[1] "hmeasure"
> measure = HMeasure(test$Class, predBoth)
Class labels have been switched from (democrat,republican) to (0,1)
> measure$metrics
H
Gini
AUC
AUCH
KS
MER
M
WL
pred.post 0.7762976 0.9212355 0.9606178 0.9714286 0.8498069 0.08256881 0.065482
70
predL.post 0.7672269 0.9204633 0.9602317 0.9704633 0.8362934 0.08256881 0.071374
46
Spec.Sens95 Sens.Spec95
ER
Sens
Spec Precision
pred.post
0.8783784 0.7714286 0.1009174 0.9428571 0.8783784 0.7857143
predL.post 0.8648649 0.7714286 0.1009174 0.9428571 0.8783784 0.7857143
Recall
TPR
FPR
F
Youden TP FP TN FN
pred.post 0.9428571 0.9428571 0.1216216 0.8571429 0.8212355 33 9 65 2

predL.post 0.9428571 0.9428571 0.1216216 0.8571429 0.8212355 33 9 65 2


> plotROC(measure)
> ?plotROC
> function accuracy(table) {
Errore: unexpected symbol in "function accuracy"
> accuracy function(table) {
Errore: unexpected 'function' in "accuracy function"
> accuracy = function(table) {
+ return(sum(diag(table))/sum(table)) }
> accuracy(table(predL, test$Class))
[1] 0.8990826
> accuracy(table(pred, test$Class))
[1] 0.8990826
> #Curve ROC quasi identiche e accuratezze uguali
>

Вам также может понравиться