EX1

pacman::p_load(mlmRev,HSAUR3,knitr,kableExtra,readr,dplyr,ggplot2,tidyr,car,magrittr,tibble,data.table,stringr)
head(dta1 <- read.csv(paste0("http://",IDPW,"140.116.183.121/~sheu/dataM/Data/nlsy86long.csv"), header = T))
##     id    sex     race time grade year month      math      read
## 1 2390 Female Majority    1     0    6    67 14.285714 19.047619
## 2 2560 Female Majority    1     0    6    66 20.238095 21.428571
## 3 3740 Female Majority    1     0    6    67 17.857143 21.428571
## 4 4020   Male Majority    1     0    5    60  7.142857  7.142857
## 5 6350   Male Majority    1     1    7    78 29.761905 30.952381
## 6 7030   Male Majority    1     0    5    62 14.285714 17.857143
head(gather(dta1,"test_var", "test_score", 8:9))
##     id    sex     race time grade year month test_var test_score
## 1 2390 Female Majority    1     0    6    67     math  14.285714
## 2 2560 Female Majority    1     0    6    66     math  20.238095
## 3 3740 Female Majority    1     0    6    67     math  17.857143
## 4 4020   Male Majority    1     0    5    60     math   7.142857
## 5 6350   Male Majority    1     1    7    78     math  29.761905
## 6 7030   Male Majority    1     0    5    62     math  14.285714

EX2

參考老師簡報第19頁程式碼

head(dta2 <- Vocab)
##          year    sex education vocabulary
## 20040001 2004 Female         9          3
## 20040002 2004 Female        14          6
## 20040003 2004   Male        14          9
## 20040005 2004 Female        17          8
## 20040008 2004   Male        14          1
## 20040010 2004   Male        14          7
str(dta2)
## 'data.frame':    21638 obs. of  4 variables:
##  $ year      : int  2004 2004 2004 2004 2004 2004 2004 2004 2004 2004 ...
##  $ sex       : Factor w/ 2 levels "Female","Male": 1 1 2 1 2 2 1 2 2 1 ...
##  $ education : int  9 14 14 17 14 14 12 10 11 9 ...
##  $ vocabulary: int  3 6 9 8 1 7 6 6 5 1 ...
dta2 %>%
  rename(gender = sex) %>%
  group_by(year, gender) %>%
  summarize(edu_m= mean(education, na.rm = T),
            edu_se= sd(education, na.rm = T)/sqrt(n())) %>%
  ggplot(data = ., aes(x = year, y = edu_m, color = gender)) +
  geom_point() +
  geom_line(aes(group = gender)) +
  geom_errorbar(aes(ymin = edu_m - 2*edu_se, ymax = edu_m + 2*edu_se), width = .1) + 
  labs(x = "Year", y = "Average Education score") +
  theme_bw() 

兩性的教育分數隨年份增加,且女性有追上男性的趨勢

dta2 %>%
  rename(gender = sex) %>%
  group_by(year, gender) %>%
  summarize(voc_m= mean(vocabulary, na.rm = T),
            voc_se= sd(vocabulary, na.rm = T)/sqrt(n())) %>%
  ggplot(data = ., aes(x = year, y = voc_m, color = gender)) +
  geom_point() +
  geom_line(aes(group = gender)) +
  geom_errorbar(aes(ymin = voc_m - 2*voc_se, ymax = voc_m + 2*voc_se), width = .1) + 
  labs(x = "Year", y = "Average Vocabulart score") +
  theme_bw() 

女性的字彙分數在近廿年有超越男性的趨勢

EX3

head(dta3 <- read.table(paste0("http://",IDPW,"140.116.183.121/~sheu/dataM/Data/probeL.txt"), header = T))
##    ID Response_Time Position
## 1 S01            51        1
## 2 S01            36        2
## 3 S01            50        3
## 4 S01            35        4
## 5 S01            42        5
## 6 S02            27        1
dta3 %>% 
  mutate(Position = paste("Pos", Position)) %>% 
  spread(Position, Response_Time)
##     ID Pos 1 Pos 2 Pos 3 Pos 4 Pos 5
## 1  S01    51    36    50    35    42
## 2  S02    27    20    26    17    27
## 3  S03    37    22    41    37    30
## 4  S04    42    36    32    34    27
## 5  S05    27    18    33    14    29
## 6  S06    43    32    43    35    40
## 7  S07    41    22    36    25    38
## 8  S08    38    21    31    20    16
## 9  S09    36    23    27    25    28
## 10 S10    26    31    31    32    36
## 11 S11    29    20    25    26    25

http://140.116.183.121/~sheu/dataM/Rdw/data/nobel_countries.txt

EX4

head(dta4_1 <- read.table(paste0("http://",IDPW,"140.116.183.121/~sheu/dataM/Rdw/data/nobel_countries.txt"), header = T))
##   Country Year
## 1  France 2014
## 2      UK 1950
## 3      UK 2017
## 4      US 2016
## 5  Canada 2013
## 6   China 2012
head(dta4_2 <- read.table(paste0("http://",IDPW,"140.116.183.121/~sheu/dataM/Rdw/data/nobel_winners.txt"), header = T))
##                Name Gender Year
## 1   Patrick Modiano   Male 2014
## 2 Bertrand  Russell   Male 1950
## 3    Kazuo Ishiguro   Male 2017
## 4        Bob  Dylan   Male 2016
## 5      Alice  Munro Female 2013
## 6            Mo Yan   Male 2012
#兩組資料中,以year比對,將match的留下
inner_join(dta4_1, dta4_2)
## Joining, by = "Year"
##   Country Year              Name Gender
## 1  France 2014   Patrick Modiano   Male
## 2      UK 1950 Bertrand  Russell   Male
## 3      UK 2017    Kazuo Ishiguro   Male
## 4      US 2016        Bob  Dylan   Male
## 5  Canada 2013      Alice  Munro Female
## 6   China 2012            Mo Yan   Male
#
#
#兩組資料中,比對後,留下與否以前者為主要參考依據
semi_join(dta4_1, dta4_2)
## Joining, by = "Year"
##   Country Year
## 1  France 2014
## 2      UK 1950
## 3      UK 2017
## 4      US 2016
## 5  Canada 2013
## 6   China 2012
#
#
#兩組資料中,前者都留下,前後者共有的也留下
left_join(dta4_1, dta4_2)
## Joining, by = "Year"
##   Country Year              Name Gender
## 1  France 2014   Patrick Modiano   Male
## 2      UK 1950 Bertrand  Russell   Male
## 3      UK 2017    Kazuo Ishiguro   Male
## 4      US 2016        Bob  Dylan   Male
## 5  Canada 2013      Alice  Munro Female
## 6   China 2012            Mo Yan   Male
## 7  Russia 2015              <NA>   <NA>
## 8  Sweden 2011              <NA>   <NA>
#
#
#兩組資料中,XOR互斥,在前者中沒有出現在後者的留下
anti_join(dta4_1, dta4_2)
## Joining, by = "Year"
##   Country Year
## 1  Russia 2015
## 2  Sweden 2011
#
#
#兩組資料中,全部都留下
full_join(dta4_1, dta4_2)
## Joining, by = "Year"
##   Country Year              Name Gender
## 1  France 2014   Patrick Modiano   Male
## 2      UK 1950 Bertrand  Russell   Male
## 3      UK 2017    Kazuo Ishiguro   Male
## 4      US 2016        Bob  Dylan   Male
## 5  Canada 2013      Alice  Munro Female
## 6   China 2012            Mo Yan   Male
## 7  Russia 2015              <NA>   <NA>
## 8  Sweden 2011              <NA>   <NA>
## 9    <NA> 1938        Pearl Buck Female