Đọc dữ liệu vào R setwd“c:/works/stats” bc... Tần số theo nhóm : barplot agegroup... Tính liên tục của số liệu: stripchart stripchartlm, xlab=“Lean mass; kg" Lean mass; kg?. 14 Tóm tắt c
Trang 1Phân tích số liệu bằng R:
Phân tích đồ thị
2
Tổng quan
• Số liệu
• Đồ thị cột- Barchart
• Đồ thị tần số- Historgram
• Đồ thị đường thẳng-Stripchart
• Đồ thị hộp-Boxplot
• Đồ thị xy- Scatter plot
3
Số liệu
• Số liệu về thành phần của thân thể đo bằng phương
pháp hấp thu tia X
• 43 nam và nữ tuổi từ 11 đến 28
• Tên biến:
– id
– age
– sex
– dur
– weight
– height
– lm (lean mass)
– pclm (percent lean mass)
– fm (fat mass)
– pcfm (percent fat mass)
– bmc (bone mineral contents)
Trang 2Đọc dữ liệu vào R
setwd(“c:/works/stats”)
bc <- read.table(“comp.txt”, header=T)
attach(bc)
names(bc)
[1] "id" "age" "sex" "dur" "weight"
"height" "lm" "pclm"
[9] "fm" "pcfm" "bmc"
5
Xem số liệu
bc
id age sex dur weight height lm pclm fm pcfm bmc
1 1 15 M 5 39 148 32.96 84.50 4.86 12.5 1.33
3 3 11 M 4 23 132 18.51 80.50 2.99 13.0 0.74
5 5 19 M 6 56 166 46.63 83.00 5.61 10.2 2.56
7 7 16 M 8 53 170 45.23 85.00 5.15 9.8 2.21
9 9 21 M 8 46 166 39.44 85.70 4.64 10.1 2.00
11 11 13 M 5 32 142 25.50 79.70 4.26 13.9 0.99
40 40 12 M 10 39 155 33.00 84.60 3.50 9.2 1.43
42 42 22 M 7 46 157 38.50 84.00 4.63 10.3 1.86
6
Tần số dạng cột: barplot
freq <- table(sex)
barplot(freq)
barplot(freq, horiz=T, main="Sex distribution")
Se x distribution
0 5 10 15 20 25 30
Trang 3Tần số theo nhóm : barplot
agegroup <- cut(age, 3)
agesex <- table(sex, agegroup)
barplot(agesex)
(11,16.7] (16.7,22.3] (22.3,28]
8
agegroup <- cut(age, 3)
agesex <- table(sex, agegroup)
barplot(agesex, xlab="Age group")
barplot(agesex, beside=T, xlab="Age group")
(11,16.7] (16.7,22.3] (22.3,28]
Age group
(11,16.7] (16.7,22.3 ] (22.3,28]
A ge group
Tần số theo nhóm : barplot
9
Phân phối số liệu: Histogram
par(mfrow=c(2,2))
hist(age)
hist(age, breaks=20)
hist(age, breaks=50)
Histogram of age
age
10 15 20 25
Histo gram of age
age
15 20 25
Histogram of age
age
15 20 25
Histo gram of age
age
15 20 25
Trang 4Phân phối số liệu: Histogram
par(mfrow=c(2,2))
hist(age)
hist(weight)
hist(lm)
His togram of age
age
10 15 20 25
Histogram of w eight
weight
20 30 40 50 60
His togram of lm
lm
15 20 25 30 35 40 45 50
Histogram of fm
fm
2 4 6 8 10 12 14
11
Phân phối số liệu: Hàm mật độ-plot(density)
hist(lm, main="Distribution of lean mass")
plot(density(lm), main="Distribution of lean mass")
Distrib utio n o f lean m ass
lm
15 20 25 30 35 40 45 50
10 20 30 4 0 50
D is tribution o f lea n m a ss
N = 43 Bandwid th = 2 60 7
12
Phân phối chuẩn? qqnorm
• qqnorm(lm)
Normal Q-Q Plot
The oretical Quantiles
Trang 5Tính liên tục của số liệu: stripchart
stripchart(lm, xlab=“Lean mass; kg")
Lean mass; kg
?
14
Tóm tắt của số liệu liên tục: boxplot
boxplot(fm) boxplot(lm)
LM
Min 1st Qu Median Mean 3rd Qu Max
18.51 31.91 35.92 35.65 40.14 46.63
FM
Min 1st Qu Median Mean 3rd Qu Max
2.990 4.250 5.270 6.500 8.795 12.800
15
boxplot(fm ~ sex) boxplot(lm ~ sex)
Lean mass by sex Fat mass by sex
Tóm tắt của số liệu liên tục: boxplot
Trang 6Phân tích mức độ liên kết: scatter plot
plot(lm ~ age) plot(lm ~ age, pch=16)
age
age
17
line <- lm(lm ~ age)
plot(lm ~ age, pch=16)
abline(line)
age
Phân tích mức độ liên kết: scatter plot
18
plot(lm ~ age, pch=ifelse(sex=="M", "M", "F"),
xlab="Age", ylab="Kg")
M M
M
M
M
M M
M
M M
M
M M
F
F
F F
F
M
M
F
F
M
M M
F
M
M
M
F
M
M
M
M
M
M
M
M M
A ge
Phân tích mức độ liên kết: scatter plot
Trang 7Phân tích nhiều liên kết-multiple
associations
data <- data.frame(age, weight, lm, fm, bmc)
pairs(data)
age
25 45 4 8 10 12
weight
lm
fm
15 25 20 30 40 1.0 1.5 2.0 2.5
bmc
20
Phân tích nhiều sự liên kết –
nhiều đồ thị
matrix.cor <- function(x, y, digits=2, prefix="",
cex.cor){ usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1)) r <- abs(cor(x, y))
txt <- format(c(r, 0.123456789), digits=digits)[1]
txt <- paste(prefix, txt, sep="")
if(missing(cex.cor)) cex <- 0.8/strwidth(txt)
test <- cor.test(x,y) # borrowed from printCoefmat
Signif <- symnum(test$p.value, corr = FALSE, na =
FALSE, cutpoints = c(0, 0.001, 0.01,
0.05, 0.1, 1), symbols = c("***",
"**", "*", ".", " ")) text(0.5, 0.5, txt, cex
= cex * r) text(.8, 8, Signif, cex=cex, col=2)}
pairs(data,lower.panel=panel.smooth,
upper.panel=matrix.cor)
21
Kết quả
age
25 35 45 55
0.48** 0.36*
4 8 10 12
0 0 9 5
0.56***
weight 0.88 *** 0 1 1 0.85 ***
lm 0.36*
0.86 ***
fm 0.16
15 20 25 20 30 40 1.0 2.0
bmc
Trang 8Tóm tắt
• R mạnh về phân tích đồ thị
• Bước đầu tiên trong phân tích số liệu: phân tích
đồ thị
• Nhìn đồ thị lưu ý
– Dạng phân phối
– Sự khác biệt
– Tính tương hỗ, liên kết