زئوتکنیک

زئوتکنیک

مطالب علمی و آموزشی درباره دامپروری و علوم جانوری
زئوتکنیک

زئوتکنیک

مطالب علمی و آموزشی درباره دامپروری و علوم جانوری

Diabetes Prediction in R - Random Forest Scripts پیش بینی احتمال ابتلا به دیابت با هوش مصنوعی - نرم افزار آر

# Prediction of Diabetes status for new person/ new indivudual / new case/new patient with language of R

library(readr)  # for data manipulation
library(dplyr)  # for data manipulation
library(ggplot2)  # for visualization
library(randomForest)  # for using Random Forest Classifier algorithm
library(caret)  # for accuracy

df <- read_csv('diabetes2.csv')  # Read a CSV file by given path

cat('Total number of records: ', nrow(df), '\n')  # For checking number of records

library(ggplot2)
library(reshape2)
library(ggcorrplot)

# Assuming df is already defined as a data frame
correlation_matrix <- cor(df)
ggcorrplot(correlation_matrix, lab = TRUE, colors = c("blue", "white", "red"), title = "Correlation Heatmap")

library(dplyr)

any(is.na(df))

library(caret)
X <- df[, c('Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI', 'DiabetesPedigreeFunction', 'Age')]
Y <- df$Outcome
train_test_split <- createDataPartition(Y, p = 0.8, list = FALSE)
X_train <- X[train_test_split, ]
X_test <- X[-train_test_split, ]
Y_train <- Y[train_test_split]
Y_test <- Y[-train_test_split]

library(caret)
set.seed(0)
split <- createDataPartition(Y, p = 0.3, list = FALSE)
X_test <- X[split, ]
X_train <- X[-split, ]
Y_test <- Y[split]
Y_train <- Y[-split]

n_estimators <- 10
base_models <- list()

for (i in 1:n_estimators) {
  rf <- randomForest(X_train, Y_train, ntree = 500)
  base_models <- c(base_models, rf)
}

predictions <- lapply(base_models, function(model) predict(model, newdata = X_test))

library(tidyverse)

# Get input from the user

user_input <- list(
    Glucose = as.numeric(readline("Enter Glucose level: ")),
    BloodPressure = as.numeric(readline("Enter Blood Pressure: ")),
    SkinThickness = as.numeric(readline("Enter Skin Thickness: ")),
    Insulin = as.numeric(readline("Enter Insulin level: ")),
    BMI = as.numeric(readline("Enter BMI: ")),
    DiabetesPedigreeFunction = as.numeric(readline("Enter Diabetes Pedigree Function: ")),
    Age = as.numeric(readline("Enter Age: "))
)

# Convert the user input into a DataFrame
user_df <- data.frame(user_input)

# Make a prediction using the trained model
predict(rf, user_df)


# Interpret the prediction
if (predict(rf, user_df)<0.5) {
  result <- "No diabetes"
} else {
  result <- "Diabetes"
}


print(paste("The model predicts:", result))

if (user_prediction[1] == 0) {
  result <- "No diabetes"
} else {
  result <- "Diabetes"
}

cat("The model predicts:", result, "\n")
 
ادامه مطلب ...