exercise1

dta <- read.table("C:/Users/user/Desktop/stateAnxiety.txt",header = T)
dta <- na.omit(dta)
head(dta)
str(dta)
## 'data.frame':    50 obs. of  10 variables:
##  $ f1: int  13 26 13 22 18 32 16 18 14 20 ...
##  $ f2: int  17 31 17 24 19 31 16 22 17 19 ...
##  $ f3: int  18 33 24 26 19 30 21 25 23 23 ...
##  $ f4: int  20 38 29 27 22 31 27 29 21 25 ...
##  $ f5: int  24 42 32 29 30 32 30 35 25 28 ...
##  $ m1: int  6 4 17 19 12 11 14 9 12 11 ...
##  $ m2: int  14 11 25 22 21 16 23 18 16 13 ...
##  $ m3: int  22 14 26 26 21 20 26 20 23 17 ...
##  $ m4: int  20 12 29 30 23 19 29 20 26 14 ...
##  $ m5: int  24 23 38 34 24 22 33 24 32 20 ...
library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
library(tidyverse)
## -- Attaching packages -------------------------------------- tidyverse 1.3.0 --
## √ tibble  2.1.3     √ stringr 1.4.0
## √ readr   1.3.1     √ forcats 0.5.0
## √ purrr   0.3.3
## -- Conflicts ----------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
dta1 <- dta %>% gather(key=gender_week,value=score) %>% separate(gender_week,c("gender", "week"), sep = 1) %>% mutate(n_week=6-as.numeric(week),id=paste0(gender,rep((1:50),10)))%>%as.data.frame() 

dta2 <- dta1 %>% group_by(n_week,gender)%>% summarize(score_m=mean(score, na.rm=T), score_se=sd(score, na.rm=T)/sqrt(n()), score_lb=score_m-1.96*score_se, score_ub=score_m+1.96*score_se)


p1 <- qplot(n_week,score_m, data=dta2, geom="line", color=gender, group=gender) +
    geom_point() +
    geom_errorbar(aes(ymin = score_m - score_se, ymax = score_m + score_se), width=.1) +
    xlab('Week before exam') + ylab('Anxiety score')+  
    
  theme_bw()

p2 <- qplot(n_week,score, data=dta1, geom="line", color=id, group=id) +
    geom_point() +
    scale_x_continuous(name="Week before exam",) +
  scale_y_continuous(name="Anxiety score" ) +
    facet_grid(. ~ gender) +
    theme_bw()

p1

p2

From plot1, there are gender difference in state anxiety. From plot2, there is no obvious individual differences in state anxiety.

exercise3

dta <- read.table("C:/Users/user/Desktop/coping.txt",header = T)
dta <- na.omit(dta)
head(dta)
str(dta)
## 'data.frame':    84 obs. of  10 variables:
##  $ annoy    : int  4 4 2 4 4 4 3 3 3 4 ...
##  $ sad      : int  2 4 2 3 2 3 2 1 1 4 ...
##  $ afraid   : int  2 4 2 4 1 1 2 1 1 2 ...
##  $ angry    : int  2 2 2 4 1 4 2 2 2 1 ...
##  $ approach : num  1 4 2.67 4 1 2.33 2 1.33 1 1.67 ...
##  $ avoid    : num  2 3 3 1.5 2.75 2.5 1 4 1 4 ...
##  $ support  : num  1 1.25 1 3.25 1.25 1 1.5 2.75 1.33 3.5 ...
##  $ agressive: num  2.5 1.5 2.33 1 1.5 3.67 1 2 1.67 2.5 ...
##  $ situation: Factor w/ 6 levels "Bully","Fail",..: 2 4 5 1 6 3 2 4 5 1 ...
##  $ sbj      : Factor w/ 14 levels "S135","S137",..: 6 6 6 6 6 6 4 4 4 4 ...
dta1 <- dta %>% gather(key="emotions", value="score", 1:8)
head(dta1)
library(ggplot2)
qplot(score, data=dta1, geom="density", facets=situation~emotions)

library(ggcorrplot)
ggcorrplot(cor(dta[1:8]), hc.order=T, type="lower", lab=T,colors = c("#6D9EC1", "white", "#E46726"))

m1 <- dta1%>% group_by(situation,emotions)%>% summarize(mean=mean(score),na.rm=T)
m2 <- dta1%>%group_by(emotions)%>% summarize(t_mean=mean(score),na.rm=T)

dta2 <- merge(m1,m2) %>% mutate(nscore=t_mean-mean)

p2 <- ggplot(dta2) + 
  aes(x=nscore, 
      y=situation) +
   facet_wrap(.~emotions, nrow = 2) +
  geom_point(size=rel(3)) +
  geom_segment(aes(xend=0,
                   yend=reorder(situation, nscore)))+
   geom_vline(xintercept = 0, color = 'grey55')+
  labs(x="scale scores", 
       y="situation") +

  theme_minimal()
  
p2