-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathR script.R
More file actions
68 lines (57 loc) · 2.22 KB
/
R script.R
File metadata and controls
68 lines (57 loc) · 2.22 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
library(MASS)
library(nnet)
library(ggplot2)
library(caret)
install.packages("ggpubr")
library(ggpubr)
df <- read.csv("heart.csv")
# Extracting Numerical Variables
df <- df[, c("Age", "RestingBP", "Cholesterol", "MaxHR", "HeartDisease")]
df$HeartDisease <- as.factor(df$HeartDisease) # Convert target to factor
# QQ Plots
qq_plots <- list()
for (var in colnames(df)[1:4]) {
p <- ggqqplot(df[[var]], ylab = var) + ggtitle(paste("QQ Plot for", var))
qq_plots[[var]] <- p
}
ggarrange(plotlist = qq_plots, ncol = 2, nrow = 2)
# Internal Validation
# Dr. Ali's Function
flda <- function(x, class) {
cat("Fisher Linear Discriminant:\n")
a <- lda(x, class)
d <- predict(a)
t <- table(class, d$class)
print(t)
er <- 100 * (sum(t) - sum(diag(t))) / nrow(x)
cat("Error Rate =", er, "%\n")
return(list(predictions = d, error_rate = er))
}
# Internal Validation for FLDA
result_flda_internal <- flda(df[, 1:4], df$HeartDisease)
flda_internal_error_rate <- result_flda_internal$error_rate
# Internal Validation for Multinomial
mn_model_internal <- multinom(HeartDisease ~ ., data = df, trace = FALSE)
pred_mn_internal <- predict(mn_model_internal, df)
t_mn_internal <- table(df$HeartDisease, pred_mn_internal)
print(t_mn_internal)
mn_internal_error_rate <- mean(pred_mn_internal != df$HeartDisease) * 100
internal_comparison <- data.frame(
Method = c("FLDA", "Multinomial"),
Internal_Error = c(flda_internal_error_rate, mn_internal_error_rate)
)
print(internal_comparison)
# Leave-One-Out Validation (External Validation)
# Suppress iteration output
ctrl <- trainControl(method = "LOOCV", verboseIter = FALSE)
# Leave-One-Out Validation for FLDA
flda_loocv <- train(HeartDisease ~ ., data = df, method = "lda", trControl = ctrl)
flda_loocv_error <- 100 * (1 - flda_loocv$results$Accuracy)
# Leave-One-Out Validation for Multinomial
mn_loocv <- train(HeartDisease ~ ., data = df, method = "multinom", trControl = ctrl, trace = FALSE)
mn_loocv_error <- 100 * (1 - mn_loocv$results$Accuracy)
loocv_comparison <- data.frame(
Method = c("FLDA", "Multinomial"),
LOOCV_Error = c(flda_loocv_error, mn_loocv_error)
)
print(loocv_comparison)