Académique Documents
Professionnel Documents
Culture Documents
packages("ggpubr")
###################################################################################
#################################
# One sample T-test is used to compare the mean of one sample to a known standard
(or theoretical/hypothetical) mean.
## one-sample t-test can be used only, when the data are normally distributed.
## whether the mean m of the sample is equal to the theoretical mean mu?
## whether the mean m of the sample is less than the theoretical mean mu?
## whether the mean m of the sample is greater than the theoretical mean mu?
set.seed(1234)
my_data <- data.frame(
name = paste0(rep("M_", 10), 1:10),
weight = round(rnorm(10, 20, 2), 1)
)
head(my_data, 10)
summary(my_data$weight)
library(ggpubr)
# We want to know, if the average weight of the mice differs from 25g (two-tailed
test)?
res
# if you want to test whether the mean weight of mice is less than 25g (one-tailed
test), type this
res1 <- t.test(my_data$weight, mu = 25, alternative = "less")
res1
# if you want to test whether the mean weight of mice is greater than 25g (one-
tailed test), type this
res2
###################################################################################
#####################################
# For example, suppose that we have measured the weight of 100 individuals: 50
women (group A) and 50 men (group B). We want to know if the mean weight of women
m_A is significantly different from that of men m_B.
### when the two groups of samples (A and B), being compared, are normally
distributed.
### and when the variances of the two groups are equal. This can be checked using
F-test.
## whether the mean of group A m_A is equal to the mean of group B m_B?
## whether the mean of group A m_A is less than the mean of group B m_B?
## whether the mean of group A m_A is greather than the mean of group B m_B?
# Classical t-test:
# S^2 = {sigma{(x-m_A)^2}+sigma{(x-m_B)^2}}/(n_A+n_B-2)
print(my_data)
library(dplyr)
group_by(my_data, group) %>%
summarise(
count = n(),
mean = mean(weight, na.rm = TRUE),
sd = sd(weight, na.rm = TRUE)
)
# Compute t-test
res <- t.test(women_weight, men_weight, var.equal = TRUE, alternative = "greater")
res
# Compute t-test
res <- t.test(weight ~ group, data = my_data, var.equal = TRUE)
res
set.seed(1234)
my_data <- data.frame(
name = paste0(rep("M_", 30), 1:30),
weight = round(rnorm(30, 20, 2), 1),
treatment = paste0(rep("tr_",30),1:3)
)
my_data
summary(res.aov)