#####R Script for Week 7--Dummy Variable Regression # dichtomous factor library(car) fix(Davis) attach(Davis) sex class(sex) contrasts(sex) # dummy regressors for sex scatterplot(weight ~ repwt | sex, smooth=FALSE) mod.davis.1 <- lm(weight ~ repwt + sex) # additive model summary(mod.davis.1) mod.davis.2 <- lm(weight ~ repwt*sex) # model with interaction summary(mod.davis.2) linear.hypothesis(mod.davis.2, c("sexM", "repwt:sexM")) # slopes AND intercepts # the same for M and F detach(Davis) # polytomous factor Prestige <- na.omit(Prestige) attach(Prestige) # treatment of factors in R type class(type) contrasts(type) # dummy regressors type <- factor(type, levels=c("bc", "wc", "prof")) # change order of levels contrasts(type) # additive dummy-regression model mod.prestige.1 <- lm(prestige ~ income + education + type) summary(mod.prestige.1) mod.prestige.0 <- lm(prestige ~ income + education) anova(mod.prestige.0, mod.prestige.1) # incremental F-test for type Anova(mod.prestige.1) # "type II" tests linear.hypothesis(mod.prestige.1, c("typewc", "typeprof")) # dummy regression with interactions mod.prestige.2 <- lm(prestige ~ income + education + type + income:type + education:type) mod.prestige.2 <- lm(prestige ~ income*type + education*type) # equivalent mod.prestige.2 <- lm(prestige ~ type*(income + education)) # equivalent summary(mod.prestige.2) Anova(mod.prestige.2) # type-II tests # "effect" plots library(effects) # effects package must be installed plot(all.effects(mod.prestige.2), ask=FALSE) plot(all.effects(mod.prestige.2), multiline=TRUE, ask=FALSE)