-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassificationMethods.Rmd
More file actions
138 lines (104 loc) · 3.19 KB
/
Copy pathClassificationMethods.Rmd
File metadata and controls
138 lines (104 loc) · 3.19 KB
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
---
output: html_document
date: "2023-06-12"
---
```{r setup, include=FALSE}
library(caret)
options(repos = c(CRAN = "https://cloud.r-project.org"))
# Install the kernlab package
install.packages("kernlab")
library(e1071) # For Support Vector Machines (SVM)
```
## Load data and Remove rows with missing values
```{r}
load("C:/Users/USER/Downloads/breastcancer2.RData")
ls()
write.csv(dat, "test.csv")
breastcancer2 <- read.csv("test.csv")
breastcancer2 <- na.omit(breastcancer2)
head(breastcancer2, 5)
```
## Convert target variable to factor
```{r}
breastcancer2$y <- as.factor(breastcancer2$y)
```
## (a)
## Train a linear SVM
```{r}
linear_svm <- svm(y ~ ., data = breastcancer2, type = "C-classification", kernel = "linear", na.action = na.omit)
```
## Training accuracy
```{r}
train_acc <- sum(predict(linear_svm) == breastcancer2$y) / length(breastcancer2$y)
train_acc
```
## Cross-validated accuracy
```{r}
cv_acc <- train( y ~ ., data = breastcancer2, method = "svmLinear", trControl = trainControl(method = "cv", number = 10) )$results$Accuracy
cv_acc
```
## (b)
## Load the tree package
```{r}
library(tree)
```
## Construct the decision tree using tree
```{r}
classification_tree <- tree(y ~ ., data = breastcancer2, split = c("deviance","gini"), control = tree.control(nobs = nrow(breastcancer2), minsize = 5))
print(classification_tree)
plot(classification_tree)
text(classification_tree, pretty = 0)
```
## Obtain the training accuracy
```{r}
predicted <- predict(classification_tree, newdata = breastcancer2, type = "class")
training_accuracy <- mean(predicted == breastcancer2$y)
```
## Perform 10-fold cross-validation
```{r}
library(caret)
cv_results <- train(y ~ ., data = breastcancer2, method = "rpart", trControl = trainControl(method = "cv", number = 10))
```
## Result Accuracy for training tree and 10 fold CV
```{r}
print(training_accuracy)
print(cv_results$results$Accuracy)
```
## (c)
## Using package randomForestSRC instead of Ranger because of facing Installation problem with ranger.
```{r}
install.packages("randomForestSRC")
library(randomForestSRC)
```
## Convert variables to factors and dataframe and check missing values
```{r}
breastcancer2 <- lapply(breastcancer2, as.factor)
breastcancer_df <- as.data.frame(breastcancer2)
# Check for missing values
if (any(is.na(unlist(breastcancer_df)))) {
print("Dataset contains missing values. Handle missing values appropriately.")
# Handle missing values in the dataset
# ...
} else {
print("No missing values found.")
}
```
## Fit the classification model using randomForestSRC
```{r}
classification_model <- rfsrc(formula = y ~ ., data = breastcancer_df)
```
## Trainig accuracy
```{r}
predicted_labels <- predict(classification_model, data = breastcancer_df)$predicted
predicted_labels
train_accuracy <- mean(predicted_labels == breastcancer_df$y)
```
## 10 fold CV
```{r}
cv_model <- rfsrc(formula = y ~ ., data = breastcancer_df, xbest = TRUE, nfold = 10)
```
## CV accuracy
```{r}
cv_predicted_labels <- cv_model$cvms$predicted
cv_accuracy <- mean(cv_predicted_labels == breastcancer_df$y)
```