top of page

Linear Regression

KEY:

ds = dataset you are currently using.

â€‹

DV = predicted variable

â€‹

IV = predictor variable

â€‹

XYXY = dummy name for a variable, matrix, or data frame into which you are moving information.

Note on standardized values

As a default, R provides unstandardized regression coefficients. This is good for predictive modeling, however, one may want the standardized coefficients (beta weights) for portioning variance.

â€‹

This is accomplished via "scale(Variable)". If you want unstandardized coefficients, simply remove "scale()" from the examples below.

â€‹

If scale() is omitted, standardized variables can alternatively be obtained using the command lm.beta(XYXY). You will need to load the QuantPsych package in order to do this. However, your data includes categorical variables, this will throw an error. Also note, lm.beta does not seem to handle interactions well.

â€‹

â€‹

Regression with two predictors.

_____________________________

XYXY <- lm(scale(DV) ~ scale(IV1) + scale(IV2), data=ds)
summary(XYXY)

_____________________________

â€‹

â€‹

Regression with interaction and visualization.

Version 1 of test:

_____________________________

XYXY <- lm(scale(DV) ~ scale(IV1) * scale(IV2), data=ds)
summary(XYXY)

_____________________________

â€‹

â€‹

Version 2 of test:

_____________________________

XYXY <- lm(scale(DV) ~ scale(IV1) + scale(IV2) + scale(IV1):scale(IV2), data=ds)
summary(XYXY)

_____________________________

â€‹

â€‹

If the interaction is significant, visualization can be critical to it's interpretation. An easy way to plot your data is to use the "interact_plot" command that is available from the "interactions" package.

*IV1 will be plotted on the X axis

*IV1 will be divided by 3 levels of IV2

*If you prefer IV1 to be divided by 1SD of IV2, then omit modx.values="terciles"

*If IV2 has discrete levels, then: modx.values = c("Level 1", "Level, 2")

_____________________________

library(interactions)
interact_plot(XYXY, pred = IV1, modx = IV2, modx.values = "terciles")

_____________________________

â€‹

bottom of page