# R for Beginners: Running Chi-Squared Tests

January 20, 2017
By

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Chi-squared test is useful to see differences between two distributions when they are not normal. Here’s an easy-to-do chi squared test on R. In this test, I’m trying to see if two hashtags are different in terms of collective coping in response to a social issue (e.g., displaying togetherness, talking about the issue) and social movement intent (e.g., organizing protest activity online). We manually coded two samples from #BlackLivesMatter and #AliveWhileBlack. You can see the test is significant for most of the indicators.

############# BlackLivesMatter & AliveWhileBlack Preliminary Statistics ################

summary(data1)
data <-as.data.frame(data1)

##################### FREQUENCIES AND CHI-SQUARE TESTS ###################
#Full names of variables: 1)Communication_about_the_Stressor (COMSTRS) #2)Displaying_Communal_Coping_Orientation (COMCOP)
#5)Maintaining_collective_identity (IDENT) 6)Group_communication (GRPCOM)
#7)Information_dissemination (INFODIS) 8)Coordination_of_activities (COORD)
#Resource: http://www.statmethods.net/stats/frequencies.html
# Rows: 0=#BlackLivesMatter, 1=#AliveWhileBlack/ Columns= 0=NO, 1=YES

#These frequency tables give an idea on the distribution of data
# A will be rows, B will be columns
table1 <- table(data\$Hashtag,data\$COMSTRS)
table2 <- table(data\$Hashtag,data\$COMCOP)
table4 <- table(data\$Hashtag,data\$BOUND)
table5 <- table(data\$Hashtag,data\$IDENT)
table6 <- table(data\$Hashtag,data\$GRPCOM)
table7 <- table(data\$Hashtag,data\$INFODIS)
table8 <- table(data\$Hashtag,data\$COORD)

# Chi-Square Tests for each variable. Tests the null hypothesis that the distribution of each variable
#is not different between the two hashtags

chisq.test(table1) #COMSTRS
chisq.test(table2) #COMCOP
chisq.test(table4) #BOUND
chisq.test(table5) #IDENT
chisq.test(table6) #GRPCOM
chisq.test(table7) #INFODIS
chisq.test(table8) #COORD

#######################################################################
Chamil Rathnayake

The post R for Beginners: Running Chi-Squared Tests appeared first on Traversing Bits.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.