# KRUSKAL WALLIS in R SCRIPT FILE. # www_statstutor_ac_uk Community Project. # Sofia Maria Karadimitriou and Ellen Marshall, Sheffield University. # Reviewed by Basile Marquier, University of Sheffield. # Dataset: reaction csv. # Resource: KRUSKAL WALLIS in R. #Open the reaction dataset which is saved as a csv file and call it reactionR. #If your file is saved as a standard Excel file, save it as a csv file first. #You will need to change the command depending on where you have saved the file. #Download the data set in .csv format and put it in a directory on your computer #Load the directory in which the .csv file is: by changing 'C:\\...\\reaction.csv'. reactionR<-read.csv('C:\\...\\reaction.csv',header=T) #Example where csv fie is stored on a memeory stick which is the D drive here. reactionR<-read.csv("E:\\reaction.csv",header=T) #Look at the data. reactionR #Tell R we are using the reaction dataset until further notice using attach. #This means that 'Drink' can be used instead of reactionR$Drink. attach(reactionR) #R assumes all numeric values are continuous so tell it that Drink is a factor factors. #as.factor(variable) is a quick way of defining factors or use the factor command to give categories names. Drink<-as.factor(Drink) #Note: If you need to label numeric categories, use the #factor (variable,c(numbers),labels=(value labels)) command instead. ############ Summarising one continuous variable by group ##############. #calculate the medians by type of drink by using tapply(dependent,independent,summary statistic) tapply(ReactionTime,Drink,median) #Alternatively use the summary command #Request summary statistics of reaction time by group #give each summary a name. Water_summary<-summary(ReactionTime[Drink=='Water']) Coffee_summary<-summary(ReactionTime[Drink=='Coffee']) Alcohol_summary<-summary(ReactionTime[Drink=='Alcohol']) #Combine the results into one table and give it a name compare1<-cbind(Water_summary,Coffee_summary,Alcohol_summary) #Then reduce the decimal places to 2. round(compare1,2) #To generate a boxplot for reaction time for each drink use the boxplot() command boxplot(ReactionTime~Drink) #Perform Kruskal-Wallis test by using kruskal.test() to find the difference in medians for different drinks kruskal.test(ReactionTime~Drink) #Carry out Wilcoxon Test with a Bonferroni adjusted of the p-values for the multiple tests. pairwise.wilcox.test(ReactionTime,Drink,p.adj='bonferroni',exact=F)