Bhawna G. Panwar

2 minute read

The HouseVotes84 dataset was used for applying the naive Bayes algorithm. library (e1071)

Naive Bayes Classifier for Discrete Predictors: we use again the Congressional Voting Records of 1984

Note refusals to vote have been treated as missing values!

data (HouseVotes84, package="mlbench") model <- naiveBayes(Class ~ ., data = HouseVotes84) head(HouseVotes84)

Class V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15

1 republican n y n y y y n n n y <NA> y y y n

2 republican n y n y y y n n n n n y y y n

3 democrat <NA> y y <NA> y y n n n n y n y y n

4 democrat n y y n <NA> y n n n n y n y n n

5 democrat y y y n y y n n n n y <NA> y y y

6 democrat n y y n y y n n n n n n y y y

V16

1 y

2 <NA>

3 n

4 y

5 y

6 y

predict the outcome of the first 20 records

predict(model, HouseVotes84[1:20,-1])

[1] republican republican republican democrat democrat democrat ## [7] republican republican republican democrat republican republican

[13] democrat democrat republican republican democrat democrat ## [19] republican democrat ## Levels: democrat republican

same but displaying posteriors

predict(model, HouseVotes84[1:20,-1], type = "raw")

democrat republican

[1,] 1.

Bhawna G. Panwar

2 minute read

Principle Component Analysis was performed using the Iris dataset.

Load data

data(iris) head(iris, 3)

Sepal.Length Sepal.Width Petal.Length Petal.Width Species

1 5.1 3.5 1.4 0.2 setosa

2 4.9 3.0 1.4 0.2 setosa

3 4.7 3.2 1.3 0.2 setosa

log transform log.ir <- log(iris[, 1:4])

ir.species <- iris[, 5]

apply PCA - scale. = TRUE is highly # advisable, but default is FALSE. ir.pca <- prcomp(log.

Bhawna G. Panwar

3 minute read

The Random Forest analysis techniques was used to improve prediction on the German Credit Dataset. The dataset is found: url="http://freakonometrics.free.fr/german_credit.csv" credit=read.csv(url, header = TRUE, sep = ",") str(credit)

'data.frame': 1000 obs. of 21 variables:

$ Creditability : int 1 1 1 1 1 1 1 1 1 1 …

$ Account.Balance : int 1 1 2 1 1 1 1 1 4 2 …

$ Duration.of.Credit..month. : int 18 9 12 12 12 10 8 6 18 24 .