# Thiết lập seed để đảm bảo tái lập kết quả
set.seed(123)
# 1. Sinh ngẫu nhiên 600 giá trị từ phân phối chuẩn N(40, 12^2)
data <- rnorm(600, mean = 40, sd = 12)
# Xem một vài giá trị đầu tiên để xác nhận
print(head(data))
## [1] 33.27429 37.23787 58.70450 40.84610 41.55145 60.58078
# 2. Kiểm tra tính chuẩn bằng kiểm định Shapiro-Wilk
shapiro_test <- shapiro.test(data)
# In kết quả kiểm định
print(shapiro_test)
##
## Shapiro-Wilk normality test
##
## data: data
## W = 0.99873, p-value = 0.9518
# 3. (Tuỳ chọn) Vẽ biểu đồ Histogram và Q-Q Plot để kiểm tra trực quan
par(mfrow = c(1, 2)) # Chia cửa sổ đồ thị thành 2 cột
# Histogram
hist(data, breaks = 30, main = "Histogram of Data", xlab = "Value", col = "lightblue", border = "black")
# Q-Q Plot
qqnorm(data, main = "Q-Q Plot")
qqline(data, col = "red")

# 1. Đọc tập dữ liệu Iris
data(iris) # Tập dữ liệu iris có sẵn trong R
head(iris) # Hiển thị vài dòng đầu để kiểm tra
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
# 2. Tính giá trị trung bình và phương sai của Sepal.Width theo từng loại hoa
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
# Tính trung bình và phương sai
summary_stats <- iris %>%
group_by(Species) %>%
summarise(
Mean_Sepal_Width = mean(Sepal.Width),
Variance_Sepal_Width = var(Sepal.Width)
)
# Hiển thị kết quả
print(summary_stats)
## # A tibble: 3 × 3
## Species Mean_Sepal_Width Variance_Sepal_Width
## <fct> <dbl> <dbl>
## 1 setosa 3.43 0.144
## 2 versicolor 2.77 0.0985
## 3 virginica 2.97 0.104
# 3. Vẽ biểu đồ Boxplot của Sepal.Width theo từng loại hoa
library(ggplot2)
ggplot(iris, aes(x = Species, y = Sepal.Width, fill = Species)) +
geom_boxplot() +
labs(title = "Boxplot of Sepal Width by Species",
x = "Species",
y = "Sepal Width") +
theme_minimal()
