#Viec 1
ob <- read.csv("C:/Users/Acer/OneDrive/DAU/Tập huấn/2025 10-Nghiên cứu khoa học/Data ex/Obesity data.csv")
summary(ob$age)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 13.00 35.00 48.00 47.15 58.00 88.00
table(ob$gender)
##
## F M
## 862 355
summary(ob$age)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 13.00 35.00 48.00 47.15 58.00 88.00
table(ob$gender)
##
## F M
## 862 355
summary(ob$weight)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 34.00 49.00 54.00 55.14 61.00 95.00
summary(ob$height)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 136.0 151.0 155.0 156.7 162.0 185.0
summary(ob$pcfat)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 9.2 27.0 32.4 31.6 36.8 48.4
table(ob$hypertension)
##
## 0 1
## 600 617
table(ob$diabetes)
##
## 0 1
## 1082 135
library(ggplot2)
ggplot(ob, aes(x = factor(hypertension))) + geom_bar()
quantile(ob$age, probs = c(0.25, 0.5, 0.75))
## 25% 50% 75%
## 35 48 58
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
ob %>% group_by(gender) %>%
summarise(
age = median(age, na.rm = TRUE),
weight = median(weight, na.rm = TRUE),
height = median(height, na.rm = TRUE),
pcfat = median(pcfat, na.rm = TRUE),
hypertension = mean(hypertension),
.groups = 'drop'
)
## # A tibble: 2 × 6
## gender age weight height pcfat hypertension
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 F 49 51 153 34.7 0.501
## 2 M 44 62 165 24.6 0.521
t.test(age ~ gender, data = ob)
##
## Welch Two Sample t-test
##
## data: age by gender
## t = 4.2608, df = 587.49, p-value = 2.372e-05
## alternative hypothesis: true difference in means between group F and group M is not equal to 0
## 95 percent confidence interval:
## 2.626083 7.117272
## sample estimates:
## mean in group F mean in group M
## 48.57309 43.70141
t.test(weight ~ gender, data = ob)
##
## Welch Two Sample t-test
##
## data: weight by gender
## t = -16.952, df = 551.85, p-value < 2.2e-16
## alternative hypothesis: true difference in means between group F and group M is not equal to 0
## 95 percent confidence interval:
## -10.836942 -8.586319
## sample estimates:
## mean in group F mean in group M
## 52.31090 62.02254
#Viec 2: so sanh 2 nhom
A <- c(14, 4, 10, 6, 3, 11, 12)
B <- c(16, 17, 13, 12, 7, 16, 11, 8, 7)
shapiro.test(A)
##
## Shapiro-Wilk normality test
##
## data: A
## W = 0.92541, p-value = 0.5126
shapiro.test(B)
##
## Shapiro-Wilk normality test
##
## data: B
## W = 0.89641, p-value = 0.2319
summary(A)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 3.000 5.000 10.000 8.571 11.500 14.000
summary(B)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 7.00 8.00 12.00 11.89 16.00 17.00
boxplot(A, B, names = c("A", "B"))
t.test(A, B)
##
## Welch Two Sample t-test
##
## data: A and B
## t = -1.6, df = 12.554, p-value = 0.1345
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -7.813114 1.178194
## sample estimates:
## mean of x mean of y
## 8.571429 11.888889
library(boot)
data <- data.frame(group = rep(c("A", "B"), c(length(A), length(B))),
value = c(A, B))
boot_diff <- function(data, indices) {
d <- data[indices, ]
mean(d$value[d$group == "A"]) - mean(d$value[d$group == "B"])
}
results <- boot(data, statistic = boot_diff, R = 1000)
boot.ci(results, type = "perc")
## BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
## Based on 1000 bootstrap replicates
##
## CALL :
## boot.ci(boot.out = results, type = "perc")
##
## Intervals :
## Level Percentile
## 95% (-7.416, 0.500 )
## Calculations and Intervals on Original Scale
boot_median_diff <- function(data, indices) {
d <- data[indices, ]
median(d$value[d$group == "A"]) - median(d$value[d$group == "B"])
}
results_median <- boot(data, statistic = boot_median_diff, R = 1000)
boot.ci(results_median, type = "perc")
## BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
## Based on 1000 bootstrap replicates
##
## CALL :
## boot.ci(boot.out = results_median, type = "perc")
##
## Intervals :
## Level Percentile
## 95% (-10, 3 )
## Calculations and Intervals on Original Scale