Convert the R script file, ch02.R , to an ouput in R Notebook. You might need to do this: File -> Reopen with Encoding -> BIG5 可以解決原本打開很多亂碼的問題

input data

#讀檔案,資料來自於 TIMSS 2011 年
dta <- read.table(file = "TIMSSmath.txt", header = TRUE)

#看看前六筆
head(dta)
##   國家 性別 父母教育     數學     化學     地科     生物     物理 電腦 書桌 書
## 1 台灣 女生     高中 729.3937 624.7918 607.8187 650.6866 618.3397   有   無 有
## 2 台灣 女生     高中 776.1965 661.7790 665.7075 627.9452 616.7004   有   有 有
## 3 台灣 女生 國小以下 718.1735 708.7784 629.3393 617.4507 663.3176   有   有 無
## 4 台灣 女生     初中 607.1847 632.9836 620.3192 592.0527 616.4611   有   無 無
## 5 台灣 女生 大學以上 658.1759 617.8183 614.0907 560.1919 591.0047   有   無 無
## 6 台灣 女生     專科 478.5763 601.2205 591.0417 598.7882 553.9959   有   有 有
##   房間 網路      書量
## 1   無   有  26-100本
## 2   無   有   11-25本
## 3   無   有  10本以下
## 4   無   有 200本以上
## 5   無   有  26-100本
## 6   有   有  26-100本
#看看後六筆
tail(dta)
##       國家 性別 父母教育     數學     化學     地科     生物     物理 電腦 書桌
## 15544 韓國 女生     專科 664.3045 589.9469 600.0643 614.9202 628.0099   有   有
## 15545 韓國 女生 大學以上 728.7410 657.6870 606.8774 616.4779 636.9204   有   有
## 15546 韓國 女生 大學以上 697.2188 553.7950 545.8474 577.7953 524.0949   有   有
## 15547 韓國 女生     高中 451.7339 446.9285 424.8414 468.2117 417.1787   有   有
## 15548 韓國 女生     專科 629.8083 517.1390 563.3859 542.6878 547.8551   有   有
## 15549 韓國 女生 大學以上 595.4523 611.7756 541.5611 651.4343 611.9802   有   有
##       書 房間 網路      書量
## 15544 有   有   有 200本以上
## 15545 有   有   有 200本以上
## 15546 有   無   有 200本以上
## 15547 有   無   有   11-25本
## 15548 有   有   有 200本以上
## 15549 有   有   有  10本以下

read.table 讀文字檔

header = TRUE 第一列讀為變項名稱

summary data

dta <- dta |> mutate(
  國家 = as.factor(國家),
  性別 = as.factor(性別),
  父母教育 = as.factor(父母教育),
  電腦 = as.factor(電腦),
  書桌 = as.factor(書桌),
= as.factor(書),
  房間 = as.factor(房間),
  網路 = as.factor(網路),
  書量 = as.factor(書量),
)
#看看資料基本統計#
summary(dta)
##    國家        性別          父母教育         數學            化學      
##  日本:3420   女生:8053   大學以上:5502   Min.   :136.2   Min.   :227.5  
##  台灣:4535   男生:7496   初中    :1291   1st Qu.:546.0   1st Qu.:509.5  
##  香港:3249               高中    :6375   Median :609.3   Median :565.6  
##  韓國:4345               國小以下: 384   Mean   :603.2   Mean   :564.4  
##                          專科    :1997   3rd Qu.:666.3   3rd Qu.:621.4  
##                                          Max.   :918.1   Max.   :909.7  
##       地科            生物            物理       電腦       書桌      
##  Min.   :172.2   Min.   :196.0   Min.   :151.9   有:15053   有:13960  
##  1st Qu.:507.5   1st Qu.:510.4   1st Qu.:510.8   無:  496   無: 1589  
##  Median :563.1   Median :565.6   Median :568.0                        
##  Mean   :558.8   Mean   :560.3   Mean   :563.9                        
##  3rd Qu.:613.9   3rd Qu.:614.2   3rd Qu.:621.2                        
##  Max.   :848.2   Max.   :829.1   Max.   :852.5                        
##   書        房間       網路              書量     
##  有:14284   有:10778   有:14619   101-200本:2761  
##  無: 1265   無: 4771   無:  930   10本以下 :2054  
##                                   11-25本  :2875  
##                                   200本以上:3367  
##                                   26-100本 :4492  
## 
str(dta)
## 'data.frame':    15549 obs. of  14 variables:
##  $ 國家    : Factor w/ 4 levels "日本","台灣",..: 2 2 2 2 2 2 2 2 2 2 ...
##  $ 性別    : Factor w/ 2 levels "女生","男生": 1 1 1 1 1 1 1 1 1 1 ...
##  $ 父母教育: Factor w/ 5 levels "大學以上","初中",..: 3 3 4 2 1 5 3 1 2 3 ...
##  $ 數學    : num  729 776 718 607 658 ...
##  $ 化學    : num  625 662 709 633 618 ...
##  $ 地科    : num  608 666 629 620 614 ...
##  $ 生物    : num  651 628 617 592 560 ...
##  $ 物理    : num  618 617 663 616 591 ...
##  $ 電腦    : Factor w/ 2 levels "有","無": 1 1 1 1 1 1 1 1 1 1 ...
##  $ 書桌    : Factor w/ 2 levels "有","無": 2 1 1 2 2 1 1 1 1 1 ...
##  $ 書      : Factor w/ 2 levels "有","無": 1 1 2 2 2 1 1 1 1 1 ...
##  $ 房間    : Factor w/ 2 levels "有","無": 2 2 2 2 2 1 2 1 1 1 ...
##  $ 網路    : Factor w/ 2 levels "有","無": 1 1 1 1 1 1 1 1 1 1 ...
##  $ 書量    : Factor w/ 5 levels "101-200本","10本以下",..: 5 3 2 4 5 5 3 5 5 1 ...

summary針對連續會列出min, max, mean, quantile, median。針對類別則為各level count,若沒有設定為factor則會出現長度與屬性.

plot

#載進 lattice,準備畫圖。
pacman::p_load(lattice)

pacman::p_load()is a wrapper for library and require. It checks to see if a package is installed, if not it attempts to install the package from CRAN and/or any other repository in the pacman repository list.

histogram

#看看數學分數的直方圖
histogram(~ 數學, data = dta, xlab = '數學分數', ylab='機率',type = "density")

要做scatter plot前,要先創造只留學科分數的dataset

##連續變項間關係
#把學科分數取出來
dta_scores <- dta[, c('數學', '化學', '地科', '生物', '物理')]

scatter plot

“pch”以點繪製資料,圖形上半部分(“upper.panel”)繪製平滑線(“panel.smooth”),圖形下半部分(“lower.panel”)是空的(“NULL”)

#兩兩變項畫散佈圖
pairs(dta_scores, pch = '.', upper.panel = panel.smooth, lower.panel = NULL, 
      col = 'gray')

大部分都是線性的,只有數學在部分學科有略偏離紅線

Q:type不曉得代表什麼意思?

#利用 lattice 的 splom 指令重畫兩兩變項散佈圖,算是進階版
splom(~ dta_scores, cex = 0.1, pch = '.', axis.text.cex = 0.5, 
      type = c('p', 'r', 'g'))

correlation and test

#數學與物理分數相關
round(cor(dta$數學,dta$物理), 3)
## [1] 0.725
#所有學科分數相關
round(cor(dta_scores), 3)
##       數學  化學  地科  生物  物理
## 數學 1.000 0.723 0.694 0.711 0.725
## 化學 0.723 1.000 0.857 0.868 0.859
## 地科 0.694 0.857 1.000 0.876 0.861
## 生物 0.711 0.868 0.876 1.000 0.897
## 物理 0.725 0.859 0.861 0.897 1.000
#檢定相關是否顯著,也可以看到信賴區間
cor.test( ~ 數學 + 物理, data = dta_scores)
## 
##  Pearson's product-moment correlation
## 
## data:  數學 and 物理
## t = 131.06, df = 15547, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.7169507 0.7318879
## sample estimates:
##       cor 
## 0.7245044

Hmisc套件可以一次檢定所有相關,但須用在矩陣上,因此須先把dataframe,轉為matrix.

#載進 Hmist,一次檢定所有相關
pacman::p_load(Hmisc)
rcorr(as.matrix(dta_scores), type="pearson")
##      數學 化學 地科 生物 物理
## 數學 1.00 0.72 0.69 0.71 0.72
## 化學 0.72 1.00 0.86 0.87 0.86
## 地科 0.69 0.86 1.00 0.88 0.86
## 生物 0.71 0.87 0.88 1.00 0.90
## 物理 0.72 0.86 0.86 0.90 1.00
## 
## n= 15549 
## 
## 
## P
##      數學 化學 地科 生物 物理
## 數學       0    0    0    0  
## 化學  0         0    0    0  
## 地科  0    0         0    0  
## 生物  0    0    0         0  
## 物理  0    0    0    0
#檢驗數學與物理、數學與生物何者相關高
pacman::p_load(cocor)
cocor(~數學 + 物理 | 數學 + 生物, dta)
## 
##   Results of a comparison of two overlapping correlations based on dependent groups
## 
## Comparison between r.jk (數學, 物理) = 0.7245 and r.jh (數學, 生物) = 0.7108
## Difference: r.jk - r.jh = 0.0137
## Related correlation: r.kh = 0.8972
## Data: dta: j = 數學, k = 物理, h = 生物
## Group size: n = 15549
## Null hypothesis: r.jk is equal to r.jh
## Alternative hypothesis: r.jk is not equal to r.jh (two-sided)
## Alpha: 0.05
## 
## pearson1898: Pearson and Filon's z (1898)
##   z = 5.5418, p-value = 0.0000
##   Null hypothesis rejected
## 
## hotelling1940: Hotelling's t (1940)
##   t = 5.5631, df = 15546, p-value = 0.0000
##   Null hypothesis rejected
## 
## williams1959: Williams' t (1959)
##   t = 5.5544, df = 15546, p-value = 0.0000
##   Null hypothesis rejected
## 
## olkin1967: Olkin's z (1967)
##   z = 5.5418, p-value = 0.0000
##   Null hypothesis rejected
## 
## dunn1969: Dunn and Clark's z (1969)
##   z = 5.5518, p-value = 0.0000
##   Null hypothesis rejected
## 
## hendrickson1970: Hendrickson, Stanley, and Hills' (1970) modification of Williams' t (1959)
##   t = 5.5631, df = 15546, p-value = 0.0000
##   Null hypothesis rejected
## 
## steiger1980: Steiger's (1980) modification of Dunn and Clark's z (1969) using average correlations
##   z = 5.5498, p-value = 0.0000
##   Null hypothesis rejected
## 
## meng1992: Meng, Rosenthal, and Rubin's z (1992)
##   z = 5.5496, p-value = 0.0000
##   Null hypothesis rejected
##   95% confidence interval for r.jk - r.jh: 0.0182 0.0381
##   Null hypothesis rejected (Interval does not include 0)
## 
## hittner2003: Hittner, May, and Silver's (2003) modification of Dunn and Clark's z (1969) using a backtransformed average Fisher's (1921) Z procedure
##   z = 5.5493, p-value = 0.0000
##   Null hypothesis rejected
## 
## zou2007: Zou's (2007) confidence interval
##   95% confidence interval for r.jk - r.jh: 0.0088 0.0185
##   Null hypothesis rejected (Interval does not include 0)
cocor(~數學 + 物理 | 地科 + 生物, dta)
## 
##   Results of a comparison of two nonoverlapping correlations based on dependent groups
## 
## Comparison between r.jk (數學, 物理) = 0.7245 and r.hm (地科, 生物) = 0.8764
## Difference: r.jk - r.hm = -0.1519
## Related correlations: r.jh = 0.6937, r.jm = 0.7108, r.kh = 0.8612, r.km = 0.8972
## Data: dta: j = 數學, k = 物理, h = 地科, m = 生物
## Group size: n = 15549
## Null hypothesis: r.jk is equal to r.hm
## Alternative hypothesis: r.jk is not equal to r.hm (two-sided)
## Alpha: 0.05
## 
## pearson1898: Pearson and Filon's z (1898)
##   z = -42.2316, p-value = 0.0000
##   Null hypothesis rejected
## 
## dunn1969: Dunn and Clark's z (1969)
##   z = -48.6493, p-value = 0.0000
##   Null hypothesis rejected
## 
## steiger1980: Steiger's (1980) modification of Dunn and Clark's z (1969) using average correlations
##   z = -48.1480, p-value = 0.0000
##   Null hypothesis rejected
## 
## raghunathan1996: Raghunathan, Rosenthal, and Rubin's (1996) modification of Pearson and Filon's z (1898)
##   z = -48.6493, p-value = 0.0000
##   Null hypothesis rejected
## 
## silver2004: Silver, Hittner, and May's (2004) modification of Dunn and Clark's z (1969) using a backtransformed average Fisher's (1921) Z procedure
##   z = -47.8431, p-value = 0.0000
##   Null hypothesis rejected
## 
## zou2007: Zou's (2007) confidence interval
##   95% confidence interval for r.jk - r.hm: -0.1590 -0.1449
##   Null hypothesis rejected (Interval does not include 0)

P值很小,因此四捨五入後都只看到0

Q:不知道是否可以讓P值不要四捨五入?

densityplot

lty = 1為實線,2為虛線

##連續變項與類別變項間的關係
##兩個類別
#看看家裡有無電腦跟數學間的關係
densityplot(~ 數學, groups = 電腦, data = dta, xlab = '數學分數', lty = c(1,2),
  plot.points = F, type = "g", , main = '電腦 (無 = 虛線, 有 = 實線)')

qq plot

#也可用QQ圖比較
qq(電腦 ~ 數學, data = dta, type = c('p', 'g'), pch = '.', aspect = 1, 
   xlab = '數學分數 (有電腦)', ylab = '數學分數 (無電腦)')

#看看有無電腦的學生數學平均與數標差
aggregate(數學 ~ 電腦, data = dta, FUN = mean)
##   電腦     數學
## 1   有 605.5098
## 2   無 532.7131
aggregate(數學 ~ 電腦, data = dta, FUN = sd)
##   電腦     數學
## 1   有 90.27149
## 2   無 93.07106

histogram 多個類別

layout 以4 column 1row呈現

#看看不同國家學生的資料分數直方圖
#圖2.6
histogram(~ 數學 | 國家, data = dta, xlab = '數學分數', ylab='機率',
          type = 'density', layout = c(4, 1))

bwplot

#以盒鬚圖看不同國家學生的差異
bwplot(國家 ~ 數學, data = dta, xlab = '數學分數',
  panel = function(x, y, ...) {
    panel.bwplot(x, y, ...)
    panel.grid(v = -1, h = 0, ...) })

#看看不同國家的學生數學平均與數標差
aggregate(數學 ~ 國家, data = dta, FUN = mean)
##   國家     數學
## 1 日本 573.7170
## 2 台灣 616.4054
## 3 香港 592.8587
## 4 韓國 620.3121
aggregate(數學 ~ 國家, data = dta, FUN = sd)
##   國家      數學
## 1 日本  83.42566
## 2 台灣 101.97112
## 3 香港  79.96656
## 4 韓國  86.35808

xyplot

#看看不同國家間,物理與數學間的關係是否類似
#圖2.8
xyplot(物理 ~ 數學 |  國家, data = dta, xlab = '數學分數', ylab = '物理分數',
       type = c("g", "p", "r"), cex = 0.1, layout = c(4, 1))

##連續變項與兩個類別變項間的關係
#看看不同國家、有無電腦的學生數學平均與對應的平均數標準誤
#程式報表2.7
show(m0 <- aggregate(數學 ~ 國家 + 電腦, data = dta, FUN = mean))
##   國家 電腦     數學
## 1 日本   有 578.7294
## 2 台灣   有 618.5894
## 3 香港   有 593.1577
## 4 韓國   有 620.6642
## 5 日本   無 524.6525
## 6 台灣   無 539.3525
## 7 香港   無 542.0274
## 8 韓國   無 576.9508
show(m1 <- aggregate(數學 ~ 國家 + 電腦, data = dta, function(x) sd(x)/sqrt(length(x))))
##   國家 電腦      數學
## 1 日本   有  1.469768
## 2 台灣   有  1.518699
## 3 香港   有  1.403073
## 4 韓國   有  1.313146
## 5 日本   無  4.645107
## 6 台灣   無  9.954817
## 7 香港   無 23.474131
## 8 韓國   無 16.026719
#把資料集中,並重排國家
#創造誤差範圍:l與u為mean-se, mean+se
m <- data.frame(m0, l = m0$數學 - m1$數學, u = m0$數學 + m1$數學)
m$國家 <- factor(m$國家, levels=c('日本', '香港', '台灣', '韓國'))

segplot 繪製兩個類別與連續變項的關係圖

#在圖中加入了誤差
segplot( 國家 ~ l + u | 電腦, data = m, centers = 數學, 
        draw.bands = F, xlab = '數學平均分數', ylab = '國家',
        main = '電腦', layout = c(1, 2),
        segments.fun = panel.arrows, ends = "both", angle = 90, length = 1, 
        unit = "mm")