Posts

Module 10

  The deliberate bug in the provided code is the use of && instead of & inside the for loop for determining outliers. The && operator evaluates only the first element of the logical vectors, which leads to incorrect outlier detection. Here's the corrected version of the function: r tukey_multiple <- function(x) {   outliers <- array(TRUE, dim = dim(x))   for (j in 1:ncol(x)) {     outliers[, j] <- outliers[, j] & tukey.outlier(x[, j])   }   outlier.vec <- vector(length = nrow(x))   for (i in 1:nrow(x)) {      outlier.vec[i] <- all(outliers[i, ])    }    return(outlier.vec)  } To test the corrected function, we can apply it to a sample dataset and observe if it returns the expected results without errors: r # Sample dataset set.seed(123) data <- matrix(rnorm(100), ncol = 5) # Applying the fixed function outliers <- tukey_multiple(data) print(outliers)

Module 9 Assignment

Title: Revealing Insights: Exploring Multi-Variable Visualization with the Iris Dataset

Assignment 9

 Step 1: Load the dataset R # Install and load necessary packages install.packages("ggplot2") library(ggplot2) # Load the mtcars dataset data(mtcars) Step 2: Basic Visualization For basic visualization without any package, you can use base R plotting functions. Let's create a scatter plot of miles per gallon (mpg) against horsepower (hp). R # Basic Scatter Plot plot(mtcars$hp, mtcars$mpg, main = "Scatter Plot of Horsepower vs. MPG",       xlab = "Horsepower", ylab = "Miles Per Gallon") Step 3: Lattice Visualization For lattice visualization, you can use the lattice package. Let's create a histogram of car weights (wt). R # Install and load the lattice package install.packages("lattice") library(lattice) # Lattice Histogram histogram(~wt, data = mtcars, main = "Histogram of Car Weights", xlab = "Weight") Step 4: ggplot2 Visualization Finally, for ggplot2 visualization, let's create a boxplot of miles per g...

Module 8 Assignment

Input/ Output, string manipulation, and plyr package  Analyzing Student Data in RStudio: A Step-by-Step Guide In this tutorial, we'll walk through the process of analyzing student data in RStudio. We'll cover importing the dataset, calculating the mean grade by sex, and accessing specific columns of the dataset. Let's dive in! Step 1: Import the Dataset First, we need to import the dataset into RStudio. We'll use the `read.table()` function to read the dataset from a text file. Replace `<Assignment.6.Dataset>.txt` with the correct file path and name for your dataset. ```R # Import the dataset Student <- read.table("<Assignment.6.Dataset>.txt", header = TRUE) ``` Step 2: Calculate Mean Grade by Sex Next, we'll calculate the mean grade for each category of sex using the `ddply()` function from the `plyr` package. ```R # Calculate mean grade by sex library(plyr) StudentAverage <- ddply(Student, "Sex", summarise, Grade_Average = mea...

Module 7

Exploring Object-Oriented Systems in R In this assignment, I will download a dataset and explore whether generic functions, S3, and S4 object-oriented systems can be assigned to it. #### Step 1: Downloading Dataset I will use the `iris` dataset available in R. ```R # Load the iris dataset data("iris") head(iris) ``` #### Step 2: Generic Functions Assignment To determine if generic functions can be assigned to the `iris` dataset, I will attempt to assign a generic function. ```R # Define a generic function my_summary <- function(data) {   summary(data) } # Attempt to assign the generic function to the iris dataset my_summary(iris) ``` Generic functions can be assigned to the `iris` dataset since it is a standard R data frame, and generic functions can operate on any R object. #### Step 3: S3 and S4 Assignment Next, I will explore if S3 and S4 object-oriented systems can be assigned to the `iris` dataset. ```R # Attempt to assign S3 class to the iris dataset class(iris) <...

Module # 6 Doing math in R part 2

  Doing Math in R: Part 2 In this blog post, we'll delve into some matrix operations and matrix generation in R. We'll use basic functions and operations to manipulate matrices and create new ones based on specific requirements. 1. Matrix Operations Consider two matrices: \( A = \begin{pmatrix} 2 & 0 \\ 1 & 3 \end{pmatrix} \) and \( B = \begin{pmatrix} 5 & 2 \\ 4 & -1 \end{pmatrix} \) (a) Addition: To find the sum of matrices \( A \) and \( B \), we'll use the `+` operator in R. ```R A <- matrix(c(2, 0, 1, 3), ncol = 2) B <- matrix(c(5, 2, 4, -1), ncol = 2) A + B ``` The result of \( A + B \) is: \( \begin{pmatrix} 2+5 & 0+2 \\ 1+4 & 3+(-1) \end{pmatrix} = \begin{pmatrix} 7 & 2 \\ 5 & 2 \end{pmatrix} \) (b) Subtraction: To find the difference between matrices \( A \) and \( B \), we'll use the `-` operator in R. ```R A - B ``` The result of \( A - B \) is: \( \begin{pmatrix} 2-5 & 0-2 \\ 1-4 & 3-(-1) \end{pmatrix} = \begin{p...

Module 4 Programming Structure in R

 Here's how you can create side-by-side boxplots and histograms for the given data: ```R # Create vectors for the data Frequency <- c(0.6, 0.3, 0.4, 0.4, 0.2, 0.6, 0.3, 0.4, 0.9, 0.2) BP <- c(103, 87, 32, 42, 59, 109, 78, 205, 135, 176) First <- c(1, 1, 1, 1, 0, 0, 0, 0, NA, 1)  # Assuming "bad" as 1 and "good" as 0 Second <- c(0, 0, 1, 1, 0, 0, 1, 1, 1, 1)  # Assuming "low" as 0 and "high" as 1 FinalDecision <- c(0, 1, 0, 1, 0, 1, 0, 1, 1, 1)  # Assuming "low" as 0 and "high" as 1 # Combine into a data frame hospital_data <- data.frame(Frequency, BP, First, Second, FinalDecision) # Plot side-by-side boxplots par(mfrow = c(1, 2))  # Set up the plotting area boxplot(BP ~ First, data = hospital_data, xlab = "First Assessment", ylab = "Blood Pressure", main = "Blood Pressure by First Assessment") boxplot(BP ~ Second, data = hospital_data, xlab = "Second Assessment", yl...