#################################################################################### #This is a file with executable R code for chapter 12 of Natalia Levshina's (2015) #How to Do Linguistics with R. Amsterdam/Philadelphia: John Benjamins. #################################################################################### ###Section 12.2 ##Main text install.packages(c("rms", "visreg", "car")) # if you haven't installed them yet library(Rling); library(rms); library(visreg); library(car) data(doenLaten) head(doenLaten) str(doenLaten) summary(doenLaten$Aux) m.lrm <- lrm(Aux ~ Causation + EPTrans + Country, data = doenLaten) m.lrm levels(doenLaten$Aux) exp(1.8631) log(6.443681) m.glm <- glm(Aux ~ Causation + EPTrans + Country, data = doenLaten, family = binomial) confint(m.glm) exp(confint(m.glm)) exp(2.5716) exp(2.5716)/(1 + exp(2.5716)) log(0.9290113/(1 - 0.9290113)) predict(m.lrm, type = "fitted")[27] m0.glm <- glm(Aux ~ 1, data = doenLaten, family = binomial) m.fw <- step(m0.glm, direction = "forward", scope = ~ Causation + EPTrans + Country) m.bw <- step(m.glm, direction = "backward") m.glm1 <- glm(Aux ~ EPTrans + Country, data = doenLaten, family = binomial) anova(m.glm1, m.glm, test = "Chisq") drop1(m.glm, test = "Chisq") m.glm.int <- glm(Aux ~ Causation + EPTrans*Country, data = doenLaten, family = binomial) anova(m.glm, m.glm.int, test = "Chisq") summary(m.glm.int) visreg(m.glm.int, "EPTrans", by = "Country") doenLaten[c(142, 356, 358, 381),] rms::vif(m.lrm) m.test <- lrm(Aux~Causation + EPTrans + EPTrans1 + Country, data = doenLaten) m.test rms::vif(m.test) m.lrm1 <- lrm(Aux ~ Causation + EPTrans + Country, data = doenLaten, x = T, y = T) validate(m.lrm1, B = 200) s <- sample(455, 100) d.small <- doenLaten[s,] m.lrm1.small <- lrm(Aux ~ Causation + EPTrans + Country, data = d.small, x = T, y = T) validate(m.lrm1.small, B = 200) influencePlot(m.glm, id.method = "identify") ##Boxes with additional information table(doenLaten$Aux, doenLaten$Country) m.Country <- lrm(Aux ~ Country, data = doenLaten) m.Country exp(0.7528)