library(table1)
##
## Attaching package: 'table1'
## The following objects are masked from 'package:base':
##
## units, units<-
library(lessR)
##
## lessR 4.3.9 feedback: gerbing@pdx.edu
## --------------------------------------------------------------
## > d <- Read("") Read text, Excel, SPSS, SAS, or R data file
## 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, and descriptive statistics 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
library(ggplot2)
bw = read.csv("/Users/121493/Dropbox/_Conferences and Workshops/Datasets/birthwt.csv")
# Listing the first 6 lines
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
# Listing the last 10 lines
tail(bw, 10)
## id low age lwt race smoke ptl ht ui ftv bwt
## 180 71 1 17 120 2 0 0 0 0 2 2438
## 181 75 1 26 154 3 0 1 1 0 1 2442
## 182 76 1 20 105 3 0 0 0 0 3 2450
## 183 77 1 26 190 1 1 0 0 0 0 2466
## 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
# Coding data
bw$ethnicity [bw$race==1] = "White"
bw$ethnicity [bw$race==2] = "Black"
bw$ethnicity [bw$race==3] = "Others"
bw$ethnicity = factor(bw$ethnicity, levels=c("White", "Others", "Black"))
bw$smoking = ifelse(bw$smoke==1, "Smoking", "Non Smoking")
bw$low.bw = ifelse(bw$low==1, "Low BW", "Normal")
bw$mother.wt = bw$lwt * 0.45
head(bw)
## id low age lwt race smoke ptl ht ui ftv bwt ethnicity smoking low.bw
## 1 85 0 19 182 2 0 0 0 1 0 2523 Black Non Smoking Normal
## 2 86 0 33 155 3 0 0 0 0 3 2551 Others Non Smoking Normal
## 3 87 0 20 105 1 1 0 0 0 1 2557 White Smoking Normal
## 4 88 0 21 108 1 1 0 0 1 2 2594 White Smoking Normal
## 5 89 0 18 107 1 1 0 0 1 0 2600 White Smoking Normal
## 6 91 0 21 124 3 0 0 0 0 0 2622 Others Non Smoking Normal
## mother.wt
## 1 81.90
## 2 69.75
## 3 47.25
## 4 48.60
## 5 48.15
## 6 55.80
table1(~age + ethnicity + smoking + mother.wt + bwt, data=bw)
Overall (N=189) |
|
---|---|
age | |
Mean (SD) | 23.2 (5.30) |
Median [Min, Max] | 23.0 [14.0, 45.0] |
ethnicity | |
White | 96 (50.8%) |
Others | 67 (35.4%) |
Black | 26 (13.8%) |
smoking | |
Non Smoking | 115 (60.8%) |
Smoking | 74 (39.2%) |
mother.wt | |
Mean (SD) | 58.4 (13.8) |
Median [Min, Max] | 54.5 [36.0, 113] |
bwt | |
Mean (SD) | 2940 (729) |
Median [Min, Max] | 2980 [709, 4990] |
# Phan tich mo ta theo low.bw
table1(~age + ethnicity + smoking + mother.wt + bwt | low.bw, data=bw)
Low BW (N=59) |
Normal (N=130) |
Overall (N=189) |
|
---|---|---|---|
age | |||
Mean (SD) | 22.3 (4.51) | 23.7 (5.58) | 23.2 (5.30) |
Median [Min, Max] | 22.0 [14.0, 34.0] | 23.0 [14.0, 45.0] | 23.0 [14.0, 45.0] |
ethnicity | |||
White | 23 (39.0%) | 73 (56.2%) | 96 (50.8%) |
Others | 25 (42.4%) | 42 (32.3%) | 67 (35.4%) |
Black | 11 (18.6%) | 15 (11.5%) | 26 (13.8%) |
smoking | |||
Non Smoking | 29 (49.2%) | 86 (66.2%) | 115 (60.8%) |
Smoking | 30 (50.8%) | 44 (33.8%) | 74 (39.2%) |
mother.wt | |||
Mean (SD) | 55.0 (12.0) | 60.0 (14.3) | 58.4 (13.8) |
Median [Min, Max] | 54.0 [36.0, 90.0] | 55.6 [38.3, 113] | 54.5 [36.0, 113] |
bwt | |||
Mean (SD) | 2100 (391) | 3330 (478) | 2940 (729) |
Median [Min, Max] | 2210 [709, 2500] | 3270 [2520, 4990] | 2980 [709, 4990] |
# Phan bo can nang cua dua tre
Histogram(bwt, xlab="Cân nặng của trẻ sơ sanh (g)", ylab="Số bà mẹ", data=bw)
## >>> 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
# Phân bố chủng tộc
BarChart(ethnicity, data=bw)
## >>> 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 Others Black Total
## Frequencies: 96 67 26 189
## Proportions: 0.508 0.354 0.138 1.000
##
## Chi-squared test of null hypothesis of equal probabilities
## Chisq = 39.270, df = 2, p-value = 0.000
# Mối liên quan giữa cáºn nặng cá»§a mẹ và con
Plot(mother.wt, bwt, xlab="Cân nặng của mẹ", ylab="Cân nặng của con", fit="lm", data=bw)
##
## >>> Suggestions or enter: style(suggest=FALSE)
## Plot(mother.wt, bwt, enhance=TRUE) # many options
## Plot(mother.wt, bwt, color="red") # exterior edge color of points
## Plot(mother.wt, 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 mother.wt 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.62 b1 = 9.84 Fit: MSE = 516,155 Rsq = 0.034
##