Q1

dta1 <- read.table("C:/Users/Cheng_wen_sung/Desktop/dataM_problem1.txt",header = T)
str(dta1)
## 'data.frame':    15 obs. of  6 variables:
##  $ Country: Factor w/ 15 levels "Austria","Canada",..: 2 6 8 1 3 4 5 7 9 10 ...
##  $ X25.34 : int  22 9 22 29 16 28 48 7 8 26 ...
##  $ X35.44 : int  27 19 19 40 25 35 65 8 11 29 ...
##  $ X45.54 : int  31 10 21 52 36 41 84 11 18 36 ...
##  $ X55.64 : int  34 14 31 53 47 49 81 18 20 32 ...
##  $ X65.74 : int  24 27 49 69 56 52 107 27 28 28 ...
colnames(dta1) <- c("Country","25_34","35_44","45_54","55_64","65_74")

dta1L <- gather(dta1,age,counts,2:6)
str(dta1L)
## 'data.frame':    75 obs. of  3 variables:
##  $ Country: Factor w/ 15 levels "Austria","Canada",..: 2 6 8 1 3 4 5 7 9 10 ...
##  $ age    : chr  "25_34" "25_34" "25_34" "25_34" ...
##  $ counts : int  22 9 22 29 16 28 48 7 8 26 ...
dta1L$age <- as.factor(dta1L$age)

##boxplot
boxplot(counts~age,data = dta1L,varwidth = TRUE,xlab = "Age",
        ylab = "Deaths from male suicides")

#It seems the deaths from male suicides grows slightly when they aging.

Q2

#
# case study - low level graphics - how to add a mean curve
#

#create grade level
edu <- rep(c("I","II","III"),4)

#create subject ID
sbj <- paste("S", 1:4, sep="")
sbj <- rep(sbj, rep(3,4))

#give scores
score <- c(19,15,19,18,11,15,21,16,19,21,8,13)

#create a data frame including all above
dta <- data.frame(sbj=sbj, edu=edu, score=score)

#caculate the group means by grade level
meandta <- aggregate(formula =score ~ edu, data = dta, FUN = mean)
meandta <- data.frame(sbj='mean',meandta)

#combind original data frame and group means
dta <- rbind(dta, meandta)


#plot a null scale
plot(dta$score ~ as.numeric(dta$edu), type="n", axes=F,
     xlim=c(0, 4), ylim=c(min(dta$score)-1, max(dta$score+1)),
     xlab="Education level", ylab="Test score" )

#add grid
grid()

#draw data points
points(as.numeric(dta$edu), dta$score)

#add individual and mean curves
lines(as.numeric(dta$edu[dta$sbj=="S1"]), dta$score[dta$sbj=="S1"],col="cyan")
lines(as.numeric(dta$edu[dta$sbj=="S2"]), dta$score[dta$sbj=="S2"],col="cyan2")
lines(as.numeric(dta$edu[dta$sbj=="S3"]), dta$score[dta$sbj=="S3"],col="cyan3")
lines(as.numeric(dta$edu[dta$sbj=="S4"]), dta$score[dta$sbj=="S4"],col="cyan4")
lines(as.numeric(dta$edu[dta$sbj=="mean"]), dta$score[dta$sbj=="mean"],
      col="blue", lwd=2)

#add legends
legend("bottomright", c("S1","S2","S3", "S4", "Mean"), 
       lty=1, lwd=c(rep(1,4),2), bty="n", cex=0.6,
       col=c("cyan","cyan2", "cyan3", "cyan4", "blue"))

#add the grade level text
text(1,7, "I")
text(2,7, "II")
text(3,7, "III")

#give the y axis value
axis(2, min(dta$score):max(dta$score))

###

Q3

dta3 <- read.table("http://titan.ccunix.ccu.edu.tw/~psycfs/dataM/Data/family.txt",header = T)
str(dta3)
## 'data.frame':    12 obs. of  2 variables:
##  $ family    : int  1 1 1 2 2 2 2 3 3 4 ...
##  $ liberalism: int  25 29 34 18 21 21 26 31 35 30 ...
with(dta3,plot(liberalism,family,type = "n",ylim = c(0,5),xlab = "Rating of Liberalism",
               ylab = "Family",main = "Family Resemblance"))
points(dta3$liberalism,jitter(dta3$family),pch = 16)
abline(v = mean(dta3$liberalism),lty = 2)

#family = 1
segments(x0 = mean(dta3$liberalism[dta3$family==1]),y0 = 1,x1 = mean(dta3$liberalism),
         y1 = 1,lty = 2)
segments(x0 = mean(dta3$liberalism[dta3$family==1]),y0 = 0.9,x1 = mean(dta3$liberalism[dta3$family==1]),
         y1 = 1.1,lty = 2)

#family = 2
segments(x0 = mean(dta3$liberalism[dta3$family==2]),y0 = 2,x1 = mean(dta3$liberalism),
         y1 = 2,lty = 2)
segments(x0 = mean(dta3$liberalism[dta3$family==2]),y0 = 1.9,x1 = mean(dta3$liberalism[dta3$family==2]),
         y1 = 2.1,lty = 2)

#family = 3
segments(x0 = mean(dta3$liberalism[dta3$family==3]),y0 = 3,x1 = mean(dta3$liberalism),
         y1 = 3,lty = 2)
segments(x0 = mean(dta3$liberalism[dta3$family==3]),y0 = 2.9,x1 = mean(dta3$liberalism[dta3$family==3]),
         y1 = 3.1,lty = 2)

#family = 4
segments(x0 = mean(dta3$liberalism[dta3$family==4]),y0 = 4,x1 = mean(dta3$liberalism),
         y1 = 4,lty = 2)
segments(x0 = mean(dta3$liberalism[dta3$family==4]),y0 = 3.9,x1 = mean(dta3$liberalism[dta3$family==4]),
         y1 = 4.1,lty = 2)

Q4

dta4 <- read.table("http://titan.ccunix.ccu.edu.tw/~psycfs/dataM/Data/cigarettes.txt",header = T)
str(dta4)
## 'data.frame':    11 obs. of  3 variables:
##  $ Country    : Factor w/ 11 levels "Australia","Canada",..: 1 2 3 4 10 5 6 7 8 9 ...
##  $ consumption: int  480 500 380 1100 1100 230 490 250 300 510 ...
##  $ death      : int  180 150 170 350 460 60 240 90 110 250 ...
with(dta4,plot(consumption,death,xlab = "Consumption",ylab = "Death rate",type = "n"))
r1 <- lm(death~consumption,data = dta4)
abline(a = coef(r1)[1],b = coef(r1)[2],lty = 2)
text(x = dta4$consumption,y = dta4$death,labels = dta4$Country)

Q5

dta5 <- read.table("http://titan.ccunix.ccu.edu.tw/~psycfs/dataM/Data/coping.txt",header = T)
str(dta5)
## '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 ...
dta5L <- gather(dta5,emotion,score,1:8)
dta5L$emotion <- as.factor(dta5L$emotion)
str(dta5L)
## 'data.frame':    672 obs. of  4 variables:
##  $ 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 ...
##  $ emotion  : Factor w/ 8 levels "afraid","agressive",..: 4 4 4 4 4 4 4 4 4 4 ...
##  $ score    : num  4 4 2 4 4 4 3 3 3 4 ...
dta5_mean <- dta5L%>%group_by(situation,emotion)%>%summarize(mean = mean(score))
with(dta5_mean,plot(as.numeric(situation),mean,type  = "n",xlab = "Situation",ylab = "Score"))

#caculate the group means by grade level
meandta <- aggregate(formula =mean ~ situation, data = dta5_mean, FUN = mean)
meandta <- data.frame(emotion='mean',meandta)
meandta <- meandta[,c(2,1,3)]
meandta$situation <- as.character(meandta$situation)
meandta$emotion <- as.character(meandta$emotion)

#rbind
dta5_mean$emotion <- as.character(dta5_mean$emotion)
dta5_mean[49:54,] <- meandta
dta5_mean$emotion <- as.factor(dta5_mean$emotion)
str(dta5_mean)
## Classes 'grouped_df', 'tbl_df', 'tbl' and 'data.frame':  54 obs. of  3 variables:
##  $ situation: Factor w/ 6 levels "Bully","Fail",..: 1 1 1 1 1 1 1 1 2 2 ...
##  $ emotion  : Factor w/ 9 levels "afraid","agressive",..: 1 2 3 4 5 6 8 9 1 2 ...
##  $ mean     : num  1.57 1.79 2.14 3 1.9 ...
##  - attr(*, "vars")=List of 1
##   ..$ : symbol situation
##  - attr(*, "drop")= logi TRUE
#add points
points(as.numeric(dta5_mean$situation[dta5_mean$emotion == "afraid"]),dta5_mean$mean[dta5_mean$emotion == "afraid"],pch = 16,col = "grey10")
points(as.numeric(dta5_mean$situation[dta5_mean$emotion == "agressive"]),dta5_mean$mean[dta5_mean$emotion == "agressive"],pch = 16,col = "grey20")
points(as.numeric(dta5_mean$situation[dta5_mean$emotion == "angry"]),dta5_mean$mean[dta5_mean$emotion == "angry"],pch = 16,col = "grey30")
points(as.numeric(dta5_mean$situation[dta5_mean$emotion == "annoy"]),dta5_mean$mean[dta5_mean$emotion == "annoy"],pch = 16,col = "grey40")
points(as.numeric(dta5_mean$situation[dta5_mean$emotion == "approach"]),dta5_mean$mean[dta5_mean$emotion == "approach"],pch = 16,col = "grey50")
points(as.numeric(dta5_mean$situation[dta5_mean$emotion == "avoid"]),dta5_mean$mean[dta5_mean$emotion == "avoid"],pch = 16,col = "grey60")
points(as.numeric(dta5_mean$situation[dta5_mean$emotion == "sad"]),dta5_mean$mean[dta5_mean$emotion == "sad"],pch = 16,col = "grey70")
points(as.numeric(dta5_mean$situation[dta5_mean$emotion == "support"]),dta5_mean$mean[dta5_mean$emotion == "support"],pch = 16,col = "grey80")
points(as.numeric(dta5_mean$situation[dta5_mean$emotion == "mean"]),dta5_mean$mean[dta5_mean$emotion == "mean"],pch = 16,col = "red")
#add each emotion curve
lines(as.numeric(dta5_mean$situation[dta5_mean$emotion == "afraid"]), dta5_mean$mean[dta5_mean$emotion == "afraid"],col="grey10")
lines(as.numeric(dta5_mean$situation[dta5_mean$emotion == "agressive"]), dta5_mean$mean[dta5_mean$emotion == "agressive"],col="grey20")
lines(as.numeric(dta5_mean$situation[dta5_mean$emotion == "angry"]), dta5_mean$mean[dta5_mean$emotion == "angry"],col="grey30")
lines(as.numeric(dta5_mean$situation[dta5_mean$emotion == "annoy"]), dta5_mean$mean[dta5_mean$emotion == "annoy"],col="grey40")
lines(as.numeric(dta5_mean$situation[dta5_mean$emotion == "approach"]), dta5_mean$mean[dta5_mean$emotion == "approach"],col="grey50")
lines(as.numeric(dta5_mean$situation[dta5_mean$emotion == "avoid"]), dta5_mean$mean[dta5_mean$emotion == "avoid"],col="grey60")
lines(as.numeric(dta5_mean$situation[dta5_mean$emotion == "sad"]), dta5_mean$mean[dta5_mean$emotion == "sad"],col="grey70")
lines(as.numeric(dta5_mean$situation[dta5_mean$emotion == "support"]), dta5_mean$mean[dta5_mean$emotion == "support"],col="grey80")
lines(as.numeric(dta5_mean$situation[dta5_mean$emotion == "mean"]), dta5_mean$mean[dta5_mean$emotion == "mean"],col="red")
#add legends

legend("topright",legend =  c("Afraid","Agreesive","Angry", "Annoy", "Approach","Avoid","Sad","Support","Mean"), 
       lty=1, lwd=c(rep(1,4),2), bty="n", cex=0.6,
       col=c("grey10","grey20", "grey30", "grey40", "grey50","grey60","grey70","grey80","red"))