lister <- read.delim('https://github.com/IowaBiostat/data-sets/raw/main/lister/lister.txt')
table(lister) # make table of the data

lister$Group <- factor(lister$Group, levels = c("Sterile", "Control")) # specify order you want in the levels
lister$Outcome <- factor(lister$Outcome, levels = c("Survived", "Died"))
(lister_tab <- table(lister)) # we will use this table in later analysis

lister_manual <- rbind(c(34, 6),
                       c(19, 16))
# Add row and column names
rownames(lister_manual) <- c("Sterile", "Control")
colnames(lister_manual) <- c("Survived", "Died")
lister_manual

# Alternative way to do this:
lister_manual2 <- data.frame("Group" = c(rep("Control", 35),
                                        rep("Sterile", 40)),
                            "Outcome" = c(rep("Survived", 19),
                                          rep("Died", 16),
                                          rep("Survived", 34),
                                          rep("Died", 6)))

# specify order of rows and columns in the levels:
lister_manual2$Group <- factor(lister_manual2$Group, levels = c("Sterile", "Control"))
lister_manual2$Outcome <- factor(lister_manual2$Outcome, levels = c("Survived", "Died"))

table(lister_manual2)

fisher.test(lister_tab)

Table<-matrix(data=c("a","b","c","d"),nrow=2,byrow=TRUE)
rownames(Table)<-c("Option 1","Option 2")
colnames(Table)<-c("Success","Failure")
print(Table, quote = FALSE)

print(lister_tab)
table2<-lister_tab
table2[1:2,1:2]<-c("a","c","b","d")
print(table2)

OR <- (34*16) / (19*6)

OR

logOR <- log(OR) # the log function in R is the natural log

logOR

SElogOR <- sqrt((1 / 19) + (1 / 6) + (1 / 16) + (1 / 34))

SElogOR

logCI <- logOR + qnorm(c(0.025, 0.975)) * SElogOR

logCI

CI <- exp(logCI)
CI

mu <- 77
xbar <- 80
s <- 11
n <- 37

# Use a t-test for the sample

t <- (xbar-mu) / (s / sqrt(n))

2*pt(abs(t), n-1, lower.tail=FALSE)

mu <- 90
sigma <- 8

(p <- pnorm(95, mu, sigma, lower.tail=FALSE))

dbinom(3, 10, p)

z <- (95-90)/(sigma/sqrt(10))

(p <- pnorm(z, lower.tail=FALSE))

1-dbinom(0, 5, p)

# Writing down our known values
n <- 235
p <- 155 / n
p_0 <- 0.5
alpha <- 0.05

# Finding our z-statistic
z <- (p - p_0) / (sqrt(p_0*(1 - p_0) / n))

# Using our z-statistic to get our p-value
pvalue <- 2*(1 - pnorm(z, mean = 0, sd = 1))
pvalue

SE <- sqrt(p*(1-p) / n)
z_alpha_2 <- qnorm(0.025)
p + c(1, -1)*z_alpha_2*SE

n <- 9
s <- 3.4
SE <- 3.4 / sqrt(n)
(t <- (-4.2 - 0) / SE)
(p_value <- 2*pt(t, 8))

t_0.025 <- qt(0.975, 8)
-4.2 + c(-1,1)*t_0.025*SE
