Homework 5/07

尤怡方

2018-05-12

library(dplyr)
library(ggplot2)
library(tidyr)

2

#read the data
dt2 <- read.table("C:/Users/user/Dropbox/1062-Data_manage/0226/Data/nlsy86long.csv", header=T,sep=",")

看種族、性別在數學與閱讀分數上的分布

無論性別跟種族為何,數學成績跟閱讀成績有正相關

dt2 %>% ggplot()+ geom_smooth(mapping=aes(math,read,color=sex))  +theme_light() + facet_grid(.~race)

看年齡與成績在性別上的差異

隨著年齡上升,數學與閱讀成績都跟著上升。在6-8歲的時候有性別差異,隨著年齡上升則漸無性別差異。

dt2 %>% gather(subject,score,8:9) %>%
  ggplot(.,aes(year,score,color=sex)) +
  stat_summary(fun.data = mean_se , geom = "pointrange",
               position = position_dodge(0.3)) + 
  facet_grid(.~subject)+
  theme_light()

3

右邊的圖比較接近資料的解釋,21歲以後喝酒是合法的,死亡率比21歲以前高。 (不知道怎麼畫像網頁上那樣的圖,但盡量畫了接近的回歸線)

#read the data
dt3 <- read.table("C:/Users/user/Dropbox/1062-Data_manage/0507/alcohol_age.csv", header=T,sep=",")

#create variable: whether age is above 21
dt3 %>% mutate(more21=ifelse(Age>=21,"yes","no")) %>%
  ggplot(.,aes(Age,Alcohol,color=more21)) + 
  geom_point(size=3) +  
  theme_bw() + 
  geom_smooth(method = "lm") + 
  labs(x="Age (year)", y="Mortality rate from alcohol abuse (per 100,000)") 

4 Everitt

#read the data
dt4 <- read.table("C:/Users/user/Dropbox/1062-Data_manage/0507/inclass/data4.txt", header=T)

#rename variable
colnames(dt4)<- c("country","25-34","35-44","45-54","55-64","65-74")

knitr::kable(head(dt4))
country 25-34 35-44 45-54 55-64 65-74
Canada 22 27 31 34 24
Israel 9 19 10 14 27
Japan 22 19 21 31 49
Austria 29 40 52 53 69
France 16 25 36 47 56
Germany 28 35 41 49 52
看起來東歐 國家的男 性自殺率 比較高, 國家越先 進男性自殺率越低
dt4 %>% janitor::adorn_totals("col") %>%
  gather(Age,cases,2:6) %>%
  ggplot(.,aes(reorder(country,-cases),cases)) + 
  geom_boxplot() + theme_bw() +
  labs(x="Country",y="Deaths per 100,000 from male suicides")

5

前四個變項是測情緒面向,評分方式是1:not at all到4:very much。 第五到八個變項是策略採取的部分,評分方式是1:almost never到4:almost always

#read the data
dt5 <- read.table("C:/Users/user/Dropbox/1062-Data_manage/0507/exercise.txt", header=T)

knitr::kable(head(dt5))
annoy sad afraid angry approach avoid support agressive situation sbj
4 2 2 2 1.00 2.00 1.00 2.50 Fail S2
4 4 4 2 4.00 3.00 1.25 1.50 NoPart S2
2 2 2 2 2.67 3.00 1.00 2.33 TeacNo S2
4 3 4 4 4.00 1.50 3.25 1.00 Bully S2
4 2 1 1 1.00 2.75 1.25 1.50 Work S2
4 3 1 4 2.33 2.50 1.00 3.67 MomNo S2
####看情 境跟情 緒的關係
在六種不 同的情 境上,整體 來說不常 出現害怕的情 況,最常 感到annoy, 不過annoy的分 數變化也很大
dt5 %>% gather(emotion,e_score,1:4) %>% 
  ggplot(.,aes(situation,e_score,color=emotion)) + 
  stat_summary(fun.data = mean_se,position = position_dodge(0.3))+
  theme_bw()+
  labs(x="Situation",y="Score")

看情境跟採取策略的關係

整體來說比較少採用aggressive的方式,在“沒有全勤(Fail)”跟“作業太多(Word)”這兩種情境上,學生會比較想辦法採取一些策略來解決問題。但如果是被霸凌、被媽媽或老師禁足、被其他同學排擠,等有關社交的情境,學生會比較常採取逃避的策略。

dt5 %>% gather(coping,score,5:8) %>% 
  ggplot(.,aes(situation,score,color=coping)) + 
  stat_summary(fun.data = mean_se,position = position_dodge(0.3))+
  theme_bw()+
  labs(x="Situation",y="Score")

6

7

#read the data
dt7 <- read.table("C:/Users/user/Dropbox/1062-Data_manage/0507/CourseEval.txt", header=T)

dt7 %>% ggplot(.,aes(beauty,eval)) + geom_point() +
  facet_wrap(~courseID,ncol=6) + theme_bw() +
  labs(x="Beauty judgment score",y="Average course evaluation score")