#################################################################################### #This is a file with executable R code for chapter 9 of Natalia Levshina's (2015) #How to Do Linguistics with R. Amsterdam/Philadelphia: John Benjamins. #################################################################################### ###Section 9.2 ##Main text install.packages(c("ggplot2", "reshape", "vcd")) library(ggplot2); library(reshape); library(vcd) over <- cbind(c(22, 4), c(5, 12)) over over <- rbind(c(22, 5), c(4, 12)) over rownames(over) <- c("met", "nonmet") colnames(over) <- c("acad","conv") over barplot(over, col = c("grey20", "grey80"), main = "Bar plot of (non-)metaphoric uses of over", xlab = "Register", ylab = "Frequency") legend("topright", fill = c("grey20", "grey80"), c("metaphoric", "non-metaphoric")) barplot(over, beside = TRUE, col = c("grey20", "grey80"), main = "Bar plot of (non-)metaphoric uses of over", xlab = "Register", ylab = "Frequency") legend("topright", fill=c("grey20", "grey80"), c("metaphoric", "non-metaphoric")) prop.table(over) prop.table(over, 1) prop.table(over, 2) 22/4 5/12 assocstats(over) over1 <- over[, c(2,1)] over1 assocstats(over1) (5/12)/(22/4) 1/13.2 ((22 + 4)*(22 + 5))/(22 + 4 + 5 + 12) chisq.test(over)\$expected prop.table(chisq.test(over)\$expected, 2) prop.table(chisq.test(over)\$expected, 1) chisq.test(over) ##Boxes with additional information f1 <- factor(c(rep("A", 10), rep("B", 20))) f1 f2 <- factor(c(rep("X", 15), rep("Y", 15))) f2 yourTable <- table(f1, f2) yourTable yourTable <- xtabs(~ f1 + f2) yourTable over.df <- melt(over) colnames(over.df) <- c("Metaphoricity", "Register", "Frequency") over.df ggplot(over.df, aes(x = Register, y = Frequency, fill = Metaphoricity)) + geom_bar(stat = "identity", colour = "black") + scale_fill_grey() ggplot(over.df, aes(x = Register, y = Frequency, fill = Metaphoricity)) + geom_bar(stat = "identity", colour = "black", position = "dodge") + scale_fill_grey() test <- cbind(c(12, 2), c(4, 6)) test chisq.test(test) chisq.test(test)\$expected fisher.test(test) ###Section 9.3 ##Main text install.packages("vcd") # if you have not installed it yet library(vcd) see.m <- c(44, 48, 27, 17) see.nm <- c(26, 135, 98, 19) see.reg <- rbind(see.m, see.nm) colnames(see.reg) <- c("aca", "conv", "fic", "news") see.reg barplot(see.reg, beside = TRUE, main = "Barplot of (non-)metaphoric uses of see", xlab = "Registers", ylab = "Frequencies") legend("topright", fill = c("grey30", "grey90"), c("metaphoric", "non-metaphoric")) prop.table(see.reg, 2) assocstats(see.reg) chisq.test(see.reg) chisq.test(see.reg)\$observed chisq.test(see.reg)\$expected chisq.test(see.reg)\$residuals (44 - 22.99517)/sqrt(22.99517) mosaic(see.reg, shade = TRUE, varnames = FALSE) assoc(see.reg, shade = TRUE, varnames = FALSE)