file.exists("/Users/giangnguyen/Desktop/GiangNT/2025/Course_R/Datasets/birthwt.csv")
## [1] TRUE
bw = read.csv("/Users/giangnguyen/Desktop/GiangNT/2025/Course_R/Datasets/birthwt.csv")
bw2 = read.csv("/Users/giangnguyen/Desktop/GiangNT/2025/Course_R/Datasets/birthwt.csv")
dim(bw)
## [1] 189 11
nrow(bw)
## [1] 189
ncol(bw)
## [1] 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
tail(bw)
## id low age lwt race smoke ptl ht ui ftv bwt
## 184 78 1 14 101 3 1 1 0 0 0 2466
## 185 79 1 28 95 1 1 0 0 0 2 2466
## 186 81 1 14 100 3 0 0 0 0 2 2495
## 187 82 1 23 94 3 1 0 0 0 0 2495
## 188 83 1 17 142 2 0 0 1 0 0 2495
## 189 84 1 21 130 1 1 0 1 0 3 2495
bw$mwt = bw$lwt * 0.453592
head(bw$mwt)
## [1] 82.55374 70.30676 47.62716 48.98794 48.53434 56.24541
bw$ethnicity = factor( ifelse(bw$race == 1, "White", ifelse(bw$race == 2, "Black", "Other")), levels = c("White", "Black", "Other"))
head(bw$ethnicity)
## [1] Black Other White White White Other
## Levels: White Black Other
levels(bw$ethnicity)
## [1] "White" "Black" "Other"
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
ncol(bw1)
## [1] 3
nrow(bw1)
## [1] 189
bw3 = subset(bw, low == 1)
head(bw3)
## id low age lwt race smoke ptl ht ui ftv bwt mwt ethnicity
## 131 4 1 28 120 3 1 1 0 1 0 709 54.43104 Other
## 132 10 1 29 130 1 0 0 0 1 2 1021 58.96696 White
## 133 11 1 34 187 2 1 0 1 0 0 1135 84.82170 Black
## 134 13 1 25 105 3 0 1 1 0 0 1330 47.62716 Other
## 135 15 1 25 85 3 0 0 0 1 0 1474 38.55532 Other
## 136 16 1 27 150 3 0 0 0 0 0 1588 68.03880 Other
ncol(bw3)
## [1] 13
nrow(bw3)
## [1] 59
bw4 = subset(bw, low == 1 & smoke == 1)
head(bw4)
## id low age lwt race smoke ptl ht ui ftv bwt mwt ethnicity
## 131 4 1 28 120 3 1 1 0 1 0 709 54.43104 Other
## 133 11 1 34 187 2 1 0 1 0 0 1135 84.82170 Black
## 140 20 1 21 165 1 1 0 1 0 1 1790 74.84268 White
## 141 22 1 32 105 1 1 0 0 0 0 1818 47.62716 White
## 142 23 1 19 91 1 1 2 0 1 0 1885 41.27687 White
## 145 26 1 25 92 1 1 0 0 0 0 1928 41.73046 White
ncol(bw4)
## [1] 13
nrow(bw4)
## [1] 30
bw5 = bw4
head(bw5)
## id low age lwt race smoke ptl ht ui ftv bwt mwt ethnicity
## 131 4 1 28 120 3 1 1 0 1 0 709 54.43104 Other
## 133 11 1 34 187 2 1 0 1 0 0 1135 84.82170 Black
## 140 20 1 21 165 1 1 0 1 0 1 1790 74.84268 White
## 141 22 1 32 105 1 1 0 0 0 0 1818 47.62716 White
## 142 23 1 19 91 1 1 2 0 1 0 1885 41.27687 White
## 145 26 1 25 92 1 1 0 0 0 0 1928 41.73046 White
# Kết quả: Hiển thị số cột (biến số) của data frame "bw5"
ncol(bw5)
## [1] 13
nrow(bw5)
## [1] 30
if (!require(lessR)) install.packages("lessR")
## Loading required package: lessR
##
## lessR 4.4.2 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:base':
##
## sort_by
library(lessR)
summary(bw$bwt)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 709 2414 2977 2945 3487 4990
Histogram(bwt, data = bw, main = "Biểu đồ Historgram mô tả phân bố cân nặng trẻ sơ sinh (gram)", xlab = "Cân nặng trẻ (gram)")
## >>> Suggestions
## bin_width: set the width of each bin
## bin_start: set the start of the first bin
## bin_end: set the end of the last bin
## Histogram(bwt, density=TRUE) # smoothed curve + histogram
## Plot(bwt) # Violin/Box/Scatterplot (VBS) plot
##
## --- bwt ---
##
## n miss mean sd min mdn max
## 189 0 2944.59 729.21 709.00 2977.00 4990.00
##
##
##
## --- Outliers --- from the box plot: 1
##
## Small Large
## ----- -----
## 709.0
##
##
## Bin Width: 500
## Number of Bins: 9
##
## Bin Midpnt Count Prop Cumul.c Cumul.p
## -----------------------------------------------------
## 500 > 1000 750 1 0.01 1 0.01
## 1000 > 1500 1250 4 0.02 5 0.03
## 1500 > 2000 1750 14 0.07 19 0.10
## 2000 > 2500 2250 40 0.21 59 0.31
## 2500 > 3000 2750 38 0.20 97 0.51
## 3000 > 3500 3250 45 0.24 142 0.75
## 3500 > 4000 3750 38 0.20 180 0.95
## 4000 > 4500 4250 7 0.04 187 0.99
## 4500 > 5000 4750 2 0.01 189 1.00
##
table(bw$ethnicity)
##
## White Black Other
## 96 26 67
BarChart(ethnicity, data = bw, main = "Biểu đồ thanh (Bar Chart) mô tả phân bố chủng tộc của mẹ", xlab = "Chủng tộc")
## >>> Suggestions
## BarChart(ethnicity, horiz=TRUE) # horizontal bar chart
## BarChart(ethnicity, fill="reds") # red bars of varying lightness
## PieChart(ethnicity) # doughnut (ring) chart
## Plot(ethnicity) # bubble plot
## Plot(ethnicity, stat="count") # lollipop plot
##
## --- ethnicity ---
##
## Missing Values: 0
##
## White Black Other Total
## Frequencies: 96 26 67 189
## Proportions: 0.508 0.138 0.354 1.000
##
## Chi-squared test of null hypothesis of equal probabilities
## Chisq = 39.270, df = 2, p-value = 0.000
cor(bw$lwt, bw$bwt)
## [1] 0.1857333
ScatterPlot(lwt, bwt, data = bw, fit = "lm", main = "Biểu đồ phân tán (Scatter Plot) mô tả tương quan giữa cân nặng mẹ và trẻ sơ sinh", xlab = "Cân nặng mẹ (pound)", ylab = "Cân nặng trẻ (gram)")
##
##
## >>> Suggestions or enter: style(suggest=FALSE)
## Plot(lwt, bwt, enhance=TRUE) # many options
## Plot(lwt, bwt, color="red") # exterior edge color of points
## Plot(lwt, bwt, out_cut=.10) # label top 10% from center as outliers
##
##
## >>> 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
##
by(bw[, c("lwt", "bwt")], bw$ethnicity, function(x) cor(x$lwt, x$bwt))
## bw$ethnicity: White
## [1] 0.1998637
## ------------------------------------------------------------
## bw$ethnicity: Black
## [1] 0.1507008
## ------------------------------------------------------------
## bw$ethnicity: Other
## [1] 0.2129624
ScatterPlot(lwt, bwt, data = bw, by = ethnicity, fit = "lm", main = "Biểu đồ phân tán (Scatter Plot) mô tả tương quan giữa cân nặng mẹ và trẻ sơ sinh theo chủng tộc", xlab = "Cân nặng mẹ (pound)", ylab = "Cân nặng trẻ (gram)")
##
##
## >>> 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
##
## ethnicity: White Line: b0 = 2442.418 b1 = 5.000 Linear Model MSE = 514,065.615 Rsq = 0.040
##
## ethnicity: Black Line: b0 = 2363.222 b1 = 2.428 Linear Model MSE = 415,263.548 Rsq = 0.023
##
## ethnicity: Other Line: b0 = 2070.778 b1 = 6.120 Linear Model MSE = 505,570.324 Rsq = 0.045
##