7.9 Summary of important R code

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)

    • Estimates a regression model using least squares.
  • summary(MODELNAME)

    • Provides parameter estimates and R-squared (used heavily in Chapter ?? as well).
  • par(mfrow=c(2, 2)); plot(MODELNAME)

    • Provides four regression diagnostic plots in one plot.
  • 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)

    • Creates a transformed variable called log.y – change this to be more specific to your “\(y\)” or “\(x\)”.
  • predict(MODELNAME, se.fit=T)

    • Provides fitted values for all observed \(x\text{'s}\) with SEs for the mean.
  • predict(MODELNAME, newdata=tibble(x = XNEW), interval=“confidence”)

    • Provides fitted value for a specific \(x\) (XNEW) with CI for the mean. Replace x with name of explanatory variable.
  • predict(MODELNAME, newdata=tibble(x = XNEW), interval=“prediction”)

    • Provides fitted value for a specific \(x\) (XNEW) with PI for a new observation. Replace x with name of explanatory variable.
  • qt(0.975, df=n - 2)

    • Gets the \(t^*\) multiplier for making a 95% confidence or prediction interval with \(n-2\) replaced by the sample size – 2.