#Question 1:

# Create a data frame with the noise observations for each circuit design
noise_data <- data.frame(
  Design = rep(c("Design 1", "Design 2", "Design 3", "Design 4"), each = 5),
  Noise_Observed = c(21, 20, 29, 30, 24, 30, 31, 33, 26, 30, 27, 26, 25, 35, 39, 35, 41, 33, 28, 38)
)


# a) Dependent variable: Noise Observed
# Represents the data being measured.

# b) Factor: Circuit Design
# Categorizes the designs being compared.

# c) Factor levels: Design 1, Design 2, Design 3, and Design 4
# Specific categories within the factor "Circuit Design."

# d) If conducting multiple t-tests instead of ANOVA
# If opting for t-tests, we'd compare each pair of designs individually.
getwd()
## [1] "C:/Users/cerpa/OneDrive/Documents/CAP3330_Fall2023"
#Identifying working drive
#Question 2:

# Load the CSV file into a data frame named "Pigs_Weights"
Pigs_Weights <- read.csv("Pigs_Weights.csv")

# a) Dependent variable: Pig's Weight
# The data being analyzed for differences in pig weights.

# b) Factor: Diet
# The factor categorizing the diets.

# c) Factor levels: Four different diets
# Specific categories within the factor "Diet."

# Compute the average weight for each diet
diet_avg_weight <- aggregate(Weight ~ Diet, data = Pigs_Weights, FUN = mean)
diet_avg_weight
#Question 2 Continued: 
# d) Run ANOVA and make the appropriate conclusion
# ANOVA assesses if there are significant differences in pig weights among diets.

anova_result <- aov(Weight ~ Diet, data = Pigs_Weights)
summary(anova_result)
##             Df Sum Sq Mean Sq F value Pr(>F)  
## Diet         3  130.8   43.60   3.134 0.0547 .
## Residuals   16  222.5   13.91                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# The p-value is approximately 0.0547, which is slightly above the common significance level of 0.05, therefore I would fail to reject the null hypothesis. There is not enough evidence to conclude that there are significant differences in pig weights among the diets. 
#Question 3
# Data Entry 
design <- rep(c("1", "2", "3", "4"), each = 5)
noise <- c(21, 20, 29, 30, 24, 30, 31, 33, 26, 30, 27, 26, 25, 35, 39, 35, 41, 33, 28, 38)
data_df <- data.frame(Design = design, Noise_Observed = noise)

# a) State the hypotheses (Ho and Ha)
# - Ho (Null Hypothesis): The mean noise levels are the same for all designs.
# - Ha (Alternative Hypothesis): At least two designs have different mean noise levels.

# b) Run ANOVA and make the appropriate conclusion
anova_noise <- aov(Noise_Observed ~ Design, data = data_df)
summary(anova_noise)
##             Df Sum Sq Mean Sq F value Pr(>F)  
## Design       3    261   86.98   3.845 0.0301 *
## Residuals   16    362   22.63                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Question 3 continued:


# Post-hoc Test (Tukey's HSD)
posthoc <- TukeyHSD(anova_noise)
posthoc
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = Noise_Observed ~ Design, data = data_df)
## 
## $Design
##     diff       lwr       upr     p adj
## 2-1  5.2 -3.406868 13.806868 0.3419780
## 3-1  5.6 -3.006868 14.206868 0.2826091
## 4-1 10.2  1.593132 18.806868 0.0176270
## 3-2  0.4 -8.206868  9.006868 0.9991237
## 4-2  5.0 -3.606868 13.606868 0.3744075
## 4-3  4.6 -4.006868 13.206868 0.4442230
# - The post-hoc test results reveal which designs have significantly different average noise levels

#The analysis shows that there is a significant difference in noise levels among the four circuit designs (p = 0.0301). Design 4 has significantly higher noise than design 1 (p = 0.0176), but there are no significant differences between the other pairs of designs (p > 0.05).