#——–Ngày 2: Hiển thị dữ liệu #Việc 1. Đọc dữ liệu “CHNS data full.csv” vào R và gọi dữ liệu là “df”
file.choose()
## [1] "D:\\HOC TAP\\TAP HUAN UNG DUNG AI TRONG PT DU LIEU SU DUNG R\\THUC HANH TAI LOP\\hocR1_5\\CNHS.html"
df=read.csv("D:\\HOC TAP\\TAP HUAN UNG DUNG AI TRONG PT DU LIEU SU DUNG R\\thuc hanh\\CHNS data full.csv")
head(df)
## id whours wgroup dead fu.time gender age edu marital residence income occu
## 1 1 35 1 0 4 2 52 3 3 1 116000 1
## 2 2 48 3 0 4 1 36 3 2 1 25200 1
## 3 3 40 1 0 4 2 31 3 2 1 27000 1
## 4 4 48 3 0 4 2 51 2 2 1 27600 2
## 5 5 32 2 0 4 1 58 2 2 1 34800 2
## 6 6 40 1 0 4 1 42 3 2 1 77000 1
## smoking drinking height weight bmi sys1 sys2 sys3 dias1 dias2 dias3 tsf1
## 1 0 0 168 83.5 29.58 120 126 120 80 82 76 28
## 2 1 0 173 85.0 28.40 120 120 120 90 80 80 25
## 3 0 1 167 50.0 17.93 110 108 110 70 70 70 18
## 4 0 0 164 80.0 29.74 120 110 120 80 82 80 27
## 5 0 0 175 65.0 21.22 120 120 120 80 82 80 23
## 6 0 1 179 75.0 23.41 110 112 110 72 76 70 24
## tsf2 tsf3 uac hc wc
## 1 27 28 36 111 103
## 2 44 25 35 102 95
## 3 17 18 25 96 72
## 4 26 27 32 104 97
## 5 22 22 35 102 90
## 6 23 24 28 96 90
dim(df)
## [1] 9317 29
#Việc 2: Soạn biểu đồ phân bố histogram #2.1 Thể hiện phân bố thu nhập (income) bằng biểu đồ histogram. Nhận xét phân bố này
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
ggplot(df, aes(x = income)) +
geom_histogram(fill = "blue", color = "white", bins = 30) +
labs(
x = "Thu nhập",
y = "Số người",
title = "Phân bố thu nhập"
) +
theme_minimal()
## Warning: Removed 224 rows containing non-finite outside the scale range
## (`stat_bin()`).
#2.2 Phân bố thu nhập rõ ràng hơn- Sử dụng giá trị logarithm
# Bước 2: Loại bỏ các giá trị thiếu hoặc income <= 0
df_clean <- subset(df, income > 0 & !is.na(income))
# Bước 3: Tính log(income)
df_clean$log_income <- log(df_clean$income)
# Bước 4: Vẽ biểu đồ histogram
hist(df_clean$log_income,
breaks = 30,
main = "Biểu đồ histogram phân bố log(income)",
xlab = "log(income)",
col = "lightblue",
border = "black")
#vẽ đường mật độ
hist(df_clean$log_income,
breaks = 30,
freq = FALSE, # Để biểu diễn xác suất, không phải tần số
main = "Histogram với mật độ log(income)",
xlab = "log(income)",
col = "lightgreen",
border = "gray")
lines(density(df_clean$log_income), col = "red", lwd = 2)
#2.3 Phân bố thu nhập theo giới tính
# Bước 4: Chuyển biến gender thành factor với nhãn rõ ràng
df_clean$gender <- factor(df_clean$gender, levels = c(1, 2), labels = c("Nam", "Nữ"))
# Bước 5: Vẽ biểu đồ histogram phân theo giới tính
# Dùng ggplot2 để dễ phân biệt màu
library(ggplot2)
ggplot(df_clean, aes(x = log_income, fill = gender)) +
geom_histogram(position = "identity", alpha = 0.5, bins = 30, color = "black") +
scale_fill_manual(values = c("red", "blue")) +
labs(title = "Phân bố log(income) theo giới tính",
x = "log(income)",
y = "Tần suất",
fill = "Giới tính") +
theme_minimal()
#Việc 3. Biểu đồ thanh 3.1 Thu nhập theo trình độ học vấn
# Bước 2: Lọc dữ liệu hợp lệ: income không thiếu và > 0, edu không thiếu
df_clean <- subset(df, !is.na(income) & income > 0 & !is.na(edu))
# Bước 3: Chuyển biến edu thành factor với nhãn rõ ràng
# (Giả sử edu được mã hóa thành 1 = Primary, 2 = Secondary, 3 = Tertiary)
df_clean$edu <- factor(df_clean$edu,
levels = c(1, 2, 3),
labels = c("Primary", "Secondary", "Tertiary"))
# Bước 4: Tính trung vị thu nhập theo trình độ học vấn
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.4.3
##
## 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
library(ggplot2)
median_income <- df_clean %>%
group_by(edu) %>%
summarise(median_income = median(income, na.rm = TRUE))
# Bước 5: Vẽ biểu đồ thanh
ggplot(median_income, aes(x = edu, y = median_income, fill = edu)) +
geom_col(width = 0.6, color = "black") +
scale_fill_manual(values = c("Primary" = "green", # Xanh
"Secondary" = "red", # Đỏ
"Tertiary" = "orange")) + # Cam
labs(title = "Trung vị thu nhập theo trình độ học vấn",
x = "Trình độ học vấn",
y = "Trung vị thu nhập",
fill = "Trình độ") +
theme_minimal()
#3.2 Thêm giá trị thu nhập #PROMPT: Thêm giá trị thu nhập vào biểu
đồ
# Bước 3: Vẽ biểu đồ với nhãn giá trị
ggplot(median_income, aes(x = edu, y = median_income, fill = edu)) +
geom_col(width = 0.6, color = "black") +
geom_text(aes(label = round(median_income, 1)), # Thêm giá trị nhãn làm tròn 1 chữ số
vjust = -0.5, # Đặt vị trí phía trên cột
size = 4, fontface = "bold") + # Kích thước & kiểu chữ
scale_fill_manual(values = c("Primary" = "green",
"Secondary" = "red",
"Tertiary" = "orange")) +
labs(title = "Trung vị thu nhập theo trình độ học vấn",
x = "Trình độ học vấn",
y = "Trung vị thu nhập",
fill = "Trình độ") +
theme_minimal()
#Việc 4. Soạn biểu đồ hộp so sánh phân bố của thu nhập theo giới tính
#PROMPT: Soạn biểu đồ hộp để so sánh phân bố của thu nhập (log(income))
theo giới tính (gender: 1 = Male, 2 = Female) và trình bày dữ liệu mỗi
cá nhân. Hiển thị cả giá trị outliers
# Kiểm tra income có tồn tại và lọc dữ liệu hợp lệ
df_clean <- subset(df, !is.na(income) & income > 0 & !is.na(gender))
# Tạo biến log(income)
df_clean$log_income <- log(df_clean$income)
# Chuyển giới tính thành factor
df_clean$gender <- factor(df_clean$gender,
levels = c(1, 2),
labels = c("Male", "Female"))
# Cài ggplot2 nếu chưa có
# install.packages("ggplot2")
library(ggplot2)
# Vẽ boxplot + điểm từng cá nhân
ggplot(df_clean, aes(x = gender, y = log_income, fill = gender)) +
geom_boxplot(outlier.colour = "black", outlier.shape = 16, outlier.size = 2) +
geom_jitter(width = 0.2, alpha = 0.3, color = "gray40") +
scale_fill_manual(values = c("Male" = "lightblue", "Female" = "lightpink")) +
labs(title = "Phân bố log(income) theo giới tính",
x = "Giới tính",
y = "log(income)",
fill = "Giới tính") +
theme_minimal()
#Việc 5. Soạn biểu đồ tương quan #5.1 Mối liên quan giữa thu nhập và
tuổi
# Bước 2: Lọc dữ liệu hợp lệ
df_clean <- subset(df, !is.na(age) & !is.na(income) & income > 0)
# Bước 3: Tính log(income)
df_clean$log_income <- log(df_clean$income)
# Bước 4: Vẽ biểu đồ tương quan
library(ggplot2)
ggplot(df_clean, aes(x = age, y = log_income)) +
geom_point(alpha = 0.5, color = "blue") + # Các điểm dữ liệu
geom_smooth(method = "lm", se = TRUE, color = "red") + # Đường hồi quy tuyến tính
labs(title = "Mối liên hệ giữa tuổi và log(income)",
x = "Tuổi",
y = "log(Thu nhập)") +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
#5.2 Mối liên quan giữa thu nhập và tuổi theo giới tính #PROMPT: Hãy thể
hiện mối liên quan giữa thu nhập (log(income)) và tuổi (age) theo giới
tính (gender: 1 = Male, 2 = Female)
# Bước 2: Làm sạch dữ liệu
df_clean <- subset(df, !is.na(age) & !is.na(income) & income > 0 & !is.na(gender))
# Bước 3: Tạo biến log(income)
df_clean$log_income <- log(df_clean$income)
# Bước 4: Chuyển gender thành factor có nhãn rõ ràng
df_clean$gender <- factor(df_clean$gender,
levels = c(1, 2),
labels = c("Male", "Female"))
# Bước 5: Vẽ biểu đồ tương quan theo giới tính
library(ggplot2)
ggplot(df_clean, aes(x = age, y = log_income, color = gender)) +
geom_point(alpha = 0.4) + # Điểm dữ liệu
geom_smooth(method = "lm", se = TRUE) + # Đường hồi quy riêng cho từng giới
labs(title = "Mối liên hệ giữa tuổi và log(thu nhập) theo giới tính",
x = "Tuổi",
y = "log(Thu nhập)",
color = "Giới tính") +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
#5.3 Thêm đường mô tả mối liên quan #PROMPT 1 : Thêm đường mô tả mối
liên quan thật sự của dữ liệu
# Chuẩn bị dữ liệu
df_plot <- df %>%
filter(!is.na(age), !is.na(income), income > 0, !is.na(gender)) %>%
mutate(
log_income = log(income),
gender = factor(gender, levels = c(1, 2), labels = c("Male", "Female"))
)
# Vẽ scatter plot + đường mô tả thực tế bằng loess
ggplot(df_plot, aes(x = age, y = log_income, color = gender)) +
geom_point(alpha = 0.4) +
geom_smooth(method = "loess", se = TRUE, span = 0.8) +
labs(
x = "Tuổi",
y = "Log(Thu nhập)",
color = "Giới tính",
title = "Mối liên hệ giữa tuổi và log(thu nhập) theo giới tính (LOESS)"
) +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
# Làm sạch dữ liệu
df_clean <- subset(df, !is.na(age) & !is.na(income) & income > 0 & !is.na(gender))
df_clean$log_income <- log(df_clean$income)
# Gán nhãn giới tính
df_clean$gender <- factor(df_clean$gender,
levels = c(1, 2),
labels = c("Male", "Female"))
# Tải thư viện ggplot2
library(ggplot2)
# Vẽ biểu đồ: chỉ đường loess mô tả thực tế
ggplot(df_clean, aes(x = age, y = log_income, color = gender)) +
geom_point(alpha = 0.4) + # Dữ liệu cá nhân
geom_smooth(method = "loess", se = FALSE, size = 1.2) + # Đường loess mô tả thực tế
labs(title = "Mối liên hệ thực tế giữa tuổi và log(thu nhập) theo giới tính",
x = "Tuổi",
y = "log(Thu nhập)",
color = "Giới tính") +
theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `geom_smooth()` using formula = 'y ~ x'
#mối liên quan bậc 3 có vẻ phù hợp nhất
# Làm sạch dữ liệu
df_clean <- subset(df, !is.na(age) & !is.na(income) & income > 0 & !is.na(gender))
df_clean$log_income <- log(df_clean$income)
# Gán nhãn giới tính
df_clean$gender <- factor(df_clean$gender,
levels = c(1, 2),
labels = c("Male", "Female"))
# Tải thư viện
library(ggplot2)
# Vẽ biểu đồ với đường hồi quy bậc 3 (đa thức cấp 3)
ggplot(df_clean, aes(x = age, y = log_income, color = gender)) +
geom_point(alpha = 0.4) + # Điểm dữ liệu cá nhân
geom_smooth(method = "lm",
formula = y ~ poly(x, 3),
se = TRUE,
size = 1.2) + # Đường hồi quy bậc 3
labs(title = "Mối liên hệ bậc 3 giữa tuổi và log(thu nhập) theo giới tính",
x = "Tuổi",
y = "log(Thu nhập)",
color = "Giới tính") +
theme_minimal()