The main components of R code used in this chapter follow with components to modify in lighter and/or ALL CAPS text, remembering that any R packages mentioned need to be installed and loaded for this code to have a chance of working:
MODELNAME <- lm(Y~X, data=DATASETNAME)
Probably the most frequently used command in R.
Here it is used to fit the reference-coded One-Way ANOVA model with Y as the response variable and X as the grouping variable, storing the estimated model object in MODELNAME. Remember that X should be defined as a factor variable.
MODELNAME <- lm(Y~X-1, data=DATASETNAME)
summary(MODELNAME)
anova(MODELNAME)
Generates the ANOVA table but must only be run on the reference-coded version of the model.
Results are incorrect if run on the cell-means model since the reduced model under the null is that the mean of all the observations is 0!
pf(FSTATISTIC, df1=NUMDF, df2=DENOMDF, lower.tail=F)
par(mfrow=c(2,2)); plot(MODELNAME)
plot(allEffects(MODELNAME))
Requires the effects
package be loaded.
Plots the estimated model component.
Tm2 <- glht(MODELNAME, linfct=mcp(X=“Tukey”)); confint(Tm2); plot(Tm2); summary(Tm2); cld(Tm2)
Requires the multcomp
package to be installed and loaded.
Can only be run on the reference-coded version of the model.
Generates the text output and plot for Tukey’s HSD as well as the compact letter display information.