Revision Session 1: Week 33

Aims: Revise Spring Term Content

In today’s lab, we want you to try to complete these coding tasks as independently as you can - meaning we will provide one example with code but will then leave the rest up to you. You can refer back to your old exercises, scripts, and instructions. The point of this is that we want you to get practice at knowing where to find the solutions if you don’t know them already.

Hopefully, this will help you recognise your strengths / weaknesses with RStudio. Remember, these sessions will be your last chance to speak to your ALs about questions regarding RStudio, so make use of them!!

Today, we will begin by using the ice_cream_data.csv for our analyses.


First you need to import your data. We do this using the read.csv() function. You can save this to an object called df. See previous weeks for instructions on how to do this.

We will load all the required packages at the top of our script, today we need the following:

library(tidyverse) #for the view() function
library(psych) #for the describeBy() function

library(rstatix) #for chi square related material
library(gmodels) #for chi square related material

options("scipen"=9999, "digits"=10) ### to avoid scientific notation

Before moving onto the tasks for today, it is always a good idea to take a look at your data. You can do so using the view() package, which is provided by the tidyverse package.

view(df)

You should see a dataframe with 9 variables in. Take a look at the variables. This dataset is all about ice cream! Consider what type of data each of these are. Our variables are:

  • Ice_Cream_Flavour - the flavour the customer bought
  • Ice_Cream_Van - where the ice cream was bought from
  • Eye_Colour - the customer’s eye colour
  • Hair_Colour - the customer’s hair colour
  • Satisfaction - the customer’s satisfaction after their purchase
  • Mood - the customer’s post-ice-cream mood
  • Pre_Icecream_Hunger - the customer’s self-reported hunger level before eating their ice-cream
  • Post_Icecream_Hunger - the customer’s self-reported hunger level after eating their ice-cream
  • Pre_enjoyment - the customer’s self-reported how much they were enjoying their day before the ice-cream
  • Post_enjoyment - the customer’s self-reported how much they were enjoying their day after eating their ice-cream
  • Pre_Opinion - the customer’s opinion of the ice cream van before eating their ice cream
  • Post_Opinion - the customer’s opinion of the ice cream van after eating their ice cream

Task 1: Independent t-tests

Independent t-tests are utilised to investigate the difference between two means. They are independent because the data being used for each mean comes from two different sets of participants. As an example, if you were investigating the ice cream sales of two competing ice-cream vans… ice-cream sales would be your dependent variable (the variable we measure), while ice-cream van (van 1 vs. van 2) would be our independent variable (the variable we manipulate/change). To analyse this, we would want an independent t-test because our two means of ice cream sales are from different participants (i.e., van 1 vs. van 2).

So, we can run an independent t-test to investigate if our two means are significantly different from one another.

Task 1.1: Worked example

We will now work through an example together using our actual data. In this example, we will be using Ice_Cream_Van and Satisfaction from our df. Ice_Cream_Van is a categorical variable, with either Sunny_Ice_Cream or Summer_Icepop_Stand as options.

  1. First, let’s make our categorical variable into a factor. Then, we’ll take a look at how many responses of each type there are:
df$Ice_Cream_Van <- as.factor(df$Ice_Cream_Van)

table(df$Ice_Cream_Van)
## 
## Summer_Icepop_Stand     Sunny_Ice_Cream 
##                  39                  61
  1. Now, let’s take a look at our Satisfaction means for each Ice_Cream_Van:
describeBy(df$Satisfaction, df$Ice_Cream_Van)

Remember to look at the means, sds, and skew! Look back on week 26 for more information on this and assumption checks! We look at skew for normality - wanting each variable to fall within the -1 to 1 range (as we have more than 20 participants). We look at both variable’s sd for homogeneity - we want them to be close to one another. Largely different sds (i.e., if one is twice as large as the other) are problematic!

  1. Let’s see if the Sunny_Ice_Cream van had significantly different levels of Satisfaction compared to the Summer_Icepop_Stand:
t.test(df$Satisfaction ~ df$Ice_Cream_Van, var.equal = TRUE)
## 
##  Two Sample t-test
## 
## data:  df$Satisfaction by df$Ice_Cream_Van
## t = -3.0599466, df = 98, p-value = 0.002855762
## alternative hypothesis: true difference in means between group Summer_Icepop_Stand and group Sunny_Ice_Cream is not equal to 0
## 95 percent confidence interval:
##  -27.433231586  -5.848819696
## sample estimates:
## mean in group Summer_Icepop_Stand     mean in group Sunny_Ice_Cream 
##                       38.35897436                       55.00000000
  1. Remember, if we wanted to save this output to an object, we can do so using the <- symbol. This allows us to call the output into our console more easily (rather than retyping our whole code again). Once saved as an object, you can just type the name of this into your script/console and run that line:
satisfaction_t_test <- t.test(df$Satisfaction ~ df$Ice_Cream_Van, var.equal = TRUE)

satisfaction_t_test
  1. Have a look at your output and try your best to interpret the results. Remember, we want to look at the two means and the p value. Looking at the p value allows us to determine the significance our test. In this case, as p = .003, there is a significant difference between the two means. As this test was significant, we need to examine the two means to see which was significantly bigger/smaller.

  2. In a report, we would format the reporting of results like so: “We found a significant difference (t (98) = -3.06, p < .01) in satisfaction levels between the Summer Icepop Stand (M = 38.36 (SD = 26.74)) and the Sunny Ice Cream van (M = 55.00 (SD = 26.39)).

In the above example, the SD is taken from the describeBy() function from earlier.

Task 1.2: Individual attempts

Now, have an attempt at working out the suitable code for the questions below. Note, remember to check the assumptions:

  1. Is there a significant difference in Satisfaction across Eye_Colour?

  2. Is there a significant difference in Mood across Ice_Cream_Van?

  3. Is there a significant difference in Mood across Eye_Colour?

NOTE. Answers can be found at the bottom of this document - but we highly recommend trying yourself first!

Task 2: Paired t-tests

Paired t-tests are very similar to independent t-tests. However, they are utilised to investigate the difference between two means that come from the same person (i.e., repeat measures) or matched participants (revisit week 27 for theory on this).

Using our ice-cream sales example from before: if we were this time investigating ice-cream sales from one ice-cream van but at two different times (i.e., winter vs summer), then this would be suited for a paired t-test.

Task 2.1: Worked example

For our worked example, we will utilise the two variables: Pre_Icecream_Hunger and Post_Icecream_Hunger. We would be investigating if eating an ice-cream has had an effect on hunger levels.

The first step with a paired t-test is to calculate a difference score so we can check the normality assumption. We begin by subtracting one variable (Post_Icecream_Hunger) from the other (Pre_Icecream_Hunger), creating a new variable. We’ll call this hunger_change. We then need to examine the skew of this variable:

df$hunger_change <- (df$Post_icecream_hunger - df$Pre_icecream_hunger)

skew(df$hunger_change)

### OR, you can achieve the same by doing just the following line... both give the same answer!

skew(df$Post_icecream_hunger - df$Pre_icecream_hunger)

We have more than 20 participants in this dataframe, so the skew value would need to be outside the -1.00 to 1.00 range in order for normality to be violated. As our value fits within this range, we can assume normality! Woo! We can move forward with our paired t-test.

This is now nearly identical to independent t-tests, but we separate our variables with a comma (,) instead of a tilda (~), and we add the ‘paired =’ argument to our code. We can then run our means and standard deviations to round out our report:

#t-test
t.test(df$Pre_icecream_hunger, df$Post_icecream_hunger, paired = TRUE)

# Pre-stress
mean(df$Pre_icecream_hunger) 
sd(df$Pre_icecream_hunger)

#Post-stress
mean(df$Post_icecream_hunger)
sd(df$Post_icecream_hunger)

A very significant result (as we might have expected!). To report this:

“We investigated the effect of ice-cream consumption on hunger. The normality assumption was met as the skew (skew = -0.02) of the change-score of these variables fell within the -1 and 1 range. Post-icecream hunger (M = 15.96 (SD = 8.71) was lower than pre-icecream hunger (M = 65.50 (SD = 20.52)), this difference was found to be significant (t(99) = 22.84, p < .001).

Task 2.2: Individual attempts

Now, have an attempt at working out the suitable code for the questions below. We will be investigating whether customer’s self-reports of their enjoyment different before (Pre_Enjoyment) and after (Post_Enjoyment) eating their ice-cream.

  1. First of all, calculate the change score for these variables. What is the mean change score?

  2. Is the skew of the change score acceptable for us to continue with a paired t-test?

  3. Was there a significant difference between participants’ Pre_Enjoyment and Post_Enjoyment?

  4. Which score was higher?

  5. Attempt to write up all these findings into a report-style paragraph.

Task 3: Chi Square Tests

In this term we also looked at chi square tests! These tests are used to analyse frequencies. In short, the chi square test (and the McNemar test) work by calculating if there is a significant difference between our observed frequencies and the expected frequencies. The expected frequencies is what we would expect to see if there was no association between our variables.

Let’s go back to our ice-cream example!! We might want to see what demographics buy what flavours. Let’s imagine we have two types of customers (green eyes vs. brown eyes), and two ice-cream flavours (vanilla vs. strawberry). We want to see if eye colour has any effect on flavour preference. If there was no association, we would expect ice-cream sales to be equal:

Expected frequencies:

Brown eyes Green eyes
Vanilla 50 50
Strawberry 50 50

If, however, there is an association between eye colour and flavour preference, our observed frequencies would be different:

Observed frequencies:

Brown eyes Green eyes
Vanilla 50 50
Strawberry 20 80

Task 3.1: Worked example

The first thing we need to do is to create a contingency table for our variables:

table1 <- table(df$Ice_Cream_Flavour, df$Ice_Cream_Van)

You can view this table by running the following code:

table1

Using the CrossTable() function allows us our contingency table to also include row percentages.

table1_with_percentage <- CrossTable(df$Ice_Cream_Flavour, df$Ice_Cream_Van, 
                     prop.r = TRUE, prop.c = FALSE, prop.t = FALSE, 
                     prop.chisq = FALSE, format = "SPSS")
## 
##    Cell Contents
## |-------------------------|
## |                   Count |
## |             Row Percent |
## |-------------------------|
## 
## Total Observations in Table:  100 
## 
##                      | df$Ice_Cream_Van 
## df$Ice_Cream_Flavour | Summer_Icepop_Stand  |     Sunny_Ice_Cream  |           Row Total | 
## ---------------------|---------------------|---------------------|---------------------|
##           Strawberry |                  9  |                 43  |                 52  | 
##                      |             17.308% |             82.692% |             52.000% | 
## ---------------------|---------------------|---------------------|---------------------|
##              Vanilla |                 30  |                 18  |                 48  | 
##                      |             62.500% |             37.500% |             48.000% | 
## ---------------------|---------------------|---------------------|---------------------|
##         Column Total |                 39  |                 61  |                100  | 
## ---------------------|---------------------|---------------------|---------------------|
## 
## 

This gives us a very early indication of where potential differences may lie. In this case, we see that that Strawberry and Vanilla sales are quite different: The Summer Icepop Stand barely sold any strawberry, while the Sunny Ice Cream van sold mostly strawberry.

We still need to run our actual analysis, though!


In order to run our analysis, we need to use our chisq.test function, and input our first table we created (table1), and then fill out the correct= argument with “FALSE”. This tells RStudio to not use a correction - this is simply not needed right now.

chi <- chisq.test(table1, correct = FALSE)

chi
## 
##  Pearson's Chi-squared test
## 
## data:  table1
## X-squared = 21.427879, df = 1, p-value = 0.000003673902

So, we now know our chi square analysis was significant, we now need to check our standardised residuals… these tell us where exactly the significant differences are:

chi$stdres
##             
##              Summer_Icepop_Stand Sunny_Ice_Cream
##   Strawberry        -4.629025659     4.629025659
##   Vanilla            4.629025659    -4.629025659

These show us how many standard deviations a frequency is away from the expected frequencies. Our cutoff is +/- 2. As all of our values are greater than +/- 2, we see that there is an association between ice-cream flavours at both ice-cream vans. Meaning:

“We carried out a chi-square analysis in order to investigate whether ice-cream flavour sales are independent of the ice-cream van selling them.

Results indicated that ice-cream flavour and ice-cream van are not independent, χ²(1, N=100) = 21.48, p < .001. 82.69% of the Sunny Ice-Cream van’s sales are strawberry flavour, while only 17.31% of the Summer Icepop Stand’s sales were strawberry flavour.”

It seems like one ice cream van has a very good strawberry flavour and one does not!

Task 3.2: Individual attempts

Now, we want you to see if there is an association between the following variables in the ice_cream_data.csv:

  1. Is there an association between Ice-Cream Flavour preference and Eye Colour

  2. Is there an association between Ice-Cream Flavour preference and Hair Colour

  3. For the next task, we want you to apply what we’ve covered on chi square tests and apply this to a McNemar test. A McNemar test is very similar to chi square, but is used when the data comes from the same participants (i.e., repeat measures). We will provide guided steps but you will need to work out the exact code. We will be investigating the customers’ pre- (Pre_Opinion) and post-opinions (Post_Opinion) of the ice cream van:

  1. First, we need to make sure that our two enjoyment variables are made into factors. Do so by using the as.factor()!

  2. Then, we need to create a table. This time, however, we use the xtabs() function.

  3. Fill in the red text with the correct details


table2 <- xtabs( ~ response 1 + response 2, data = name of data frame)


  1. Just like with the chi square test, we can create another table, and this one can include the row percentages:

CrossTable(df$first response, df$second response, prop.r = TRUE, prop.c = TRUE, prop.t = FALSE, prop.chisq = FALSE, format = “SPSS”)


  1. Now, we carry out our McNemar test in the same way we did our chi square test:

mc_n <- mcnemar_test(our xtabs table, correct = False)

Finally, call this object by typing its name into your console/script and run the line:

mc_n



Answers

Independent t-test Answers

Question 1

df$Eye_Colour <- as.factor(df$Eye_Colour)
table(df$Eye_Colour)
describeBy(df$Satisfaction, df$Eye_Colour)

t.test(df$Satisfaction ~ df$Eye_Colour, var.equal = TRUE)
  • Customers with brown eyes (M = 59.13 (SD = 24.16)) had higher satisfaction levels than customers with green eyes (M = 35.53 (SD = 26.22)) and this difference was significant (t(98) = 4.68, p < .001).

Question 2

table(df$Ice_Cream_Van)

describeBy(df$Mood, df$Ice_Cream_Van)

t.test(df$Mood ~ df$Ice_Cream_Van, var.equal = TRUE)
  • Mood ratings at the Summer Icepop Stand (M = 49.77 (SD = 30.94)) and the Sunny Ice Cream van (M = 53.72 (SD = 28.23)) were not significantly different (t(98) = -0.66, p = .512).

Question 3

table(df$Eye_Colour)

describeBy(df$Mood, df$Eye_Colour)

t.test(df$Mood ~ df$Eye_Colour, var.equal = TRUE)
  • Customers with brown eyes (M = 44.42 (SD = 26.23)) had higher mood scores than customers with green eyes (M = 61.67, (SD = 30.16)). This difference was significant (t(98) = -3.06, p < .01).

Paired t-test Answers

  1. Which score was higher?

  2. Attempt to write up all these findings into a report-style paragraph.

Question 1

df$enjoyment_change <- (df$Post_enjoyment - df$Pre_enjoyment)
mean(df$enjoyment_change)
## [1] 33.81

Question 2

skew(df$enjoyment_change)
## [1] 0.08033894256
## OR

skew(df$Post_enjoyment - df$Pre_enjoyment)
## [1] 0.08033894256

This falls within our range of -1 to 1, so we can proceed.

Question 3

t.test(df$Pre_enjoyment, df$Post_enjoyment, paired = TRUE)

The difference is significant!

Question 4

mean(df$Pre_enjoyment)
## [1] 30.87
sd(df$Pre_enjoyment)
## [1] 16.83170109
mean(df$Post_enjoyment)
## [1] 64.68
sd(df$Post_enjoyment)
## [1] 20.05289974

Question 5

We investigated the difference between participants’ enjoyment levels before and after eating an ice cream. The normality assumption was met as the skew of the change-score of these variables (skew = -0.08) fell within the -1 and 1 range. Pre-ice cream enjoyment scores (M= 30.87 (SD = 16.68) were lower than post-ice cream enjoyment scores (M = 64.68 (SD = 20.05)) and this difference was significant (t(99) = -12.27, p < .001).


Chi Square Answers

Question 1

table2 <- table(df$Ice_Cream_Flavour, df$Eye_Colour)
table2
##             
##              Brown Green
##   Strawberry    24    28
##   Vanilla       31    17
table2_with_percentages <- CrossTable(df$Ice_Cream_Flavour, df$Eye_Colour, 
           prop.r = TRUE, prop.c = FALSE, prop.t = FALSE,
           prop.chisq = FALSE, format = "SPSS")
## 
##    Cell Contents
## |-------------------------|
## |                   Count |
## |             Row Percent |
## |-------------------------|
## 
## Total Observations in Table:  100 
## 
##                      | df$Eye_Colour 
## df$Ice_Cream_Flavour |    Brown  |    Green  | Row Total | 
## ---------------------|-----------|-----------|-----------|
##           Strawberry |       24  |       28  |       52  | 
##                      |   46.154% |   53.846% |   52.000% | 
## ---------------------|-----------|-----------|-----------|
##              Vanilla |       31  |       17  |       48  | 
##                      |   64.583% |   35.417% |   48.000% | 
## ---------------------|-----------|-----------|-----------|
##         Column Total |       55  |       45  |      100  | 
## ---------------------|-----------|-----------|-----------|
## 
## 
table2_with_percentages
## NULL
chi2 <- chisq.test(table2, correct = FALSE)
chi2
## 
##  Pearson's Chi-squared test
## 
## data:  table2
## X-squared = 3.4252784, df = 1, p-value = 0.06420542
chi2$stdres
##             
##                     Brown        Green
##   Strawberry -1.850750773  1.850750773
##   Vanilla     1.850750773 -1.850750773

“We carried out a chi-square analysis in order to investigate whether ice-cream flavour preference is independent of customer eye colour.

Results indicated that customer’s ice-cream flavour preference and eye colour are independent, χ²(1, N=100) = 3.43, p = .064. While people with brown eyes more often liked vanilla (64.58%) and customers with green eyes more often liked strawberry (53.85%), these observed frequencies were not significantly different from the expected frequencies. This suggests, therefore, that there is no relationship present.”

Question 2

table3 <- table(df$Ice_Cream_Flavour, df$Hair_Colour)
table3
##             
##              Blonde Brunette Ginger
##   Strawberry     17       20     15
##   Vanilla        12       22     14
table3_with_percentages <- CrossTable(df$Ice_Cream_Flavour, df$Hair_Colour, 
           prop.r = TRUE, prop.c = FALSE, prop.t = FALSE,
           prop.chisq = FALSE, format = "SPSS")
## 
##    Cell Contents
## |-------------------------|
## |                   Count |
## |             Row Percent |
## |-------------------------|
## 
## Total Observations in Table:  100 
## 
##                      | df$Hair_Colour 
## df$Ice_Cream_Flavour |   Blonde  | Brunette  |   Ginger  | Row Total | 
## ---------------------|-----------|-----------|-----------|-----------|
##           Strawberry |       17  |       20  |       15  |       52  | 
##                      |   32.692% |   38.462% |   28.846% |   52.000% | 
## ---------------------|-----------|-----------|-----------|-----------|
##              Vanilla |       12  |       22  |       14  |       48  | 
##                      |   25.000% |   45.833% |   29.167% |   48.000% | 
## ---------------------|-----------|-----------|-----------|-----------|
##         Column Total |       29  |       42  |       29  |      100  | 
## ---------------------|-----------|-----------|-----------|-----------|
## 
## 
table3_with_percentages
## NULL
chi3 <- chisq.test(table3, correct = FALSE)
chi3
## 
##  Pearson's Chi-squared test
## 
## data:  table3
## X-squared = 0.83312282, df = 2, p-value = 0.65931
chi3$stdres
##             
##                      Blonde       Brunette         Ginger
##   Strawberry  0.84693633433 -0.74620282783 -0.03528901393
##   Vanilla    -0.84693633433  0.74620282783  0.03528901393

“We carried out a chi-square analysis in order to investigate whether ice-cream flavour preference is independent of customer hair colour.

Results indicated that customer’s ice-cream flavour preference and hair colour are independent, χ²(2, N=100) = 0.83, p = .659.”

Question 3

df$Pre_Opinion <- as.factor(df$Pre_Opinion)
df$Post_Opinion <- as.factor(df$Post_Opinion)

table4 <- xtabs(~Pre_Opinion + Post_Opinion, data = df)
table4
##            Post_Opinion
## Pre_Opinion Negative Positive
##    Negative        5       30
##    Positive        3       62
CrossTable(df$Pre_Opinion, df$Post_Opinion, prop.r = TRUE,
prop.c = TRUE, prop.t = FALSE, prop.chisq = FALSE, format = "SPSS")
## 
##    Cell Contents
## |-------------------------|
## |                   Count |
## |             Row Percent |
## |          Column Percent |
## |-------------------------|
## 
## Total Observations in Table:  100 
## 
##                | df$Post_Opinion 
## df$Pre_Opinion | Negative  | Positive  | Row Total | 
## ---------------|-----------|-----------|-----------|
##       Negative |        5  |       30  |       35  | 
##                |   14.286% |   85.714% |   35.000% | 
##                |   62.500% |   32.609% |           | 
## ---------------|-----------|-----------|-----------|
##       Positive |        3  |       62  |       65  | 
##                |    4.615% |   95.385% |   65.000% | 
##                |   37.500% |   67.391% |           | 
## ---------------|-----------|-----------|-----------|
##   Column Total |        8  |       92  |      100  | 
##                |    8.000% |   92.000% |           | 
## ---------------|-----------|-----------|-----------|
## 
## 
mc_n <- mcnemar_test(table4, correct = FALSE)
mc_n
## # A tibble: 1 × 6
##       n statistic    df         p p.signif method      
## * <int>     <dbl> <dbl>     <dbl> <chr>    <chr>       
## 1   100      22.1     1 0.0000026 ****     McNemar test

“McNemar’s test was used to determine whether the frequency of customers who held positive views of the ice cream stands changed significantly before and after eating an ice-cream.

85.71%% of customers who held negative opinions beforehand, held positive opinions after eating an ice cream, and only 4.62% liked it before and not after (McNemar’s χ²(1, N=100) = 22.09, p < .001). 65.% of customers held positive views of the ice cream stands before eating their ice cream, which increased to 92% after.