# Predictive Analytics on Chocolate Data ### This script shows only the application of predictive statistics ### The datasets are the final chocolateData and summary dataframes ### Load datafiles into datasets chocolateData<-read.csv("ChocolateDataAnalysis.csv",stringsAsFactors = TRUE) summary<-read.csv("SummaryData.csv",stringsAsFactors = TRUE) ### Scatterplot and Correlation ### # Create a scatterplot of number of chocolates eaten vs. minimum survival time plot(x=summary$Eaten, y=summary$MinSurvive, xlab="Number eaten", ylab="Minimum survival time", main="Number eaten vs. Minimum survival time") # Calculate correlation between number of chocolates eaten and minimum survival time # Create a data frame to analyse dataObs<-data.frame(NumberEaten=summary$Eaten, MinSurvival=summary$MinSurvive) # Calculate Pearson's correlation corData<-cor(dataObs,method="pearson") ### Statistical Analyses # t-test: Survival time for different types of chocolates # Extract chocolateData$SurvivalTime for each type of chocolate Roses<-chocolateData$SurvivalTime[chocolateData$Chocolate=="Roses"] QStreet<-chocolateData$SurvivalTime[chocolateData$Chocolate=="Quality Street"] # Is each data sample normal? Use Shapiro test to find out shapiro.test(Roses) shapiro.test(QStreet) # Both of these show as p<0.05, so they are not normally distributed # Therefore create boxplots to see where the outliers are boxplot(Roses, main="Roses") boxplot(QStreet, main="QStreet") # Roses outliers are above 200, so filter the data sample to remove them # NB: This may take several iterations to remove all outliers Roses<-Roses[Roses<200] boxplot(Roses, main="Roses") # QStreet outliers are above 200, so filter the data sample to remove them # NB: This may take several iterations to remove all outliers QStreet<-QStreet[QStreet<200] boxplot(QStreet, main="QStreet") # Perform t-test for independent samples # Now normal, so can do the t-test # HO: There is no difference between the means, i.e. both chocolates have the same survival time # H1: There is a difference between the means, i.e. one type lasts longer than the other t.test(Roses, QStreet) # The calculated p-value for this is 0.7967. This is more than 0.05, # so we cannot reject the null hypothesis at the p=0.05 level. # We therefore accept the null hypothesis that the type of chocolate # makes no difference to the mean survival time. # Difference between chocolates eaten by doctors and those eaten by nurses # t-test: Survival time for different types of chocolates # Extract chocolateData$SurvivalTime for each type of chocolate Doctors<-chocolateData$SurvivalTime[chocolateData$WhoAte=="Doctor"] Nurses<-chocolateData$SurvivalTime[chocolateData$WhoAte=="Nurse"] # Is each data sample normal? Use Shapiro test to find out shapiro.test(Doctors) shapiro.test(Nurses) # Both of these show as p<0.05, so they are not normally distributed # Therefore create boxplots to see where the outliers are boxplot(Doctors, main="Doctors") boxplot(Nurses, main="Nurses") # Doctors outliers are above 200, so filter the data sample to remove them # NB: This may take several iterations to remove all outliers Doctors<-Doctors[Doctors<200] boxplot(Doctors, main="Doctors") # Nurses outliers are above 300, so filter the data sample to remove them # NB: This may take several iterations to remove all outliers Nurses<-Nurses[Nurses<300] boxplot(Nurses, main="Nurses") # Perform t-test for independent samples # Now normal, so can do the t-test # HO: There is no difference between the means, i.e. doctors and nurses eat chocolates at similar times # H1: There is a difference between the means, i.e. doctors and nurses eat chocolates at different times t.test(Doctors,Nurses) # The calculated p-value for this is 0.7624. This is more than 0.05, # so we cannot reject the null hypothesis at the p=0.05 level. # We therefore accept the null hypothesis that doctors and nurses eat chocolates # at about the same times # ANOVA # Differences between doctors, nurses, and administrators # H0: There is no difference between doctors, nurses, and administrators # H1: There is a significant difference between at least two of the groups # Filter the dataSet to create a new dataframe with just the survival time and the person who ate the chocolate anovaData<-chocolateData[,c(5,10)] anovaData # Filter the new dataset to extract only chocolates eaten by doctors, nurses and administrators anovaData<-anovaData[anovaData$WhoAte %in% c("Doctor","Nurse","Administrative"),] anovaData # Carry out ANOVA test # Compute the analysis of variance resaov <- aov(anovaData$SurvivalTime ~ anovaData$WhoAte, data = anovaData) # Summary of the analysis summary(resaov) # Df Sum Sq Mean Sq F value Pr(>F) # anovaData$WhoAte 2 45814 22907 3.382 0.0378 * # Residuals 103 697674 6774 # --- # Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 # There is a significant difference between groups at the p=0.05 level # We can reject the null hypothesis and accept the alternative hypothesis # It is therefore necessary to perform post-hoc tests to find out where these differences occur # Perform Tukey Pair-wise post-hoc analysis TukeyHSD(resaov) #Tukey multiple comparisons of means #95% family-wise confidence level #Fit: aov(formula = anovaData$SurvivalTime ~ anovaData$WhoAte, data = anovaData) #$`anovaData$WhoAte` # diff lwr upr p adj #Doctor-Administrative 58.65367 4.006131 113.30122 0.0323376 #Nurse-Administrative 24.73349 -23.998754 73.46574 0.4517825 #Nurse-Doctor -33.92018 -78.978504 11.13815 0.1779281 # The Tukey test shows that there is a significant difference at the p=0.05 level # between doctors and administrators, but not between other pairs of groups