# GSS example of chi-square test of independence # Edps 589 # Fall 2018 # C.J.Anderson # # library MASS # vcd # # Shows multiple ways to get X2 and G2 for independence # forshadow: A good model for these data # # Set working directory setwd("D:\\Dropbox\\edps 589\\2 Chi-square") # set libraries library(MASS) library(vcd) # Input data ( gss <- read.table("gss_data.txt",header=TRUE) ) ( gss.tab <- xtabs(count ~ fechld + mapaid,data=gss) ) ( addmargins(gss.tab) ) # Pearson chi-square statistic, df, and pvalue: tabular data chisq.test(gss.tab,correct=FALSE) # or assocstats(gss.tab) # Pearson and Likelihood ratio statistics, df, and pvalues: data frame (case data) loglm(count ~ fechld + mapaid, data=gss) # Pearson and Likelihood ratio statistics, df, and pvalues: tabular data loglm( ~ fechld + mapaid, data=gss.tab) #... and yet another way (likelihood ratio statistic is "deviance"--- # a pre-view to latter in the semester. # -- Need factors fechld.f <- as.factor(gss$fechld) mapaid.f <- as.factor(gss$mapaid) # -- Fit model of independence model.glm <- glm(count ~ fechld.f + mapaid.f, data=gss, family=poisson) summary(model.glm) # -- to get p-values 1-pchisq(44.961,12) # A good model: "log linear x linear"....pre-view to even later in the semester model.good <- glm(count ~ fechld.f + mapaid.f + fechld*mapaid, data=gss, family=poisson) summary(model.good)