#Chapter 8 #Chi-Square #8.1 Summarising and Visualizing Data #8.1.1 Summary Tables for Goodness-of-Fit Data table(geeslin3\$item3) 100*table(geeslin3\$item3)/sum(table(geeslin3\$item3)) #8.1.2 Summaries of Group Comparison Data[Crosstabs] Table <- xtabs(~numberoflang+catdominance, data=beqDom) .Table TM<-matrix(c(12,0,18,16),nrow=2,ncol=2,byrow=T, dimnames=list(c("Relative Clauses", "No +RCs"), c("Method A", "Method B"))) TM .Table <- xtabs(~numberoflang+catdominance+sex, data=beqDom) .Table rowPercents(.Table) # Row Percentages colPercents(.Table) # Column Percentages totPercents(.Table) # Percentage of Total; this only works with two-way tables #8.1.3 Visualizing Categorical Data #8.1.4 Barplots in R barplot(table(geeslin3\$item3), xlab="item3", ylab="frequency") install.packages(“epitools”) library(epitools) colors.plot(TRUE) #パレットの上で左クリック。右クリックでエスケープ→色の番号が出力される attach(beqDom) barplot(tapply(catdominance, list(catdominance, numberoflang), length), col=c("grey83", "grey53", "grey23"), beside=T, ylab="number of responses", xlab="number of languages") locator() 　#barplotの上で左クリック。右クリックでエスケープ→位置の数値が出力される legend(.827,158,legend=c("L1 dominant", "LX dominant", "L1+more dominant"), fill=c("grey83", "grey53", "grey23")) TM<-matrix(c(12,0,18,16),nrow=2,ncol=2,byrow=T, dimnames=list(c("Relative Clauses", "No +RCs"), c("Method A", "Method B"))) barplot(TM,beside=T, main="Teaching Method and Production of Relative Clauses") legend(3.24,14.1,c("Rel.clauses", "No RCs"),fill=c("grey22", "grey83")) #8.1.6 Association Plots install.packages("vcd") library(vcd) (DOM=structable(catdominance ~ numberoflang,data=beqDom)) assoc(DOM, gp=shading_Friendly, labeling_args=list(set_varnames= c(CatDominance ="L1 Dominant", NumberOfLang ="Number of Lges Known"))) DOM3=structable(catdominance~numberoflang+sex,data=beqDom) assoc(DOM3) #Mosaic plot mosaic(DOM, gp=shading_Friendly, labeling_args=list(set_varnames= c(CatDominance="L1 Dominant", NumberOfLangs="Number of Lges Known"))) (DEV=structable(developdelpost~group+pretest,data=Mackey)) mosaic(DEV, gp=shading_Friendly, labeling_args=list(set_varnames= c(developdelpost="delayed posttest level", group="Group", pretest="Pretest level"))) #Doubledecker plot library(vcd) doubledecker(Survived~Class+Sex+Age, data=Titanic) #8.3 One-way Goodness-of-Fit Test chisq.test(table(geeslin3\$item3), correct=false) prob=c(.4,.4,.2) chisq.test(table(geeslin3\$item3),correct=false,p=prob) #8.4 Two-way Group-Independence Test chisq.test(xtabs(~catdominance+numberoflang, data=beqDom), correct=FALSE) .Test= chisq.test(xtabs(~catdominance + numberoflang,data= beqDom), correct=FALSE) .Test\$expected summary(assocstats(xtabs(~catdominance+numberoflang, data=beqDom))) library(vcd) summary(assocstats(xtabs(~catdominance+numberoflang, data=beqDom))) library(coin) independence_test(catdominance~numberoflang,data=beqDom,teststat="quad")