inclass 3

The data set concerns student evaluation of instructor’s beauty and teaching quality for several courses at the University of Texas. The teaching evaluations were done at the end of the semester, and the beauty judgments were made later, by six students who had not attended the classes and were not aware of the course evaluations. Use the lattice package to produce the plot above. Hint: Use ‘reorder’ after obtaining regression coefficients to rearrange conditioning panels.

knitr::include_graphics("beautyCourseEval.png")

#匯入資料
dta=read.table("d:/beautyCourseEval.txt",header=T) 
head(dta)
  eval     beauty sex age minority tenure courseID
1  4.3  0.2015666   1  36        1      0        3
2  4.5 -0.8260813   0  59        0      1        0
3  3.7 -0.6603327   0  51        0      1        4
4  4.3 -0.7663125   1  40        0      1        2
5  4.4  1.4214450   1  31        0      0        0
6  4.2  0.5002196   0  62        0      1        0
#463 obs. of  7 variables
str(dta)
'data.frame':   463 obs. of  7 variables:
 $ eval    : num  4.3 4.5 3.7 4.3 4.4 4.2 4 3.4 4.5 3.9 ...
 $ beauty  : num  0.202 -0.826 -0.66 -0.766 1.421 ...
 $ sex     : int  1 0 0 1 1 0 1 1 1 0 ...
 $ age     : int  36 59 51 40 31 62 33 51 33 47 ...
 $ minority: int  1 0 0 0 0 0 0 0 0 0 ...
 $ tenure  : int  0 1 1 1 0 1 0 1 0 0 ...
 $ courseID: int  3 0 4 2 0 0 4 0 0 4 ...
#無遺漏值
table(is.na(dta))

FALSE 
 3241 
library(lattice)
library(tools)
#表示要儲存成png的格式
png("score.png", width=1500, height=1500)
#在~左邊的,是y的值;在~右邊的,是x的值

xyplot(eval ~ beauty | factor(courseID), 
       data=dta,  
       main = list(
         label="Relationship between Beauty score and Evaluation score", 
         cex=4),
       xlab=list(
         label="Beauty judgement score", 
         cex=2), 
       ylab=list(
         label="Average course evaluation score",
         cex=2),
       scales = list(cex = 2),
       par.strip.text = list(cex = 2),
       layout=c(5, 7),
#畫迴歸線
#function的寫法,會用大括號包起來,裡面表示要進行的動作:
#在這個panel function裡面,我們進行了4個動作
panel=function(x,y){  
  panel.fill(col="white")        # 1.改變背景顏色(gray)
  panel.xyplot(x, y)             # 2.繪製x-y的散布圖
  panel.grid(h=-1, v=1)          # 3.畫出輔助線
  panel.lmline(x, y, col="blue") # 4.畫出線性迴歸的趨勢線

}
)

#最後要關掉輸出圖片裝置 
#把檔案存成.png檔,看起來比RPubs清楚
dev.off()  
png 
  2 
knitr::include_graphics("score.png")

待釐清: 1.輔助線無法在每個格子上 2.圖片排序的order無法克服