?Vocab
## No documentation for 'Vocab' in specified packages and libraries:
## you could try '??Vocab'
head(carData::Vocab)
##          year    sex education vocabulary
## 19740001 1974   Male        14          9
## 19740002 1974   Male        16          9
## 19740003 1974 Female        10          9
## 19740004 1974 Female        10          5
## 19740005 1974 Female        12          8
## 19740006 1974   Male        16          8
##前六列看趨勢
dta<- carData::Vocab
pacman::p_load(lattice)
##切割檔案
dta1974 <- subset(dta, dta$year=="1974")
xyplot(vocabulary ~ education, groups=sex, data=dta1974, type=c("p", "g"), auto.key=list(columns=2))

dta1984 <- subset(dta, dta$year=="1984")
xyplot(vocabulary ~ education, groups=sex, data=dta1984, type=c("p", "g"), auto.key=list(columns=2))

dta1994 <- subset(dta, dta$year=="1994")
xyplot(vocabulary ~ education, groups=sex, data=dta1994, type=c("p", "g"), auto.key=list(columns=2))

dta2004 <- subset(dta, dta$year=="2004")
xyplot(vocabulary ~ education, groups=sex, data=dta2004, type=c("p", "g"), auto.key=list(columns=2))

##終於一次全部呈現,大體上隨著教育年數的增長,單字量也會增加
lattice::xyplot(vocabulary ~ education | year, groups = sex, type = c("p", "g", "r"), data =dta , auto.key = list(columns = 2), xlab = "Education", ylab = "Vocabulary")

##男性識字和年代迴歸較為穩定
malec <- subset(dta, dta$sex=="Male")
lapply(split(malec, malec$year), function(x) coef(lm(x$vocabulary ~ x$education)))
## $`1974`
## (Intercept) x$education 
##   1.5318434   0.3713183 
## 
## $`1976`
## (Intercept) x$education 
##   1.6342960   0.3555403 
## 
## $`1978`
## (Intercept) x$education 
##   0.9762161   0.3963762 
## 
## $`1982`
## (Intercept) x$education 
##   0.9730291   0.3832637 
## 
## $`1984`
## (Intercept) x$education 
##    1.678465    0.337124 
## 
## $`1987`
## (Intercept) x$education 
##   0.8103651   0.3818373 
## 
## $`1988`
## (Intercept) x$education 
##   1.0459936   0.3592442 
## 
## $`1989`
## (Intercept) x$education 
##   1.0596176   0.3708525 
## 
## $`1990`
## (Intercept) x$education 
##   1.7000935   0.3377029 
## 
## $`1991`
## (Intercept) x$education 
##   1.2504604   0.3683962 
## 
## $`1993`
## (Intercept) x$education 
##   1.6384884   0.3221049 
## 
## $`1994`
## (Intercept) x$education 
##   1.8684770   0.3146151 
## 
## $`1996`
## (Intercept) x$education 
##   0.8221711   0.3770325 
## 
## $`1998`
## (Intercept) x$education 
##   1.5199973   0.3314754 
## 
## $`2000`
## (Intercept) x$education 
##   1.1203888   0.3558918 
## 
## $`2004`
## (Intercept) x$education 
##   1.4259424   0.3411153 
## 
## $`2006`
## (Intercept) x$education 
##   2.1383454   0.2952926 
## 
## $`2008`
## (Intercept) x$education 
##   1.4212286   0.3277987 
## 
## $`2010`
## (Intercept) x$education 
##   1.7996389   0.3135749 
## 
## $`2012`
## (Intercept) x$education 
##   1.7303105   0.3061534 
## 
## $`2014`
## (Intercept) x$education 
##   1.4804789   0.3262112 
## 
## $`2016`
## (Intercept) x$education 
##   1.8562367   0.3031146
##女性識字和年代迴歸隨年代有下降趨勢
femalec <- subset(dta, dta$sex=="Female")
lapply(split(femalec, femalec$year), function(x) coef(lm(x$vocabulary ~ x$education)))
## $`1974`
## (Intercept) x$education 
##   1.5652579   0.3816095 
## 
## $`1976`
## (Intercept) x$education 
##   1.7021281   0.3824002 
## 
## $`1978`
## (Intercept) x$education 
##   1.3006416   0.4002707 
## 
## $`1982`
## (Intercept) x$education 
##   0.9829602   0.3949758 
## 
## $`1984`
## (Intercept) x$education 
##   1.4536872   0.3728698 
## 
## $`1987`
## (Intercept) x$education 
##   0.9647931   0.3843508 
## 
## $`1988`
## (Intercept) x$education 
##   1.1634561   0.3763999 
## 
## $`1989`
## (Intercept) x$education 
##   1.0682600   0.3863606 
## 
## $`1990`
## (Intercept) x$education 
##   0.4594812   0.4346902 
## 
## $`1991`
## (Intercept) x$education 
##   1.1543766   0.3875821 
## 
## $`1993`
## (Intercept) x$education 
##   1.7388287   0.3286325 
## 
## $`1994`
## (Intercept) x$education 
##   1.6453365   0.3422146 
## 
## $`1996`
## (Intercept) x$education 
##   1.1482811   0.3727178 
## 
## $`1998`
## (Intercept) x$education 
##   1.4472751   0.3592843 
## 
## $`2000`
## (Intercept) x$education 
##   1.9276040   0.3155532 
## 
## $`2004`
## (Intercept) x$education 
##    2.104150    0.304056 
## 
## $`2006`
## (Intercept) x$education 
##   2.7777171   0.2535376 
## 
## $`2008`
## (Intercept) x$education 
##   2.6074315   0.2553971 
## 
## $`2010`
## (Intercept) x$education 
##   1.3520300   0.3468821 
## 
## $`2012`
## (Intercept) x$education 
##   1.7535298   0.3080832 
## 
## $`2014`
## (Intercept) x$education 
##   2.3445239   0.2663464 
## 
## $`2016`
## (Intercept) x$education 
##   2.0055919   0.2928955