msleep <- read.csv("C:/Users/ABHIRAM/Downloads/msleep.csv")

library(magrittr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
## 
## Attaching package: 'ggplot2'
## The following object is masked _by_ '.GlobalEnv':
## 
##     msleep

Null Hypothesis 1:

Aspect of Data: Comparing the sleep duration (sleep_total) of domesticated and non-domesticated animals. Alpha Level: 0.05 (standard significance level). Power Level: 0.80 (standard power level). Minimum Effect Size: You can determine this based on the practical significance you expect, but let’s assume a minimum effect size of 1 hour.

Null Hypothesis 2:

Aspect of Data: Comparing the brain weight (brainwt) between carnivorous and herbivorous animals. Alpha Level: 0.05 (standard significance level). Power Level: 0.80 (standard power level). Minimum Effect Size: You can determine this based on the practical significance you expect, but let’s assume a minimum effect size of 0.1 kg.

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ lubridate 1.9.2     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.0
## ✔ readr     2.1.4     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ tidyr::extract()   masks magrittr::extract()
## ✖ dplyr::filter()    masks stats::filter()
## ✖ dplyr::lag()       masks stats::lag()
## ✖ purrr::set_names() masks magrittr::set_names()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(pwr)

# Hypothesis 1: Is there a significant difference in sleep duration between domesticated and non-domesticated animals?
# Null Hypothesis: There is no significant difference in sleep duration between domesticated and non-domesticated animals.
# Alternative Hypothesis: There is a significant difference in sleep duration between domesticated and non-domesticated animals.

# Defining alpha level, power level, and minimum effect size
alpha <- 0.05
power <- 0.8
effect_size <- 0.5

# Checking if we have enough data to perform the test
count_domesticated <- sum(!is.na(msleep$sleep_total) & msleep$domesticated == "domesticated")
count_non_domesticated <- sum(!is.na(msleep$sleep_total) & msleep$domesticated != "domesticated")

if (count_domesticated >= 2 && count_non_domesticated >= 2) {
  # Performing a t-test
  t_test_result <- t.test(msleep$sleep_total ~ msleep$domesticated, na.action = na.omit)
  
  # Performing a power analysis
  power_result <- pwr.t.test(n = max(count_domesticated, count_non_domesticated), 
                             d = effect_size, sig.level = alpha, 
                             type = "two.sample", alternative = "two.sided")
  
  # Printing t-test result
  print(t_test_result)
  
  # Printing power analysis result
  print(power_result)
  
  # Visualizing the data
  ggplot(msleep, aes(x = domesticated, y = sleep_total)) +
    geom_boxplot() +
    labs(title = "Sleep Duration by Domestication Status",
         x = "Domesticated",
         y = "Sleep Duration") +
    theme_minimal()
} else {
  cat("Not enough data to perform the test.")
}
## Not enough data to perform the test.
# Hypothesis 2: Is there a significant difference in brain weight between animals with and without conservation status?
# Null Hypothesis: There is no significant difference in brain weight between animals with and without conservation status.
# Alternative Hypothesis: There is a significant difference in brain weight between animals with and without conservation status.

# Defining alpha level, power level, and minimum effect size
alpha <- 0.05
power <- 0.8
effect_size <- 0.5

# Checking if we have enough data to perform the test
count_with_conservation <- sum(!is.na(msleep$brainwt) & !is.na(msleep$conservation))
count_without_conservation <- sum(!is.na(msleep$brainwt) & is.na(msleep$conservation))

if (count_with_conservation >= 2 && count_without_conservation >= 2) {
  # Performing a t-test
  t_test_result <- t.test(msleep$brainwt ~ !is.na(msleep$conservation), na.action = na.omit)
  
  # Performing a power analysis
  power_result <- pwr.t.test(n = max(count_with_conservation, count_without_conservation), 
                             d = effect_size, sig.level = alpha, 
                             type = "two.sample", alternative = "two.sided")
  
  # Printing t-test result
  print(t_test_result)
  
  # Printing power analysis result
  print(power_result)
  
  # Visualizing the data
  ggplot(msleep, aes(x = !is.na(conservation), y = brainwt)) +
    geom_boxplot() +
    labs(title = "Brain Weight by Conservation Status",
         x = "Conservation Status",
         y = "Brain Weight") +
    theme_minimal()
} else {
  cat("Not enough data to perform the test.")
}
## 
##  Welch Two Sample t-test
## 
## data:  msleep$brainwt by !is.na(msleep$conservation)
## t = -1.2197, df = 42.496, p-value = 0.2293
## alternative hypothesis: true difference in means between group FALSE and group TRUE is not equal to 0
## 95 percent confidence interval:
##  -0.6800358  0.1675785
## sample estimates:
## mean in group FALSE  mean in group TRUE 
##           0.1168630           0.3730917 
## 
## 
##      Two-sample t test power calculation 
## 
##               n = 36
##               d = 0.5
##       sig.level = 0.05
##           power = 0.5526121
##     alternative = two.sided
## 
## NOTE: n is number in *each* group
## Warning: Removed 27 rows containing non-finite values (`stat_boxplot()`).