# --------------------------------------------------
# Name: Iftekhar Rashid 
# Student ID: 2215602
# Module: ACCA7022 - Advanced Statistical Programming
# Assignment: Coursework 1 - Data Manipulation
# Date: February 2026
# --------------------------------------------------
# Task 1A - Generate Production Data

# Set seed for reproducibility
set.seed(123)

# Create empty vectors to store data
Sol <- c()
Station <- c()
Team <- c()
Output <- c()
# Counter for indexing
i <- 1
for (sol in 1:30) {
  
  for (station in 1:6) {
    
    for (team in 1:4) {
      
      RandomNoise <- sample(-5:5, 1)
      out <- 50 + (station * team) + RandomNoise
      
      Sol[i] <- sol
      Station[i] <- station
      Team[i] <- team
      Output[i] <- out
      i <- i + 1
      
    }  # end team loop
  }  # end station loop
}  # end sol loop   ← CLOSE ALL LOOPS FIRST


production_data <- data.frame(
  Sol = Sol,
  Station = Station,
  Team = Team,
  Output = Output
)

head(production_data, 10)
# End of Task 1A
# -----------------------------------------
# Task 1B – Total Production per Station
# -----------------------------------------
station_total <- numeric(6)
for (s in 1:6) {
  
  total <- 0
  
  for (i in 1:nrow(production_data)) {
    
    if (production_data$Station[i] == s) {
      total <- total + production_data$Output[i]
    }
    
  }
  
  station_total[s] <- total
}
station_totals <- data.frame(
  Station = 1:6,
  TotalOutput = station_total
)

station_totals
# -----------------------------------------
# Task 1C – Detect Production Anomalies
# Loops only (no apply/aggregate/rowSums/functions)
# -----------------------------------------

# Storage vectors for anomalies dataframe (same length as production_data)
aSol <- numeric(nrow(production_data))
aStation <- numeric(nrow(production_data))
aTeam <- numeric(nrow(production_data))
aOutput <- numeric(nrow(production_data))
aAnomaly <- logical(nrow(production_data))

k <- 1  # row index for anomalies dataframe

for (sol in 1:30) {
  for (station in 1:6) {
    
    # 1) Daily station total across 4 teams for this Sol + Station
    daily_total <- 0
    for (i in 1:nrow(production_data)) {
      if (production_data$Sol[i] == sol && production_data$Station[i] == station) {
        daily_total <- daily_total + production_data$Output[i]
      }
    }
    
    # 2) Daily station average (across teams)
    daily_avg <- daily_total / 4
    
    # 3) Check each team output against daily station average (>10% deviation)
    for (i in 1:nrow(production_data)) {
      if (production_data$Sol[i] == sol && production_data$Station[i] == station) {
        
        deviation <- abs(production_data$Output[i] - daily_avg)
        
        is_anomaly <- FALSE
        if (deviation > (0.10 * daily_avg)) {
          is_anomaly <- TRUE
        }
        
        aSol[k] <- production_data$Sol[i]
        aStation[k] <- production_data$Station[i]
        aTeam[k] <- production_data$Team[i]
        aOutput[k] <- production_data$Output[i]
        aAnomaly[k] <- is_anomaly
        
        k <- k + 1
      }
    }
  }
}

# 4) Build anomalies dataframe (as required)
anomalies <- data.frame(
  Sol = aSol,
  Station = aStation,
  Team = aTeam,
  Output = aOutput,
  Anomaly = aAnomaly
)

# 5) Display first 10 anomalies detected (TRUE rows only)
anomalies_detected <- anomalies[anomalies$Anomaly == TRUE, ]
head(anomalies_detected, 10)
nrow(anomalies)
sum(anomalies$Anomaly)
# --------------------------------------------------------
# Task 1D – Identify Peak Productivity Days
# Loops only (no apply/aggregate/rowSums/functions)
# --------------------------------------------------------

peak_station <- numeric(6)
peak_sol <- numeric(6)
peak_total <- numeric(6)

for (station in 1:6) {
  
  max_total <- -Inf
  best_sol <- 0
  
  for (sol in 1:30) {
    
    # Calculate total output for this Station on this Sol (sum across 4 teams)
    daily_total <- 0
    
    for (i in 1:nrow(production_data)) {
      if (production_data$Station[i] == station &&
          production_data$Sol[i] == sol) {
        daily_total <- daily_total + production_data$Output[i]
      }
    }
    
    # Check if this is the maximum so far
    if (daily_total > max_total) {
      max_total <- daily_total
      best_sol <- sol
    }
  }
  
  peak_station[station] <- station
  peak_sol[station] <- best_sol
  peak_total[station] <- max_total
}

# Create required dataframe
peak_days <- data.frame(
  Station = peak_station,
  Sol = peak_sol,
  TotalOutput = peak_total
)

# Display peak_days
peak_days
# --------------------------------------------------------
# Task 1E – Calculate Efficiency Score
# --------------------------------------------------------

EfficiencyScore <- numeric(nrow(production_data))

for (i in 1:nrow(production_data)) {
  EfficiencyScore[i] <- production_data$Output[i] /
    (production_data$Station[i] + production_data$Team[i])
}

# Add new column to dataframe
production_data$EfficiencyScore <- EfficiencyScore

# Display first 10 rows
head(production_data, 10)