# Đọc dữ liệu “birthwt.csv” vào R và gọi dữ liệu là “bw”
bw = read.csv('/Users/vothainguyetcam/Library/CloudStorage/OneDrive-umc.edu.vn/Archives/Documents/find info/lectures/KHÓA TẬP HUẤN "ỨNG DỤNG AI TRONG PHÂN TÍCH DỮ LIỆU VỚI R" 15.06.25/bài tập/birthwt.csv')
head(bw)
## id low age lwt race smoke ptl ht ui ftv bwt
## 1 85 0 19 182 2 0 0 0 1 0 2523
## 2 86 0 33 155 3 0 0 0 0 3 2551
## 3 87 0 20 105 1 1 0 0 0 1 2557
## 4 88 0 21 108 1 1 0 0 1 2 2594
## 5 89 0 18 107 1 1 0 0 1 0 2600
## 6 91 0 21 124 3 0 0 0 0 0 2622
# Có bao nhiêu biến số (variable) và quan sát (observation)
dim(bw)
## [1] 189 11
head(bw)
## id low age lwt race smoke ptl ht ui ftv bwt
## 1 85 0 19 182 2 0 0 0 1 0 2523
## 2 86 0 33 155 3 0 0 0 0 3 2551
## 3 87 0 20 105 1 1 0 0 0 1 2557
## 4 88 0 21 108 1 1 0 0 1 2 2594
## 5 89 0 18 107 1 1 0 0 1 0 2600
## 6 91 0 21 124 3 0 0 0 0 0 2622
# Tạo biến số mới mwt là cân nặng của mẹ tính bằng kg
bw$mwt <- bw$lwt * 0.45359237
head(bw)
## id low age lwt race smoke ptl ht ui ftv bwt mwt
## 1 85 0 19 182 2 0 0 0 1 0 2523 82.55381
## 2 86 0 33 155 3 0 0 0 0 3 2551 70.30682
## 3 87 0 20 105 1 1 0 0 0 1 2557 47.62720
## 4 88 0 21 108 1 1 0 0 1 2 2594 48.98798
## 5 89 0 18 107 1 1 0 0 1 0 2600 48.53438
## 6 91 0 21 124 3 0 0 0 0 0 2622 56.24545
# Tạo biến số mới ethnicity là biến factor
bw$ethnicity[bw$race==1]<-"White"
bw$ethnicity[bw$race==2]<-"Black"
bw$ethnicity[bw$race==3]<-"Other"
head(bw)
## id low age lwt race smoke ptl ht ui ftv bwt mwt ethnicity
## 1 85 0 19 182 2 0 0 0 1 0 2523 82.55381 Black
## 2 86 0 33 155 3 0 0 0 0 3 2551 70.30682 Other
## 3 87 0 20 105 1 1 0 0 0 1 2557 47.62720 White
## 4 88 0 21 108 1 1 0 0 1 2 2594 48.98798 White
## 5 89 0 18 107 1 1 0 0 1 0 2600 48.53438 White
## 6 91 0 21 124 3 0 0 0 0 0 2622 56.24545 Other
# Tạo 1 tập dữ liệu bw1 chỉ gồm 3 biến số id, low và bwt
bw1=bw[,c("id", "low", "bwt")]
head(bw1)
## id low bwt
## 1 85 0 2523
## 2 86 0 2551
## 3 87 0 2557
## 4 88 0 2594
## 5 89 0 2600
## 6 91 0 2622
bw1.1=bw[,c(1, 2,8:11)]
head(bw1.1)
## id low ht ui ftv bwt
## 1 85 0 0 1 0 2523
## 2 86 0 0 0 3 2551
## 3 87 0 0 0 1 2557
## 4 88 0 0 1 2 2594
## 5 89 0 0 1 0 2600
## 6 91 0 0 0 0 2622
# Tạo 1 tập dữ liệu bw2 chỉ gồm những thai phụ có cân nặng thấp (low = 1)
bw2=subset(bw,low==1)
# Mô tả đặc điểm tuổi của mẹ (age), cân nặng của mẹ (lwt) và cân nặng của con (bwt)
library (table1)
##
## Attaching package: 'table1'
## The following objects are masked from 'package:base':
##
## units, units<-
table1(~age + lwt + bwt, data=bw)
| Overall (N=189) |
|
|---|---|
| age | |
| Mean (SD) | 23.2 (5.30) |
| Median [Min, Max] | 23.0 [14.0, 45.0] |
| lwt | |
| Mean (SD) | 130 (30.6) |
| Median [Min, Max] | 121 [80.0, 250] |
| bwt | |
| Mean (SD) | 2940 (729) |
| Median [Min, Max] | 2980 [709, 4990] |
# Mô tả đặc điểm tuổi của mẹ (age), cân nặng của mẹ (lwt), tình trạng hút thuốc trong thai kỳ (smoke) , chủng tộc (race), và cân nặng của con (bwt) theo tình trạng trẻ thiếu cân (low)
table1(~age + lwt + bwt + factor(smoke)| low, data=bw)
## Warning in table1.formula(~age + lwt + bwt + factor(smoke) | low, data = bw):
## Terms to the right of '|' in formula 'x' define table columns and are expected
## to be factors with meaningful labels.
| 0 (N=130) |
1 (N=59) |
Overall (N=189) |
|
|---|---|---|---|
| age | |||
| Mean (SD) | 23.7 (5.58) | 22.3 (4.51) | 23.2 (5.30) |
| Median [Min, Max] | 23.0 [14.0, 45.0] | 22.0 [14.0, 34.0] | 23.0 [14.0, 45.0] |
| lwt | |||
| Mean (SD) | 133 (31.7) | 122 (26.6) | 130 (30.6) |
| Median [Min, Max] | 124 [85.0, 250] | 120 [80.0, 200] | 121 [80.0, 250] |
| bwt | |||
| Mean (SD) | 3330 (478) | 2100 (391) | 2940 (729) |
| Median [Min, Max] | 3270 [2520, 4990] | 2210 [709, 2500] | 2980 [709, 4990] |
| factor(smoke) | |||
| 0 | 86 (66.2%) | 29 (49.2%) | 115 (60.8%) |
| 1 | 44 (33.8%) | 30 (50.8%) | 74 (39.2%) |
# vẽ biểu đồ
library(lessR)
##
## lessR 4.4.3 feedback: gerbing@pdx.edu
## --------------------------------------------------------------
## > d <- Read("") Read data file, many formats available, e.g., Excel
## d is default data frame, data= in analysis routines optional
##
## Many examples of reading, writing, and manipulating data,
## graphics, testing means and proportions, regression, factor analysis,
## customization, forecasting, and aggregation from pivot tables
## Enter: browseVignettes("lessR")
##
## View lessR updates, now including time series forecasting
## Enter: news(package="lessR")
##
## Interactive data analysis
## Enter: interact()
##
## Attaching package: 'lessR'
## The following object is masked from 'package:table1':
##
## label
## The following object is masked from 'package:base':
##
## sort_by
Plot(lwt, bwt, fit="lm", data=bw)
##
##
## >>> Suggestions or enter: style(suggest=FALSE)
## Plot(lwt, bwt, enhance=TRUE) # many options
## Plot(lwt, bwt, fill="skyblue") # interior fill color of points
## Plot(lwt, bwt, MD_cut=6) # Mahalanobis distance from center > 6 is an outlier
##
##
## >>> Pearson's product-moment correlation
##
## Number of paired values with neither missing, n = 189
## Sample Correlation of lwt and bwt: r = 0.186
##
## Hypothesis Test of 0 Correlation: t = 2.585, df = 187, p-value = 0.011
## 95% Confidence Interval for Correlation: 0.044 to 0.320
##
##
## Line: b0 = 2369.624 b1 = 4.429 Linear Model MSE = 516,155.173 Rsq = 0.034
##
library(lattice)
histogram(~ bwt,
data = bw,
xlab = "Cân nặng trẻ sơ sinh tính bằng (g)",
ylab = "Số bà mẹ",
main = "Phân bố cân nặng trẻ sơ sinh")