#load & see 
library(datasets)
dta <- datasets::ChickWeight
head(dta)
##   weight Time Chick Diet
## 1     42    0     1    1
## 2     51    2     1    1
## 3     59    4     1    1
## 4     64    6     1    1
## 5     76    8     1    1
## 6     93   10     1    1
#understand data
help(ChickWeight)
## starting httpd help server ... done
library(lattice)

#draw plot (see note)
stripplot(weight ~ Time | Chick, 
          data=dta,
          pch=16, 
          col="purple", 
          xlab="Time",
          ylab="weight",
          par.settings=standard.theme(color=FALSE))

p1 <- xyplot(weight ~ Time | Chick,
       data=dta, 
       type=c("p", "g"))

p1

#Extract regression coefficients
#(see Data wrangling note P11)

lapply(split(dta, dta$Chick), function(x) coef(lm(x$Time ~ x$weight)))
## $`18`
## (Intercept)    x$weight 
##        19.5        -0.5 
## 
## $`16`
## (Intercept)    x$weight 
## -27.8253707   0.6803954 
## 
## $`15`
## (Intercept)    x$weight 
## -18.4080146   0.4225865 
## 
## $`13`
## (Intercept)    x$weight 
## -17.6319500   0.4208641 
## 
## $`9`
## (Intercept)    x$weight 
## -14.5742419   0.3140564 
## 
## $`20`
## (Intercept)    x$weight 
##  -9.8882713   0.2653127 
## 
## $`10`
## (Intercept)    x$weight 
##  -9.4215789   0.2447933 
## 
## $`8`
## (Intercept)    x$weight 
##  -8.3654135   0.1996241 
## 
## $`17`
## (Intercept)    x$weight 
##  -9.3952561   0.2195884 
## 
## $`19`
## (Intercept)    x$weight 
##  -4.7998578   0.1811703 
## 
## $`4`
## (Intercept)    x$weight 
##  -4.9811534   0.1600452 
## 
## $`6`
## (Intercept)    x$weight 
##  -5.5896906   0.1451108 
## 
## $`11`
## (Intercept)    x$weight 
##  -4.5672364   0.1191833 
## 
## $`3`
## (Intercept)    x$weight 
##   -2.440880    0.115317 
## 
## $`1`
## (Intercept)    x$weight 
##  -2.4868258   0.1200313 
## 
## $`12`
## (Intercept)    x$weight 
##  -2.1128825   0.1142108 
## 
## $`2`
## (Intercept)    x$weight 
##  -2.5741368   0.1125015 
## 
## $`5`
## (Intercept)    x$weight 
##  -1.2636380   0.0961603 
## 
## $`14`
## (Intercept)    x$weight 
## -1.48394792  0.08194239 
## 
## $`7`
## (Intercept)    x$weight 
##  0.05002390  0.07244429 
## 
## $`24`
## (Intercept)    x$weight 
## -26.4252630   0.5636518 
## 
## $`30`
## (Intercept)    x$weight 
##  -6.3103604   0.1664447 
## 
## $`22`
## (Intercept)    x$weight 
##  -6.5982853   0.1680091 
## 
## $`23`
## (Intercept)    x$weight 
##  -5.6175336   0.1483997 
## 
## $`27`
## (Intercept)    x$weight 
##  -3.8762478   0.1339736 
## 
## $`28`
## (Intercept)    x$weight 
##  -2.3000411   0.1017322 
## 
## $`26`
## (Intercept)    x$weight 
## -1.67783363  0.09614122 
## 
## $`25`
## (Intercept)    x$weight 
## -1.51001714  0.08684928 
## 
## $`29`
## (Intercept)    x$weight 
##  0.32039085  0.07470935 
## 
## $`21`
## (Intercept)    x$weight 
## -0.79566738  0.06348149 
## 
## $`33`
## (Intercept)    x$weight 
##   -6.367863    0.157490 
## 
## $`37`
## (Intercept)    x$weight 
##  -4.0271029   0.1457929 
## 
## $`36`
## (Intercept)    x$weight 
## -2.27498368  0.09777628 
## 
## $`31`
## (Intercept)    x$weight 
## -1.24063585  0.09454804 
## 
## $`39`
## (Intercept)    x$weight 
## -0.93763023  0.08830016 
## 
## $`38`
## (Intercept)    x$weight 
## -0.21632004  0.07821771 
## 
## $`32`
## (Intercept)    x$weight 
## -0.73981836  0.07397029 
## 
## $`40`
## (Intercept)    x$weight 
## -0.33811875  0.07142117 
## 
## $`34`
## (Intercept)    x$weight 
##  0.10004703  0.06406685 
## 
## $`35`
## (Intercept)    x$weight 
## -0.04352163  0.05673954 
## 
## $`44`
## (Intercept)    x$weight 
##  -6.5272502   0.1520788 
## 
## $`45`
## (Intercept)    x$weight 
##  -4.4783071   0.1287385 
## 
## $`43`
## (Intercept)    x$weight 
##  -4.6866141   0.1091139 
## 
## $`41`
## (Intercept)    x$weight 
##  -4.6130920   0.1209326 
## 
## $`47`
## (Intercept)    x$weight 
##  -4.2201111   0.1183331 
## 
## $`49`
## (Intercept)    x$weight 
##   -3.194997    0.102444 
## 
## $`46`
## (Intercept)    x$weight 
##  -2.6976976   0.1015366 
## 
## $`50`
## (Intercept)    x$weight 
## -1.90964359  0.08695804 
## 
## $`42`
## (Intercept)    x$weight 
## -1.46234743  0.08303419 
## 
## $`48`
## (Intercept)    x$weight 
##  -0.1223961   0.0700152