All data in the exercises are simulated fake data.
a <- 0.1
, a <- 5
.a <- "hello world"
, a <- "3"
.a <- c()
. For example, fruits <- c("banana", "apple", "orange")
, fruits is a vector of strings.a <- factor()
. For example, music_genre <- factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock", "Jazz"))
, music_genre is a factor with 4 levels Jazz, Rock, Classic, and Pop (duplicate values are ignored). It’s good practice to convert categorical independent variables to factors before running statistical analysis.a <- list()
. For example, edibles <- list(c("banana", "apple", "orange"), c("tomato", "potato", "spinach"), c("rice", "bread", "noodle"))
, edibles is a list of vectors.a <- matrix()
. For example, thismatrix <- matrix(c(1,2,3,4,5,6), nrow = 3, ncol = 2)
, thismatrix is a matrix with 2 columns and 3 rows.a <- array()
. For example, thisarray <- array(1:24)
, thisarray is a one-dimensional array.a <- data.frame()
. For example, df <- data.frame (Training = c("Strength", "Stamina", "Other"), Pulse = c(100, 150, 120), Duration = c(60, 30, 45))
, df is a data frame with columns Training, Pulse, and Duration, with three rows of data.Suppose I want to know whether UCL students’ IQs are significantly different from the average (100)1 Fun fact: IQ tests are specifically designed such that the average person would receive the score 100.. I randomly picked 20 students at UCL and asked them to do an IQ test.
Here are their scores: 117 125 116 113 102 122 123 93 99 129 132 94 85 92 101 83 119 119 101 97 (mean = 108.1, s.d.=15.05).
t.test()
function.data = c(117, 125, 116, 113, 102, 122, 123, 93, 99, 129, 132, 94, 85, 92, 101, 83, 119, 119, 101, 97)
mean(data)
## [1] 108.1
sd(data)
## [1] 15.05394
shapiro.test(data)
##
## Shapiro-Wilk normality test
##
## data: data
## W = 0.94113, p-value = 0.2518
t.test(data, mu = 100)
.t.test(data, mu=100)
##
## One Sample t-test
##
## data: data
## t = 2.4063, df = 19, p-value = 0.02646
## alternative hypothesis: true mean is not equal to 100
## 95 percent confidence interval:
## 101.0545 115.1455
## sample estimates:
## mean of x
## 108.1
A one-sample t-test was run to determine whether the mean IQ scores for students at university A (n=20) was different from the average IQ (µ=100). Sampled IQ scores were normally distributed as assessed by Shapiro-Wilk’s test (p= 0.25) and there were no outliers in the data. The group of 20 students were found to have a significantly higher IQ (M=108.1, s.d.=15.05) than the average, t(19) = 2.4, p < 0.03.
I’m interested in whether the frequency of the word ‘museum’ differ between humanities journal papers and blog articles on similar topics. I randomly accessed 10 corpora of humanities journals papers, and calculated the count of the word ‘museum’ per million words in each corpus. And then I did the same with humanities blog articles.
Frequencies of ‘museum’ in each journal paper corpus (count per million): 76 67 66 72 74 64 72 70 74 78
Frequencies of ‘museum’ in each blog article corpus (count per million): 66 63 68 68 60 67 58 70 67 66
values <- c(76, 67, 66, 72, 74, 64, 72, 70, 74, 78, 66, 63, 68, 68, 60, 67, 58, 70, 67, 66)
groups <- rep(c('journal', 'blog'), each=10)
data_2 <- data.frame(groups, values)
# print the data to take a look
data_2
## groups values
## 1 journal 76
## 2 journal 67
## 3 journal 66
## 4 journal 72
## 5 journal 74
## 6 journal 64
## 7 journal 72
## 8 journal 70
## 9 journal 74
## 10 journal 78
## 11 blog 66
## 12 blog 63
## 13 blog 68
## 14 blog 68
## 15 blog 60
## 16 blog 67
## 17 blog 58
## 18 blog 70
## 19 blog 67
## 20 blog 66
boxplot()
.boxplot(values ~ groups, data = data_2)
describeBy()
from the psych
package.library(psych)
describeBy(values ~ groups, data = data_2, digits = 2)
##
## Descriptive statistics by group
## groups: blog
## vars n mean sd median trimmed mad min max range skew kurtosis se
## values 1 10 65.3 3.8 66.5 65.62 2.22 58 70 12 -0.72 -0.96 1.2
## ------------------------------------------------------------
## groups: journal
## vars n mean sd median trimmed mad min max range skew kurtosis se
## values 1 10 71.3 4.52 72 71.38 4.45 64 78 14 -0.19 -1.43 1.43
# Check for normality
tapply(data_2$values, data_2$groups, shapiro.test)
## $blog
##
## Shapiro-Wilk normality test
##
## data: X[[i]]
## W = 0.88842, p-value = 0.1627
##
##
## $journal
##
## Shapiro-Wilk normality test
##
## data: X[[i]]
## W = 0.96276, p-value = 0.8168
# Check for homogeneity of variance
library(car)
leveneTest(values ~ factor(groups), data = data_2)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 0.4171 0.5265
## 18
t.test(values ~ groups, data=data_2, var.equal=TRUE)
##
## Two Sample t-test
##
## data: values by groups
## t = -3.2112, df = 18, p-value = 0.004842
## alternative hypothesis: true difference in means between group blog and group journal is not equal to 0
## 95 percent confidence interval:
## -9.925471 -2.074529
## sample estimates:
## mean in group blog mean in group journal
## 65.3 71.3
Independent samples t-test showed that the frequency of the word ‘museum’ was significantly lower in blog articles in humanities than journal papers in humanities (t(18)=-3.21, p=0.004). Data was randomly collected from 10 blog article corpora and 10 journal paper corpora (blog: M=65.3, s.d.=3.8; journal:M=71.3, s.d.=4.52).
An experimental syntactician conducted an experiment on what’s called the that-trace phenomenon. 30 participants were presented with sentences such as (1) and (2). They were asked to judge the sentences’ grammaticallity on a 7 point scale (1 = totally ungrammatical; 7 = totally grammatical).
At the end, the researcher calculated the average score given to that sentences and no-that sentences for each participants (such that each person has a score in the ‘that’ condition and one score in the ‘no-that’ condition). The researcher now want to know whether there is a significant difference between the grammaticallity of ‘that’ sentences and ‘no-that’ sentences.
data <- read.csv('that-trace.csv', header = TRUE)
.data_3 <- read.csv('that-trace.csv', header = TRUE)
# print the data to take a look
data_3
## participant condition average_judgement
## 1 1 no_that 5.31
## 2 2 no_that 6.90
## 3 3 no_that 5.22
## 4 4 no_that 6.05
## 5 5 no_that 5.23
## 6 6 no_that 6.68
## 7 7 no_that 6.63
## 8 8 no_that 6.24
## 9 9 no_that 6.45
## 10 10 no_that 6.32
## 11 11 no_that 5.61
## 12 12 no_that 6.88
## 13 13 no_that 6.40
## 14 14 no_that 6.59
## 15 15 no_that 6.16
## 16 16 no_that 6.41
## 17 17 no_that 6.97
## 18 18 no_that 6.33
## 19 19 no_that 6.29
## 20 20 no_that 6.86
## 21 21 no_that 6.72
## 22 22 no_that 6.75
## 23 23 no_that 5.74
## 24 24 no_that 5.75
## 25 25 no_that 6.16
## 26 26 no_that 6.15
## 27 27 no_that 5.95
## 28 28 no_that 6.62
## 29 29 no_that 6.86
## 30 30 no_that 6.90
## 31 1 that 4.89
## 32 2 that 4.34
## 33 3 that 4.01
## 34 4 that 2.74
## 35 5 that 2.23
## 36 6 that 2.15
## 37 7 that 3.11
## 38 8 that 1.50
## 39 9 that 1.90
## 40 10 that 3.83
## 41 11 that 2.17
## 42 12 that 3.74
## 43 13 that 4.36
## 44 14 that 3.03
## 45 15 that 2.60
## 46 16 that 3.26
## 47 17 that 2.22
## 48 18 that 3.01
## 49 19 that 4.31
## 50 20 that 3.66
## 51 21 that 4.21
## 52 22 that 5.08
## 53 23 that 4.84
## 54 24 that 1.65
## 55 25 that 4.33
## 56 26 that 1.71
## 57 27 that 3.04
## 58 28 that 3.35
## 59 29 that 4.06
## 60 30 that 2.86
boxplot(average_judgement ~ condition, data = data_3)
library(psych)
describeBy(average_judgement ~ condition, data = data_3, digits = 2)
##
## Descriptive statistics by group
## condition: no_that
## vars n mean sd median trimmed mad min max range skew
## average_judgement 1 30 6.3 0.51 6.36 6.36 0.5 5.22 6.97 1.75 -0.67
## kurtosis se
## average_judgement -0.59 0.09
## ------------------------------------------------------------
## condition: that
## vars n mean sd median trimmed mad min max range skew
## average_judgement 1 30 3.27 1.04 3.18 3.27 1.42 1.5 5.08 3.58 -0.03
## kurtosis se
## average_judgement -1.23 0.19
# 1. Normality (of difference)
data_3_dif <- with(data_3, average_judgement[condition == 'no_that'] - average_judgement[condition == 'that'])
shapiro.test(data_3_dif)
##
## Shapiro-Wilk normality test
##
## data: data_3_dif
## W = 0.96302, p-value = 0.3692
t.test(average_judgement ~ condition, data = data_3, paired = TRUE)
##
## Paired t-test
##
## data: average_judgement by condition
## t = 14.754, df = 29, p-value = 5.143e-15
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
## 2.611120 3.451547
## sample estimates:
## mean difference
## 3.031333
Paired samples t-test showed that sentences such as (2) were judged significantly higher in acceptability than sentences such as (1) (t(29)=14.75, p<0.001) (that: M=3.27, s.d.=1.04; no that: M=6.3, s.d.=0.51).
During conversation, people often change the way they speak to sound more alike each other, this phenomenon is called phonetic convergence.
A researcher recorded some spontaneous conversations in English.3 The example is adapted from Kim, M., Horton, W. S., & Bradlow, A. R. (2011). Phonetic convergence in spontaneous conversations as a function of interlocutor language distance. Note that data used in this exercise are randomly generated fake data and conclusions may differ from the real experiment. These conversations were grouped by interlocutor language distance: Close (native Southern accent - native Southern accent); Intermediate (native Southern accent - native Northern accent); and Far (L2 English - L2 English (of different L1’s)). Each group had 15 conversations. All conversations were from different speakers.
Phonetic convergence is evaluated based on change in speach similarity: how close is speaker A’s speech to speaker B’s speech, at the end of the conversation compared with at the beginning of the conversation. In other words, the data are the convergence scores: similarity scores at the end - similarity scores at the beginning. The researcher is interested in whether interlocutor language distance affects phonetic convergence. In other words, the researcher wants to compare the mean convergence scores and see whether they differ from one another.
data <- read.csv('interlocutor.csv', header = TRUE)
.data_4 <- read.csv('interlocutor.csv', header = TRUE)
data_4$conversation <- as.factor(data_4$conversation)
# visualise our data
boxplot(score ~ group, data = data_4)
# get some descriptive statistics
library(psych)
describeBy(score ~ group, data = data_4)
##
## Descriptive statistics by group
## group: close
## vars n mean sd median trimmed mad min max range skew kurtosis se
## score 1 15 5.93 3.35 5 5.77 2.97 2 12 10 0.53 -1.32 0.86
## ------------------------------------------------------------
## group: far
## vars n mean sd median trimmed mad min max range skew kurtosis se
## score 1 15 0.27 3.2 0 0.31 2.97 -5 5 10 -0.15 -1.31 0.83
## ------------------------------------------------------------
## group: intermediate
## vars n mean sd median trimmed mad min max range skew kurtosis se
## score 1 15 5.4 3.6 6 5.46 2.97 -1 11 12 -0.18 -1.07 0.93
# Run the one-way between-subject ANOVA
data_4_result <- aov(score ~ group, data = data_4)
# take a look at our results using the summary() function
summary(data_4_result)
## Df Sum Sq Mean Sq F value Pr(>F)
## group 2 293.7 146.87 12.81 4.53e-05 ***
## Residuals 42 481.5 11.46
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Check for normality (of residuals)
# using Shapiro-Wilk test
data_4_residuals <- residuals(object = data_4_result)
shapiro.test(data_4_residuals)
##
## Shapiro-Wilk normality test
##
## data: data_4_residuals
## W = 0.9695, p-value = 0.2777
# Check for homogeneity of variance
library(car)
leveneTest(score ~ group, data = data_4)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 0.0053 0.9947
## 42
# Finally, run pair-wise t-tests with Bonferroni correction as post-hocs.
pairwise.t.test(data_4$score, data_4$group, p.adjust.method = "bonferroni")
##
## Pairwise comparisons using t tests with pooled SD
##
## data: data_4$score and data_4$group
##
## close far
## far 0.00012 -
## intermediate 1.00000 0.00047
##
## P value adjustment method: bonferroni
The results of one-way analysis of variance (ANOVA) with one between-subject factor Interlocutor Language Distance (ILD) (Close vs. Intermediate vs. Far) indicate a significant main effect of ILD (F(2, 42)=12.81, p<0.001). Follow-up pairwise t-tests using Bonferroni correction method revealed that the main effect was due to significant difference of the far distance group from the others (both p’s<0.001), while there was no significant difference between the Close group and the Intermediate group (Close: M=5.93, s.d.=3.35; Intermediate: M=5.4, s.d.=3.6; Far: M=0.27, s.d.=3.2).