# INTERACTIONS IN R (ANOVA)SCRIPT FILE. # www_statstutor_ac_uk Community Project. # Sofia Maria Karadimitriou and Ellen Marshall, Sheffield University. # Reviewed by Jim Bull, University of Swansea. # Dataset: diet csv. # Resource: Interactions in ANOVA in R. #Open the diet dataset which is saved as a csv file and call it dietR. #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. dietR<-read.csv("D:\\diet.csv",header=T,sep=",") #Tell R we are using the diet dataset until further notice using attach. #This means that 'Height' can be used instead of dietR$Height. attach(dietR) #R assumes all numeric values are continuous so tell it that ‘gender’ and 'Diet' are factors. #as.factor(variable) is a quick way of defining factors or use the factor command to give categories names. Diet<-as.factor(Diet) # The factor command uses variable<-factor(variable,c(category numbers),labels=c(category names)). gender<-factor(gender,c(0,1),labels=c('Female','Male')) #To generate an interaction plot for weight lost and the interaction of diet and gender #use the interaction.plot(independent1,independent2,dependent) command interaction.plot(Diet, gender, weightlost,main="Mean weight lost by diet and gender",ylab = "mean of weightlost",xlab = "Type of Diet") ## Changing the type of lines ##. #lty specifies the type of lines #lwd specifies the thickness of the lines. If you want the lines to be different, try lwd=c(1,3) #col specifies the colours of the lines. interaction.plot(Diet, gender, weightlost,lty = c(1, 12),col=c(2:3),lwd = 3,ylab = "mean of weightlost", xlab = "Type of Diet",main="Mean weight lost by diet and gender") ## Adding and editing points ##. #Type = b adds points to the line. #pch controls the shape of the points. interaction.plot(Diet, gender, weightlost, main="Mean weight lost by diet and gender", type="b",pch=c(18,24),lty = c(1, 12),col=c(2:3),lwd = 3,ylab = "mean of weightlost", xlab = "Type of Diet",main="Mean weight lost by diet and gender") ## Legend formatting ##. #trace.label controls the name of the legend eg Gender. #leg.bty ="o" puts a box around the legend. #leg.bg=" " controls the background colour of the legend. #Use ?interaction.plot to find out more. interaction.plot(Diet, gender, weightlost, main="Mean weight lost by diet and gender",type="b",pch=c(18,24),lty = c(1, 12),col=c(2:3),lwd = 3,trace.label = "Gender",leg.bty="o", leg.bg="beige",ylab = "mean of weightlost", xlab = "Type of Diet")