#################################################################################### #This is a file with executable R code for chapter 6 of Natalia Levshina's (2015) #How to Do Linguistics with R. Amsterdam/Philadelphia: John Benjamins. #################################################################################### ###Section 6.2 ##Main text install.packages(c("ggplot2", "energy", "car")) library(Rling); library(ggplot2); library(energy); library(car) data(ldt) attach(ldt) summary(Length) summary(Mean_RT) plot(Length, Mean_RT, main = "Scatter plot of word length and mean reaction times") plot(Mean_RT ~ Length, main = "Scatter plot of word length and mean reaction times") m <- lm(Mean_RT ~ Length) abline(m) cor(Mean_RT, Length) head(Mean_RT) head(fitted(m)) head(residuals(m)) Mean_RT_1 <- Mean_RT[Mean_RT < 1200] length(Mean_RT_1) Length_1 <- Length[Mean_RT < 1200] length(Length_1) m1 <- lm(Mean_RT_1 ~ Length_1) abline(m1, lty = 2) cor(Mean_RT_1, Length_1) mvnorm.etest(cbind(Length_1, Mean_RT_1)) ncvTest(m1) durbinWatsonTest(m1) cor.test(Length_1, Mean_RT_1, alternative = "greater") detach(ldt) ##Boxes with additional information ggplot(ldt, aes(x = Length, y = Mean_RT)) + geom_point(shape = 1, size = 3) + stat_smooth(method = lm) x <- 1:10 x y <- x*2 y[5] <- NA y cor(x, y) cor(x, y, use = "complete") ###Section 6.3 ##Main text lex <- c(47, 89, 131, 186, 245, 284, 362, 444, 553, 627) gram <- c(0, 2, 1, 3, 5, 9, 7, 16, 25, 34) plot(gram ~ lex, main = "Vocabulary size and grammatical complexity", xlab = "Productive vocabulary size", ylab = "Grammatical complexity score") lines(lowess(gram ~ lex)) cor(gram, lex, method = "spearman") cor(rank(gram), rank(lex)) cor(gram, lex, method = "kendall") cor.test(gram, lex, method = "spearman", alternative = "greater") cor.test(gram, lex, method = "kendall", alternative = "greater") ###Section 6.4 ##Main text install.packages("corrgram") library(Rling); library(corrgram) data(ldt) cor(ldt, method = "spearman") corrgram(ldt, lower.panel = panel.shade, upper.panel = panel.pie, cor.method = "spearman") ldt1 <- data.frame(Length = ldt$Length, LogFreq = log1p(ldt$Freq), Mean_RT = ldt$Mean_RT) head(ldt1) corrgram(ldt1, lower.panel = panel.ellipse, upper.panel = panel.pts)