The main components of the R code used in this chapter follow with the
components to modify in lighter and/or ALL CAPS text where y is a response variable, x is an
explanatory variable, and the data are in DATASETNAME.
scatterplot(y~x, data=DATASETNAME, smooth=F)
Requires the car package.
Provides a scatterplot with a regression line.
Turn on smooth=T to add a smoothing line to help detect
nonlinear relationships.
MODELNAME <- lm(y~
x, data=DATASETNAME)
summary(MODELNAME)
par(mfrow=c(2, 2)); plot(MODELNAME)
confint(MODELNAME, level=0.95)
Provides 95% confidence intervals for the regression model coefficients.
Change level if you want other confidence levels.
plot(allEffects(MODELNAME))
Requires the effects package.
Provides a term-plot of the estimated regression line with 95% confidence interval for the mean.
DATASETNAME$log.y <- log(DATASETNAME$y)
predict(MODELNAME, se.fit=T)
predict(MODELNAME, newdata=tibble(x = XNEW), interval=“confidence”)
x with name of explanatory variable.predict(MODELNAME, newdata=tibble(x = XNEW), interval=“prediction”)
x with name of explanatory variable.qt(0.975, df=n - 2)