本週上課將介紹兩個變數的相關,包括兩個類別變數以及兩個連續變數。配合之前討論的視覺化,可以描述兩個變數之間的關係,例如:
with(anscombe, cor(x1, y1))
## [1] 0.8164205
社會科學經常分析類別資料。類別資料之間的關聯性有各種分析方法,原則為減少犯錯的比例,也就是 Proportional Reduction of Error (PRE) 。
根據資料的特性,可使用不同的關聯性指標:
為何要用\(\mathcal {X}^2\)分佈?卡方分佈適用於數個獨立變數的平方和,最初是研究變異數的學者發現。它是\(\Gamma\)分佈的一種。因此卡方分佈的最重要參數是自由度。
\(\hat{n_{ij}}=(RT\times CT)/ N\)
library(kableExtra)
dt <-data.frame(a=c("","" ,"X","" , "Total"),
b=c("" ,"" , 1 , 2,"" ),
C=c("Y", 1, "f11", "f12", "R1"),
d=c("", 2, "f21" , "f22" , "R2" ),
e=c("Total" , "" , "C1" , "C2" , "N" )
)
colnames(dt)<-NULL
dt %>%
kable("html") %>%
kable_styling()
Y | Total | |||
1 | 2 | |||
X | 1 | f11 | f21 | C1 |
2 | f12 | f22 | C2 | |
Total | R1 | R2 | N |
或者是
df | |||||
1 | 2 | 3 | 4 | 5 | |
0.05 | 3.84 | 5.99 | 7.82 | 9.49 | 11.07 |
0.01 | 6.63 | 9.21 | 11.34 | 13.28 | 15.09 |
也可以用qchisq
這個指令,設定p值與自由度之後,即可計算應該大於多少卡方值:
qchisq(0.95, df=1)
## [1] 3.841459
反過來,pchisq
可以輸出一定的自由度下卡方值的機率,例如:
1-pchisq(2.9, 1)
## [1] 0.08857955
1-pchisq(0.16, 1)
## [1] 0.6891565
性別 | 個案數 | 理論值 |
男 | 531 | 528.165 |
女 | 539 | 538.835 |
如果我們想知道政黨支持與婚姻狀態是否互相獨立?以下表資料為例:
婚姻 | Total | |||
已婚 | 其他 | |||
政黨支持 | 國民黨 | 141 | 77 | 218 |
民進黨 | 91 | 72 | 163 | |
Total | 232 | 149 | 381 |
marri<-c(141,77,91,72)
marripid<-matrix(marri,2)
marripid
## [,1] [,2]
## [1,] 141 91
## [2,] 77 72
rt<-margin.table(marripid,1)
ct<-margin.table(marripid,2)
newtable<-cbind(marripid,rt)
rbind(newtable,ct)
## rt
## 141 91 232
## 77 72 149
## ct 218 163 218
再計算卡方值
n=sum(marri)
e11<-rt[1]*ct[1]/n; e21<-rt[2]*ct[1]/n
e12<-rt[1]*ct[2]/n;e22<-rt[2]*ct[2]/n
expec <- matrix(c(e11,e21,e12,e22),2)
print(expec)
## [,1] [,2]
## [1,] 132.74541 99.25459
## [2,] 85.25459 63.74541
dt<-c((marripid[1,1]-expec[1,1])^2/e11,(marripid[1,2]-expec[1,2])^2/e12,
(marripid[2,1]-expec[2,1])^2/e21,(marripid[2,2]-expec[2,2])^2/e22)
dt
## [1] 0.5133007 0.6865003 0.7992333 1.0689132
chisq<-sum(dt)
chisq
## [1] 3.067948
如果用R
來處理,可以用chisq.test
這個函數:
marri<-c(141,77,91,72)
marripid<-matrix(marri,2)
marripid
## [,1] [,2]
## [1,] 141 91
## [2,] 77 72
chisq.test(marripid)
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: marripid
## X-squared = 2.7075, df = 1, p-value = 0.09988
教育程度 | 理論值 | 殘差 |
小學及以下 | 192.66 | -6.66 |
國中 | 228.42 | -81.42 |
高中 | 216.56 | 94.43 |
專科 | 202.64 | -65.64 |
大學及以上 | 226.68 | 59.31 |
library(ggplot2)
ggplot(anscombe, aes(x=x2, y=y1)) +
geom_point(size=3, col="darkgreen") +
labs(subtitle="Data: anscombe") +
theme_bw()
Scatter Plot
可以加上迴歸線表現兩個變數的相關:
ggplot(anscombe, aes(x=x2, y=y1)) +
geom_point(col='darkgreen', size=3) +
geom_smooth(method="lm", se=F, col='blue')
Scatter Plot with Regression Line
也可以用非線性迴歸線(Locally Weighted Scatterplot Smoothing, LOESS)表示兩者的相關:
ggplot(anscombe, aes(x=x2, y=y1)) +
geom_point(col='darkgreen', size=3) +
geom_smooth(method="loess", se=F) +
theme_bw()
Scatter Plot with LOESS Line
如果Y是X加上一個單位:
df <- tibble(X=rnorm(20,0,1), Y=X+1)
with(df, cor(X, Y))
## [1] 1
ggplot(df, aes(x=X, y=Y)) +
geom_point(fill='brown', size=5, shape=1) +
geom_smooth(method="lm", se=F, col='blue')
如果把Y改為\(X^2\):
df <- tibble(X=rnorm(20,0,1), Y=X^2)
with(df, cor(X, Y))
## [1] 0.1879333
ggplot(df, aes(x=X, y=Y)) +
geom_point(fill='brown', size=5, shape=1) +
geom_smooth(method="lm", se=F, col='blue')
回到剛剛的例子:
with(anscombe, cor(x2, y2))
## [1] 0.8162365
mtcars
這筆資料的wt以及mpg繪製散佈圖,並且計算相關係數:
PP0797B2.sav
這筆資料,然侯顯示年齡(age)與統獨立場(tondu)之間的交叉列表,再進行卡方檢定,請問這兩個變數互相獨立嗎?
Category | 20至29歲 | 30至39歲 | 40至49歲 | 50至59歲 | 60至69歲 |
N | 351 | 488 | 610 | 374 | 235 |
% | 17.05 | 23.71 | 29.64 | 18.17 | 11.41 |
UsingR
這個套件中的nym.2002
這筆資料,裡面的age以及time之間的關係,並且計算兩者的相關程度: