EX1

require(nlme)
## Loading required package: nlme
## Warning: package 'nlme' was built under R version 3.4.4
require(lattice)
## Loading required package: lattice
## Warning: package 'lattice' was built under R version 3.4.4
dta1 <- Oxboys
str(dta1)
## Classes 'nfnGroupedData', 'nfGroupedData', 'groupedData' and 'data.frame':   234 obs. of  4 variables:
##  $ Subject : Ord.factor w/ 26 levels "10"<"26"<"25"<..: 13 13 13 13 13 13 13 13 13 5 ...
##  $ age     : num  -1 -0.7479 -0.463 -0.1643 -0.0027 ...
##  $ height  : num  140 143 145 147 148 ...
##  $ Occasion: Ord.factor w/ 9 levels "1"<"2"<"3"<"4"<..: 1 2 3 4 5 6 7 8 9 1 ...
##  - attr(*, "formula")=Class 'formula'  language height ~ age | Subject
##   .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
##  - attr(*, "labels")=List of 2
##   ..$ y: chr "Height"
##   ..$ x: chr "Centered age"
##  - attr(*, "units")=List of 1
##   ..$ y: chr "(cm)"
##  - attr(*, "FUN")=function (x)  
##   ..- attr(*, "source")= chr "function (x) max(x, na.rm = TRUE)"
##  - attr(*, "order.groups")= logi TRUE
head(dta1)
## Grouped Data: height ~ age | Subject
##   Subject     age height Occasion
## 1       1 -1.0000  140.5        1
## 2       1 -0.7479  143.4        2
## 3       1 -0.4630  144.8        3
## 4       1 -0.1643  147.1        4
## 5       1 -0.0027  147.7        5
## 6       1  0.2466  150.2        6
xyplot(height ~ age  | Subject, data =dta1 , type = c("r", "g", "p"),
                index.cond = function(x, y) coef(lm(y ~ x))[1])

EX2

require(ggplot2)
## Loading required package: ggplot2
dta2<-read.csv("income_tw.csv", h=T)
str(dta2)
## 'data.frame':    41 obs. of  2 variables:
##  $ Income: Factor w/ 41 levels "1,000,000 to 1,069,999",..: 7 8 9 11 12 13 14 15 16 17 ...
##  $ Count : int  807160 301650 313992 329290 369583 452671 495387 517779 557786 584497 ...
head(dta2)
##               Income  Count
## 1  160,000 and under 807160
## 2 160,000 to 179,999 301650
## 3 180,000 to 199,999 313992
## 4 200,000 to 219,999 329290
## 5 220,000 to 239,999 369583
## 6 240,000 to 259,999 452671
dta2$Income<-factor(dta2$Income,levels =dta2$Income[order(dta2$Count)] )
ggplot(dta2, aes(Count/10000, Income)) +
  geom_point() +
  geom_segment(aes(x = 0, xend = Count/10000, yend=Income)) +
  labs(x= "Number of persons(x10,000)", y = "Income", title = "Distribution of personal disposable income in Taiwan in 2015")

EX3

require(tidyr)
## Loading required package: tidyr
dta3<-read.csv("brainsize.txt", h=T, sep = " ")
str(dta3)
## 'data.frame':    40 obs. of  8 variables:
##  $ Sbj     : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ Gender  : Factor w/ 2 levels "Female","Male": 1 2 2 2 1 1 1 1 2 2 ...
##  $ FSIQ    : int  133 140 139 133 137 99 138 92 89 133 ...
##  $ VIQ     : int  132 150 123 129 132 90 136 90 93 114 ...
##  $ PIQ     : int  124 124 150 128 134 110 131 98 84 147 ...
##  $ Weight  : int  118 NA 143 172 147 146 138 175 134 172 ...
##  $ Height  : num  64.5 72.5 73.3 68.8 65 69 64.5 66 66.3 68.8 ...
##  $ MRICount: int  816932 1001121 1038437 965353 951545 928799 991305 854258 904858 955466 ...
head(dta3)
##   Sbj Gender FSIQ VIQ PIQ Weight Height MRICount
## 1   1 Female  133 132 124    118   64.5   816932
## 2   2   Male  140 150 124     NA   72.5  1001121
## 3   3   Male  139 123 150    143   73.3  1038437
## 4   4   Male  133 129 128    172   68.8   965353
## 5   5 Female  137 132 134    147   65.0   951545
## 6   6 Female   99  90 110    146   69.0   928799

EX3-1:Are there gender differences in the three IQ scores?

dta3 %>% gather(Type, IQ, 3:5) %>% 
  ggplot(aes(Type, IQ, color = Gender))+
  stat_summary(fun.data = mean_se, geom = "pointrange", position = position_dodge(.2))+
  theme(legend.position = "top")

VIQ和FSIQ可能有性別差異,PIQ可能沒有。

EX3-2:Is the relationship between height and weight gender dependent?

ggplot(dta3, aes(Weight, Height, color = Gender)) +
  geom_point(na.rm = T) +
  stat_smooth(aes(color = Gender), method = "lm", se = F,na.rm = T) +
  theme_bw()

男女身高與體重之斜率相近

EX3-3:Is the relationship between height and weight gender dependent?

read.table("brainsize.txt", header = T) %>%
  gather(IQtype, IQvalue, 3:5) %>% 
  ggplot(aes(IQvalue, MRICount, color = Gender)) +
  geom_point() +
  stat_smooth(method = "lm", se = F) +
  facet_grid(. ~ IQtype) +
  labs(y = "Brain Size")

IQ分數和腦容量有性別差異