#Việc 1. Đọc dữ liệu "CHNS data full.csv" vào R và gọi dữ liệu là “df”
library(readr)
# Đọc file CSV
df <- read.csv("E:\\HOPT\\PTDLR\\DATA\\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
#xem all data dạng table
View(df)
#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.
# Tải thư viện cần thiết
library(ggplot2)
library(gridExtra)
# Đọc dữ liệu
df <- read.csv("E:\\HOPT\\PTDLR\\DATA\\CHNS data full.csv", header = TRUE)
# Biểu đồ 1: Histogram thu nhập không giới hạn
p1 <- ggplot(df, aes(x = income)) +
geom_histogram() +
labs(title = "",
x = "Income",
y = "Count") +
theme_minimal()
# Biểu đồ 2: Histogram thu nhập có giới hạn trục X
p2 <- ggplot(df, aes(x = income)) +
geom_histogram(fill = "steelblue", color = "white") +
labs(title = "Phân bố thu nhập",
x = "Thu nhập",
y = "Số người") +
theme_minimal()
# Ghép 2 biểu đồ nằm cạnh nhau
grid.arrange(p1, p2, ncol = 2)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 224 rows containing non-finite outside the scale range
## (`stat_bin()`).
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 224 rows containing non-finite outside the scale range
## (`stat_bin()`).
#Nhận xét về phân bố thu nhập (income)
#Biểu đồ bên trái (không giới hạn trục X):
#Phân bố thu nhập bị lệch nặng sang bên trái.
#Do có nhiều giá trị thu nhập rất lớn (outliers), phần lớn dữ liệu bị “nén lại” ở gần 0 → khó quan sát được phần phổ biến nhất.
#Đây là biểu hiện của phân bố lệch phải (right-skewed distribution).
#Biểu đồ bên phải (có màu và giới hạn trục):
#Hiển thị phân bố thu nhập rõ ràng hơn.
#Phần lớn cá nhân có thu nhập thấp, nằm dưới khoảng 50.000.
#Giúp dễ dàng nhận biết cụm giá trị phổ biến và mật độ phân bố.
#Tuy không có xlim() trong code, hệ trục tự điều chỉnh nhờ ggplot2 và độ rộng cột.
#Tổng kết:
# Phân bố thu nhập trong dữ liệu không đồng đều, tập trung chủ yếu ở mức thấp và có ít người có thu nhập cao bất thường.
#Biểu đồ giới hạn trục hoặc dùng log(income) là cần thiết để phân tích trực quan chính xác hơn.
#Dữ liệu có thể cần xử lý ngoại lệ (outliers) khi phân tích hồi quy hoặc thống kê sâu.
#2.2 Thể hiện phân bố thu nhập (income) rõ ràng hơn bằng biểu đồ histogram:
#Đọc dữ liệu
df <- read.csv("E:\\HOPT\\PTDLR\\DATA\\CHNS data full.csv", header = TRUE)
ggplot(df, aes(x = log(income)))+ geom_histogram(fill="blue",col="white")+
labs(title = "Phân bố thu nhập",
x = "Thu nhập (logarithm scale)",
y = "Số người")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 241 rows containing non-finite outside the scale range
## (`stat_bin()`).
theme_minimal()
## List of 136
## $ line :List of 6
## ..$ colour : chr "black"
## ..$ linewidth : num 0.5
## ..$ linetype : num 1
## ..$ lineend : chr "butt"
## ..$ arrow : logi FALSE
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_line" "element"
## $ rect :List of 5
## ..$ fill : chr "white"
## ..$ colour : chr "black"
## ..$ linewidth : num 0.5
## ..$ linetype : num 1
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_rect" "element"
## $ text :List of 11
## ..$ family : chr ""
## ..$ face : chr "plain"
## ..$ colour : chr "black"
## ..$ size : num 11
## ..$ hjust : num 0.5
## ..$ vjust : num 0.5
## ..$ angle : num 0
## ..$ lineheight : num 0.9
## ..$ margin : 'margin' num [1:4] 0points 0points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : logi FALSE
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ title : NULL
## $ aspect.ratio : NULL
## $ axis.title : NULL
## $ axis.title.x :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 1
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 2.75points 0points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.title.x.top :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 0
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 2.75points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.title.x.bottom : NULL
## $ axis.title.y :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 1
## ..$ angle : num 90
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 2.75points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.title.y.left : NULL
## $ axis.title.y.right :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 1
## ..$ angle : num -90
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 0points 2.75points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : chr "grey30"
## ..$ size : 'rel' num 0.8
## ..$ hjust : NULL
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text.x :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 1
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 2.2points 0points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text.x.top :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 0
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 2.2points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text.x.bottom : NULL
## $ axis.text.y :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : num 1
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 2.2points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text.y.left : NULL
## $ axis.text.y.right :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : num 0
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 0points 2.2points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text.theta : NULL
## $ axis.text.r :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : num 0.5
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 2.2points 0points 2.2points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.ticks : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ axis.ticks.x : NULL
## $ axis.ticks.x.top : NULL
## $ axis.ticks.x.bottom : NULL
## $ axis.ticks.y : NULL
## $ axis.ticks.y.left : NULL
## $ axis.ticks.y.right : NULL
## $ axis.ticks.theta : NULL
## $ axis.ticks.r : NULL
## $ axis.minor.ticks.x.top : NULL
## $ axis.minor.ticks.x.bottom : NULL
## $ axis.minor.ticks.y.left : NULL
## $ axis.minor.ticks.y.right : NULL
## $ axis.minor.ticks.theta : NULL
## $ axis.minor.ticks.r : NULL
## $ axis.ticks.length : 'simpleUnit' num 2.75points
## ..- attr(*, "unit")= int 8
## $ axis.ticks.length.x : NULL
## $ axis.ticks.length.x.top : NULL
## $ axis.ticks.length.x.bottom : NULL
## $ axis.ticks.length.y : NULL
## $ axis.ticks.length.y.left : NULL
## $ axis.ticks.length.y.right : NULL
## $ axis.ticks.length.theta : NULL
## $ axis.ticks.length.r : NULL
## $ axis.minor.ticks.length : 'rel' num 0.75
## $ axis.minor.ticks.length.x : NULL
## $ axis.minor.ticks.length.x.top : NULL
## $ axis.minor.ticks.length.x.bottom: NULL
## $ axis.minor.ticks.length.y : NULL
## $ axis.minor.ticks.length.y.left : NULL
## $ axis.minor.ticks.length.y.right : NULL
## $ axis.minor.ticks.length.theta : NULL
## $ axis.minor.ticks.length.r : NULL
## $ axis.line : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ axis.line.x : NULL
## $ axis.line.x.top : NULL
## $ axis.line.x.bottom : NULL
## $ axis.line.y : NULL
## $ axis.line.y.left : NULL
## $ axis.line.y.right : NULL
## $ axis.line.theta : NULL
## $ axis.line.r : NULL
## $ legend.background : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ legend.margin : 'margin' num [1:4] 5.5points 5.5points 5.5points 5.5points
## ..- attr(*, "unit")= int 8
## $ legend.spacing : 'simpleUnit' num 11points
## ..- attr(*, "unit")= int 8
## $ legend.spacing.x : NULL
## $ legend.spacing.y : NULL
## $ legend.key : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ legend.key.size : 'simpleUnit' num 1.2lines
## ..- attr(*, "unit")= int 3
## $ legend.key.height : NULL
## $ legend.key.width : NULL
## $ legend.key.spacing : 'simpleUnit' num 5.5points
## ..- attr(*, "unit")= int 8
## $ legend.key.spacing.x : NULL
## $ legend.key.spacing.y : NULL
## $ legend.frame : NULL
## $ legend.ticks : NULL
## $ legend.ticks.length : 'rel' num 0.2
## $ legend.axis.line : NULL
## $ legend.text :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : 'rel' num 0.8
## ..$ hjust : NULL
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ legend.text.position : NULL
## $ legend.title :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : num 0
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ legend.title.position : NULL
## $ legend.position : chr "right"
## $ legend.position.inside : NULL
## $ legend.direction : NULL
## $ legend.byrow : NULL
## $ legend.justification : chr "center"
## $ legend.justification.top : NULL
## $ legend.justification.bottom : NULL
## $ legend.justification.left : NULL
## $ legend.justification.right : NULL
## $ legend.justification.inside : NULL
## $ legend.location : NULL
## $ legend.box : NULL
## $ legend.box.just : NULL
## $ legend.box.margin : 'margin' num [1:4] 0cm 0cm 0cm 0cm
## ..- attr(*, "unit")= int 1
## $ legend.box.background : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ legend.box.spacing : 'simpleUnit' num 11points
## ..- attr(*, "unit")= int 8
## [list output truncated]
## - attr(*, "class")= chr [1:2] "theme" "gg"
## - attr(*, "complete")= logi TRUE
## - attr(*, "validate")= logi TRUE
#2.3 Hãy vẽ biểu đồ phân bố thu nhập (income) theo giới tính (gender):
#a. Theo số người
df <- read.csv("E:\\HOPT\\PTDLR\\DATA\\CHNS data full.csv", header = TRUE)
p = ggplot(df, aes(x = log(income), fill=factor(gender)))
p + geom_histogram(col="white") + labs(x="Thu nhập",y="SỐ người", title="Phấn bố thu nhập theo số người")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 241 rows containing non-finite outside the scale range
## (`stat_bin()`).
#b. Theo tỉ lệ
df <- read.csv("E:\\HOPT\\PTDLR\\DATA\\CHNS data full.csv", header = TRUE)
p = ggplot(df, aes(x = log(income), fill=factor(gender)))
p + geom_density()+ labs(x="Thu nhập",y="Tỷ lệ", title="Phấn bố thu nhập theo tỷ lệ")
## Warning: Removed 241 rows containing non-finite outside the scale range
## (`stat_density()`).
# biểu đồ thanh thể hiện thu nhập (income) theo trình độ học vấn (edu)
#3.1 Thể hiện thu nhập (income) theo trình độ học vấn (edu)
df <- read.csv("E:\\HOPT\\PTDLR\\DATA\\CHNS data full.csv", header = TRUE)
p = ggplot(data=df, aes(x=edu, y =income, fill=edu))
p + geom_bar(stat="identity")
## Warning: Removed 224 rows containing missing values or values outside the scale range
## (`geom_bar()`).
#3.2 Thêm giá trị thu nhập vào biểu đồ
df <- read.csv("E:\\HOPT\\PTDLR\\DATA\\CHNS data full.csv", header = TRUE)
p = ggplot(data=df, aes(x=edu, y =income, fill=edu))
p + geom_bar(stat="identity") + geom_text(aes(label=income, vjust=-2))+ theme(legend.position = "none")
## Warning: Removed 224 rows containing missing values or values outside the scale range
## (`geom_bar()`).
## Warning: Removed 224 rows containing missing values or values outside the scale range
## (`geom_text()`).
#Soạn biểu đồ hộp
#4.1 So sánh phân bố của thu nhập (income) theo giới tính (gender)
library(ggplot2)
df <- read.csv("E:\\HOPT\\PTDLR\\DATA\\CHNS data full.csv", header = TRUE)
p = ggplot(data=df, aes(x=factor(gender), y=log(income), col=factor(gender)))
p + geom_boxplot()
## Warning: Removed 241 rows containing non-finite outside the scale range
## (`stat_boxplot()`).
p + geom_boxplot() + geom_jitter(alpha=0.05)
## Warning: Removed 241 rows containing non-finite outside the scale range
## (`stat_boxplot()`).
## Warning: Removed 224 rows containing missing values or values outside the scale range
## (`geom_point()`).
#Việc 5: Soạn biểu đồ tương quan
#5.1 Hãy thể hiện mối liên quan giữa thu nhập (income)và tuổi (age)
df <- read.csv("E:\\HOPT\\PTDLR\\DATA\\CHNS data full.csv", header = TRUE)
p = ggplot(data=df, aes(x=age, y= log(income)))
p + geom_point()
## Warning: Removed 226 rows containing missing values or values outside the scale range
## (`geom_point()`).
#5.2 Hãy thể hiện mối liên quan giữa thu nhập (income)và tuổi (age)theo giới tính (gender)
df <- read.csv("E:\\HOPT\\PTDLR\\DATA\\CHNS data full.csv", header = TRUE)
p = ggplot(data=df, aes(x=age, y= log(income), col=factor(gender)))
p + geom_point()
## Warning: Removed 226 rows containing missing values or values outside the scale range
## (`geom_point()`).
#5.3 Thêm đường mô tả mối liên quan
df <- read.csv("E:\\HOPT\\PTDLR\\DATA\\CHNS data full.csv", header = TRUE)
p = ggplot(data=df, aes(x=age, y= log(income), col= factor(gender)))
p + geom_point() + geom_smooth()
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 243 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 226 rows containing missing values or values outside the scale range
## (`geom_point()`).
df <- read.csv("E:\\HOPT\\PTDLR\\DATA\\CHNS data full.csv", header = TRUE)
p = ggplot(data=df, aes(x=age, y= log(income), col= factor(gender)))
p + geom_point() + geom_smooth(method="lm", formula= y~x +I(x^2)+I (x^3))
## Warning: Removed 243 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Removed 226 rows containing missing values or values outside the scale range
## (`geom_point()`).
# Kết luận