133598 Shirlene Ouma
This markdown document provides a step-by-step explanation of the R script for creating and executing a model for the flower recommendation project. The script covers the following milestones:
The exploratory data analysis phase involves examining the dataset to understand its characteristics and identify patterns. This includes:
Creating a frequency table to determine the number of observations for each flower type.
```r frequency_table <- table(recommendation$Flower) print(frequency_table)
Calculating the mean, median, and mode for each numerical variable to understand the central tendency of the data.
r mean_values <- colMeans(recommendation) print(mean_values) ## Measure Distribution summary(recommendation) ## Measure of Relationship correlation_matrix <- cor(recommendation[, 1:4]) print(correlation_matrix)
Performing an ANOVA (Analysis of Variance) test to determine if there is a significant difference in flower type across different levels of soil nutrients and moisture.
r aov_model <- aov(Flower ~ Nitrogen + Phosphorus + Potassium + Moisture, data = recommendation) summary(aov_model)
Creating multivariate plots, such as scatter plots or heatmaps, to visualize the relationships between variables.
r ggplot(recommendation, aes(x = Nitrogen, y = Phosphorus)) + geom_point() + geom_smooth()
Creating univariate plots, such as histograms or density plots, to visualize the distribution of individual variables.
r ggplot(recommendation, aes(x = Moisture)) + geom_histogram()
The preprocessing and data transformation phase involves preparing the data for model training. This includes:
Checking for missing values in the dataset and identifying how they will be handled.
r missing_values <- colSums(is.na(recommendation)) print(missing_values)
The model training phase involves developing a predictive model using the prepared data. This includes:
Dividing the data into training and testing sets to evaluate the model’s performance on unseen data.
r set.seed(123) train_index <- sample(1:nrow(recommendation), 0.75) train_data <- recommendation[train_index, ] test_data <- recommendation[-train_index, ]
Evaluating the model’s stability by performing bootstrapping, which involves resampling the data and retraining the model multiple times.
r boot <- boot(train_data, lm, R=100, i=train_index) print(boot)
Assessing the model’s generalizability using cross validation, which involves splitting the training data into multiple folds and training the model on different combinations of folds.
The hyper-parameter tuning phase involves optimizing the model’s hyperparameters to improve its performance. This includes:
Using a search method, such as random search or grid search, to explore different combinations of hyperparameters and select the ones that provide the best performance.
```r set.seed(123) rs <- randomSearch(lm, data = train_data, tuneLength = 20) print(rs)
The consolidation phase involves creating an API to deploy the model and making it accessible to users. This includes:
Creating a Plumber API to expose the trained model as an endpoint that users can interact with.
r install.packages(“plumber”) library(plumber)
#’ @title Predict flower type based on soil conditions #’ @param nitrogen Nitrogen level in soil #’ @param phosphorus Phosphorus level in soil #’ @param potassium Potassium level in soil #’ @param moisture Soil moisture level #’ @return Predicted flower type #’ @get /predict predict <- function(nitrogen, phosphorus, potassium, moisture) { # Load the trained model model <- loadModel(“model.rds”)
# Predict flower type prediction <- predict(model, newdata = data.frame(Nitrogen = nitrogen, Phosphorus = phosphorus, Potassium = potassium, Moisture = moisture))
# Return prediction return(prediction$Flower) }
#’ @title Flower Prediction API #’ @description This API predicts the most suitable flower type based on soil conditions. #’ @get / #’ @get /docs #’ @get /predict #’ @get /status #’ @post /predict ROBS <- API(predict, port = 8000) ROBS$run(background = TRUE)