
#Part 1
lipids <- read.delim('https://raw.githubusercontent.com/IowaBiostat/data-sets/main/lipids/lipids.txt')

#Part 2
anorexia_alltreat <- read.delim("https://raw.githubusercontent.com/IowaBiostat/data-sets/main/anorexia/anorexia.txt")
# We need to index to just get the weights for those who received family treatment.
anorexia <- anorexia_alltreat[anorexia_alltreat$Treat == "FT",]

#Part 3
corn <- read.delim('https://raw.githubusercontent.com/IowaBiostat/data-sets/main/corn/corn.txt')

#Part 4
lister <- read.delim('https://github.com/IowaBiostat/data-sets/raw/main/lister/lister.txt')

t.test(lipids$TRG, mu = 120)

# create a new column
anorexia$diff <- anorexia$Postwt-anorexia$Prewt

SE <- sd(anorexia$diff)/sqrt(17)

t <- (mean(anorexia$diff) - 0) / SE

2*(1-pt(t, df=16))

## Test for Post - Prior
t.test(anorexia$Postwt, anorexia$Prewt, paired = TRUE)

# Using R function
t.test(corn$cross, corn$self, paired = TRUE)

# By hand
corn$corndiff <- corn$cross - corn$self
SE <- sd(corn$corndiff)/sqrt(15)
t <- (mean(corn$corndiff) - 0) / SE

2*(1-pt(t, df = 14))

# n is not specified 
power.t.test(delta = 6,
             power = .99, 
             sd = 4,
             type = "paired")

power.t.test(delta = 6,
             n = 20, 
             sd = 4, 
             type = "paired")

power.t.test(delta = 3,
             n = 15,
             sd = 5, 
             type = "paired")

tab <- table(lister)
tab

chisq.test(tab, correct = FALSE)

fisher.test(tab)

chidata <- data.frame("Method" = c(rep("Laptop", 67), rep("Hand", 76)), 
                      "Result" = c(rep("Passed", 44), rep("Failed", 23), rep("Passed", 56), rep("Failed", 20)))

tab <- table(chidata)

chisq.test(tab, correct = FALSE)

fisher.test(tab)
