[[Rのメモ]]

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

**Chapter 3 [#je208ec0]

-主な関数
--table() :例)table(FILLER)
--prop.table() : 指定したカラムの因子を集計し相対頻度(%)を出す 例:prop.table(table(FILLER))
--cumsum(): ベクトルを1つ1つ足して累積頻度を出す 例:cumsum(1:5) → 1 3 6 10 15

-plot()
--plot(a): a の値を y 、ベクトルの数値の出現順を x にとってプロット
--plot(a, b): a と b のバイプロット
--引数 type=b (both の意味):点と線でグラフを描画
--引数 type=l (line) :点はなし。線のみ
--引数 xlab=" ", ylab=" ": 軸の名前をつける
--引数 main=" ":全体のタイトルをつける
--引数 xlim, ylim:軸の数値の範囲を指定
-grid(): 全体に表中に薄いグリッド線を描く

-pie()
-barplot()
-text()

-pareto.chart() : library "qcc" より「パレート図」といわれるもの

-hist()

1.2 Measures of central tendency

-which.max()
-max()
-median()
-mean()

-geometric mean: 幾何学的平均、相乗平均ともいう
--http://www.kwansei.ac.jp/hs/z90010/sugakuc/toukei/daihyou2/daihyou2.htm

1.3 Measures of dispersion

-relative entropy: mode に対して与える dispersion measure
--2つの確率分布の差の尺度、Kullback–Leibler divergence ともいう

-interquartile range/ quartiles: median に対して使う dispersion measure

-standard deviation/ variance / quartiles: 平均値のとれるデータに対しての dispersion measure













**Chapter 1 [#x707dde3]

-p.5: Table 1 をカイ2乗検定してみる:

 > 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

-p.7: Partial eta-squared とは Effect size の種類の1つ、どのくらい特定の独立変数の従属変数への影響が大きいかを示す

 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).

-p.24: ''Occam's razor'' (or Ockham's razor), often expressed in Latin as the '''lex parsimoniae''', translating to ''law of parsimony'', ''law of economy'' or ''law of succinctness'', is a principle that generally recommends selecting the competing hypothesis that makes the fewest new assumptions, when the hypotheses are equal in other respects. For instance, they must both sufficiently explain available data in the first place. --Wikipedia

-p.34: the probability p to lose 60 times or moer just by chance is 0.02844397, 2.8%
--計算は以下のようにしているはず:

 > 1-pbinom(40,100,0.5,lower.tail=FALSE)
 [1] 0.02844397

-p.45: Figure 11 の図に近いものを作る

 > 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では[ 変数++ ]が動かないので注意だ!(笑)
 }


-token set:
--研究デザインの用語ではあまり聞いたことがない。プログラミングで演算子や変数のことを token というので、「変数セット」的な意味合いであると思う。変数ならば variable の方が普通。
--Schematic token set: 変数の名称による変数間の分類整理
--Concrete token set: その変数を具体的な刺激(stimuli)におろした場合の実際例での整理


◆ 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軸を粗めに取っていましたが、こちらの方がグラフ的に正しいのではないかと。気になる方は調整可能です。


**質問・コメント [#re54ec56]
- 2項分布の裾野を塗りつぶすための別解を載せてみました。 -- [[kanetaku]] &new{2011-04-20 (水) 23:00:40};
- Figure 28 は出せるかい? -- [[tono]] &new{2011-05-23 (月) 23:34:06};
- Figure28出してみました。あまりエレガントなやり方ではないですが……何か重要なことを忘れている気がする。 -- [[kanetaku]] &new{2011-05-24 (火) 13:49:06};
- いつの間にか2人で更新するというメッセージが(笑)!他の方も編集・コメントなどしていただいていいかもと思いますが、いかがでしょう。 -- [[kanetaku]] &new{2011-05-24 (火) 13:50:31};
- よく見たらグリッドが入っていたのでこちらも入れてみました。本はなぜ長方形に取っているのだろう。 -- [[kanetaku]] &new{2011-05-24 (火) 13:59:43};

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