Rのメモ

Stefan Th. Gries (2010) Statistics for Linguistics with R (de Gruyter) の授業メモです。投野と金田君で適宜メモっています。

Chapter 3

1.2 Measures of central tendency

1.3 Measures of dispersion

Chapter 1

> TenseAspect <-matrix(c(12,7,19,6,13,19,18,20,38),3,3)
> TenseAspect
     [,1] [,2] [,3]
[1,]   12    6   18
[2,]    7   13   20
[3,]   19   19   38
> chisq.test(TenseAspect)
  
	Pearson's Chi-squared test

data:  TenseAspect 
X-squared = 3.8, df = 4, p-value = 0.4337
Partial η2 (Partial eta-squared): Partial eta-squared describes
the "proportion of total variation attributable to the factor,
partialling out (excluding) other factors from the total nonerror variation"
(Pierce, Block & Aguinis, 2004, p. 918). Partial eta squared is often higher than eta squared.
Cohen (1992) suggests effect sizes for various indexes, including &#402;
(where 0.1 is a small effect, 0.25 is a medium effect and 0.4 is a large effect).
He also offers a conversion table (see Cohen, 1988, p. 283) for eta squared (η2) where 0.0099
constitutes a small effect, 0.0588 a medium effect and 0.1379 a large effect. Though, considering that  
η2 are comparable to r2 when df of the numerator equals 1 (both measures proportion of variance  
accounted for), these guidelines may overestimate the size of the effect. If going by the r guidelines  
(0.1 is a small effect, 0.3 a medium effect and 0.5 a large effect) then the equivalent guidelines for eta-
squared would be the squareroot of these, i.e. 01 is a small effect, 0.09 a medium effect and 0.25 a 
large effect, and these should also be applicable to eta-squared. When the df of the numerator exceeds 
1, eta-squared is comparable to R-squared (Levine & Hullett, 2002).
> 1-pbinom(40,100,0.5,lower.tail=FALSE)
[1] 0.02844397
> curve(dnorm(x,mean=0,sd=1),from=-4,to=4)
> abline(v=qnorm(0.05, lower.tail=TRUE))
> abline(v=qnorm(0.95, lower.tail=TRUE))

◆ Figure 11 -別解

par(mfrow=c(1,2))    #画面分割
plot(dnorm, -4, 4, type="l", xlab="x", ylab="y")    #一つ目の正規分布を描く
xvals <- seq(-4, -2, length=10)     #塗りつぶし範囲を分割
dvals <- dnorm(xvals)       #x軸に対応するy軸座標を入力
polygon(c(xvals,rev(xvals)), c(rep(0,10),rev(dvals)),col="gray")     #塗りつぶし
plot(dnorm, -4, 4, type="l", xlab="x", ylab="y")    #今度は右側のグラフ
xvals <- seq(2, 4, length=10)     #右側の裾野を塗りつぶします
dvals <- dnorm(xvals)    
polygon(c(xvals,rev(xvals)), c(rep(0,10),rev(dvals)),col="gray")

◆ 2項分布グラフ

plot(dbinom(1:100, 100, 0.5), type="s", xlab="100回コインを投げて表の出る回数 (回)", ylab="確率(%)")	#2項分布グラフを描く
xvals = seq(58, 100, length=43)	#x座標を準備
dvals = dbinom(xvals, 100, 0.5)	#x座標に対応する、2項分布のy座標を準備
j = 1						#ループを作成の下準備
for (i in xvals) {
y = dvals[j]				#xに対応するy座標を準備する。
polygon(c(i, i+1, i+1, i), c(0, 0, dvals[j], dvals[j]), col="orange", border="NA")	 #1マスずつ長方形に塗りつぶしていく
j = j+1					#Rでは[ 変数++ ]が動かないので注意だ!(笑)
}

◆ Figure 28. (p. 110)のレシピ 

UHM <- read.table(file="c:/_sflwr/_inputfiles/03-1_uh(m).txt", header=T, sep="\t", comment.char="", quote="") #本の指示通りにファイルを置いてある前提です

female <- mean(subset(UHM, SEX=="female")$LENGTH) #力技で各カテゴリーの平均を算出
male <- mean(subset(UHM, SEX=="male")$LENGTH) #もう少しエレガントなやり方がありそうだが、変数も少ないのでとりあえず力技
silence <- mean(subset(UHM, FILLER=="silence")$LENGTH) #まあ順番が入れ替わったりしてるので、これはこれでメリットがある。はず。
uh <- mean(subset(UHM, FILLER=="uh")$LENGTH)
uhm <- mean(subset(UHM, FILLER=="uhm")$LENGTH)
dialog <- mean(subset(UHM, GENRE=="dialog")$LENGTH)
monolog <- mean(subset(UHM, GENRE=="monolog")$LENGTH)
figure <- c(female, male, silence, uh, uhm, dialog, monolog) #上の数行のまとめ。もちろんtransform()などで順に追加していっても可。

plot(figure, xaxt="n", xlim=c(0.5, 7.5), ylim=c(840, 960), type="n", ylab="Average length", xlab="Figure 28. Mean lengths of disfluencies") 
#細かい数値はFigure28に近づけるため。なくても描けます。type="n"でドットは入れないでおく。

label <- c("female", "male", "silence", "uh", "uhm", "dialog", "monolog") #挿入するテキストの準備
text(c(1:7), figure, as.character(label)) #labelで用意したテキストを挿入。上のplotでxlimを0.5-7.5に設定しないと、文字が画面から多少見切れてしまう。要調整。
#蛇足ながらこのあたりがエレガントでないと思う所以

average <- mean(UHM$LENGTH) #線は全体の平均で引くので、計算
abline(h=average) #横線を引きます

grid() #グリッドを引きます。本ではなぜかX軸を粗めに取っていましたが、こちらの方がグラフ的に正しいのではないかと。気になる方は調整可能です。

質問・コメント



トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS