Vous êtes sur la page 1sur 3

# install.

packages("ggpubr")

###################################################################################
#################################

# One sample T-test is used to compare the mean of one sample to a known standard
(or theoretical/hypothetical) mean.

# t.test(x, mu = 0, alternative = "two.sided")

## x: a numeric vector containing your data values


## mu: the theoretical mean. Default is 0 but you can change it.
## alternative: the alternative hypothesis. Allowed value is one of "two.sided"
(default), "greater" or "less".

## one-sample t-test can be used only, when the data are normally distributed.

# Here we will use an example dataset

# Typical questions answered by the One sample T-test are:

## 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)

ggboxplot(my_data$weight, ylab = "Weight (g)", xlab = FALSE, ggtheme =


theme_minimal())

# We want to know, if the average weight of the mice differs from 25g (two-tailed
test)?

res <- t.test(my_data$weight, mu = 25)

res

# printing the p-value


res$p.value

# printing the mean


res$estimate

# printing the confidence interval


res$conf.int

# 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 <- t.test(my_data$weight, mu = 25, alternative = "greater")

res2

###################################################################################
#####################################

# unpaired two-samples t-test ( independent t-test ) is used to compare the mean of


two independent groups.

# 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.

# unpaired two-samples t-test can be used only under certain conditions:

### 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.

# Typical questions answered by the independent t-test are:

## 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?

# In statistics, we can define the corresponding null hypothesis H_0 as follow:

## H_0: m_A = m_B


## H_0: m_A <= m_B
## H_0: m_A >= m_B

# The corresponding alternative hypotheses H_a are as follow:

## H_a: m_A != m_B (different)


## H_a: m_A > m_B (greater)
## H_a: m_A < m_B (less)

# Classical t-test:

## t = (m_A - m_B)/sqrt{(S^2)/(n_A) + (S^2)/(n_B)}

# S^2 = {sigma{(x-m_A)^2}+sigma{(x-m_B)^2}}/(n_A+n_B-2)

# t.test(x, y, alternative = "two.sided", var.equal = FALSE)

# Data in two numeric vectors


women_weight <- c(38.9, 61.2, 73.3, 21.8, 63.4, 64.6, 48.4, 48.8, 48.5)
men_weight <- c(67.8, 60, 63.4, 76, 89.4, 73.3, 67.3, 61.3, 62.4)
# Create a data frame
my_data <- data.frame(
group = rep(c("Woman", "Man"), each = 9),
weight = c(women_weight, men_weight)
)

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)
)

# Plot weight by group and color by group


library("ggpubr")
ggboxplot(my_data, x = "group", y = "weight",
color = "group", palette = c("#00AFBB", "#E7B800"),
ylab = "Weight", xlab = "Groups")

# 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

res.aov <- aov(weight ~ treatment, data = my_data)

summary(res.aov)

Vous aimerez peut-être aussi