讀取資料
data <- read.table("C:/Users/X510/Desktop/2020-04-20-In-class exercises-2/langMathDutch.txt", header = T)
head(data)## school pupil IQV size lang arith
## 1 1 17001 15.0 29 46 24
## 2 1 17002 14.5 29 45 19
## 3 1 17003 9.5 29 33 24
## 4 1 17004 11.0 29 46 26
## 5 1 17005 8.0 29 20 9
## 6 1 17006 9.5 29 30 13
將class, IQ,分為三個等級
## 0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%
## 5.0 12.6 16.0 19.0 22.0 24.0 26.0 28.0 30.0 32.0 37.0
dta_size <- with(data, cut(size, ordered=T, breaks=c(0, 20, 27, 40),
labels=c("Small", "Medium", "Large"))) ##分類class
quantile(data$IQV, probs=seq(from=0, to=1, by=.1))## 0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%
## 4.0 9.3 10.5 11.0 11.5 12.0 12.5 13.0 13.5 14.5 18.0
dta_IQ <- with(data, cut(IQV, ordered=T, breaks=c(0, 11.2, 12.7, 20),
labels=c("Low", "Middle", "High"))) ##分類IQ
library(tidyr)
dta <- cbind(dta_IQ ,cbind(data, dta_size)) ##合併class, IQ
dta.1 <- unite(dta, "class_IQ",c(dta_size, dta_IQ), sep = ",",remove = T)
head(dta.1)## class_IQ school pupil IQV size lang arith
## 1 Large,High 1 17001 15.0 29 46 24
## 2 Large,High 1 17002 14.5 29 45 19
## 3 Large,Low 1 17003 9.5 29 33 24
## 4 Large,Low 1 17004 11.0 29 46 26
## 5 Large,Low 1 17005 8.0 29 20 9
## 6 Large,Low 1 17006 9.5 29 30 13
繪圖
library(ggplot2)
p0 <- ggplot(data=dta.1, aes(x=lang, y=arith)) + labs(x='Language score', y='Arithmetic score') +
geom_point() + stat_smooth(data=dta.1, formula=y ~ x, method='lm', se=T) +
facet_wrap(. ~ class_IQ, ncol=3)
p0