require(haven) #For importing from SPSS: read_sav()
## Loading required package: haven
require(tidyverse)
## Loading required package: tidyverse
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Warning: package 'ggplot2' was built under R version 3.3.2
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag():    dplyr, stats
dat <- read_sav(file = "C:/Users/Jon/Documents/Classes/quant methods I (6500 5500)/data/20 - continuous & categorical interactions, part 2/20 - mood, sugar, hypo & sex.sav")

When centering using scale(), be sure to set scale = F, otherwise you will get z-values

dat$sugar_c <- scale(dat$sugar, scale = F)

Lets name our factor levels to make things easier to keep straight

dat$hypoglyc <- factor(dat$hypoglyc, labels = c("Not Hypo", "Hypo")) 
dat$sex <- factor(dat$sex, labels = c("Male", "Female"))

By default, factors are dummy coded

contrasts(dat$hypoglyc) 
##          Hypo
## Not Hypo    0
## Hypo        1
summary(lm(mood ~ hypoglyc * sugar_c, dat))
## 
## Call:
## lm(formula = mood ~ hypoglyc * sugar_c, data = dat)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.8450 -0.8047 -0.2115  0.8587  4.0387 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           6.39024    0.23329  27.392  < 2e-16 ***
## hypoglycHypo         -0.62957    0.32365  -1.945   0.0547 .  
## sugar_c              -0.04155    0.01611  -2.579   0.0114 *  
## hypoglycHypo:sugar_c  0.10983    0.02057   5.340 6.23e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.472 on 96 degrees of freedom
## Multiple R-squared:  0.2812, Adjusted R-squared:  0.2587 
## F-statistic: 12.52 on 3 and 96 DF,  p-value: 5.619e-07

As an aside, note that when R gets an interaction term in the formula (Using *), it automatically includes all the lower order terms. THe following are equivalent:

summary(lm(mood ~ hypoglyc * sugar_c, dat))
## 
## Call:
## lm(formula = mood ~ hypoglyc * sugar_c, data = dat)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.8450 -0.8047 -0.2115  0.8587  4.0387 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           6.39024    0.23329  27.392  < 2e-16 ***
## hypoglycHypo         -0.62957    0.32365  -1.945   0.0547 .  
## sugar_c              -0.04155    0.01611  -2.579   0.0114 *  
## hypoglycHypo:sugar_c  0.10983    0.02057   5.340 6.23e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.472 on 96 degrees of freedom
## Multiple R-squared:  0.2812, Adjusted R-squared:  0.2587 
## F-statistic: 12.52 on 3 and 96 DF,  p-value: 5.619e-07
summary(lm(mood ~ hypoglyc + sugar_c + hypoglyc*sugar_c, dat))
## 
## Call:
## lm(formula = mood ~ hypoglyc + sugar_c + hypoglyc * sugar_c, 
##     data = dat)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.8450 -0.8047 -0.2115  0.8587  4.0387 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           6.39024    0.23329  27.392  < 2e-16 ***
## hypoglycHypo         -0.62957    0.32365  -1.945   0.0547 .  
## sugar_c              -0.04155    0.01611  -2.579   0.0114 *  
## hypoglycHypo:sugar_c  0.10983    0.02057   5.340 6.23e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.472 on 96 degrees of freedom
## Multiple R-squared:  0.2812, Adjusted R-squared:  0.2587 
## F-statistic: 12.52 on 3 and 96 DF,  p-value: 5.619e-07
summary(lm(mood ~ (hypoglyc + sugar_c)^2, dat)) #This can be useful if you have 3 terms but want all the 2 way interactions.  ^2 gives 2 way, ^3 gives 3 way, ^4 gives 4 way, etc.
## 
## Call:
## lm(formula = mood ~ (hypoglyc + sugar_c)^2, data = dat)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.8450 -0.8047 -0.2115  0.8587  4.0387 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           6.39024    0.23329  27.392  < 2e-16 ***
## hypoglycHypo         -0.62957    0.32365  -1.945   0.0547 .  
## sugar_c              -0.04155    0.01611  -2.579   0.0114 *  
## hypoglycHypo:sugar_c  0.10983    0.02057   5.340 6.23e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.472 on 96 degrees of freedom
## Multiple R-squared:  0.2812, Adjusted R-squared:  0.2587 
## F-statistic: 12.52 on 3 and 96 DF,  p-value: 5.619e-07

There are a number of pre-set options. See: ?contr.treatment

contrasts(dat$hypoglyc) <- contr.sum #contr.sum is effect coding.  It will always use the last level as the reference level.  
contrasts(dat$hypoglyc)
##          [,1]
## Not Hypo    1
## Hypo       -1
summary(lm(mood ~ hypoglyc * sugar_c, dat))
## 
## Call:
## lm(formula = mood ~ hypoglyc * sugar_c, data = dat)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.8450 -0.8047 -0.2115  0.8587  4.0387 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        6.07546    0.16183  37.543  < 2e-16 ***
## hypoglyc1          0.31479    0.16183   1.945   0.0547 .  
## sugar_c            0.01336    0.01028   1.300   0.1969    
## hypoglyc1:sugar_c -0.05492    0.01028  -5.340 6.23e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.472 on 96 degrees of freedom
## Multiple R-squared:  0.2812, Adjusted R-squared:  0.2587 
## F-statistic: 12.52 on 3 and 96 DF,  p-value: 5.619e-07
contrasts(dat$hypoglyc) <- c(-1, 1) #If you want a different reference level, it must be done manually
contrasts(dat$hypoglyc)
##          [,1]
## Not Hypo   -1
## Hypo        1
summary(lm(mood ~ hypoglyc * sugar_c, dat))
## 
## Call:
## lm(formula = mood ~ hypoglyc * sugar_c, data = dat)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.8450 -0.8047 -0.2115  0.8587  4.0387 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        6.07546    0.16183  37.543  < 2e-16 ***
## hypoglyc1         -0.31479    0.16183  -1.945   0.0547 .  
## sugar_c            0.01336    0.01028   1.300   0.1969    
## hypoglyc1:sugar_c  0.05492    0.01028   5.340 6.23e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.472 on 96 degrees of freedom
## Multiple R-squared:  0.2812, Adjusted R-squared:  0.2587 
## F-statistic: 12.52 on 3 and 96 DF,  p-value: 5.619e-07
contrasts(dat$hypoglyc) <- contr.treatment #This is how we get back to dummy coding. The first level is always the base.
contrasts(dat$hypoglyc)
##          2
## Not Hypo 0
## Hypo     1
summary(lm(mood ~ hypoglyc * sugar_c, dat))
## 
## Call:
## lm(formula = mood ~ hypoglyc * sugar_c, data = dat)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.8450 -0.8047 -0.2115  0.8587  4.0387 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        6.39024    0.23329  27.392  < 2e-16 ***
## hypoglyc2         -0.62957    0.32365  -1.945   0.0547 .  
## sugar_c           -0.04155    0.01611  -2.579   0.0114 *  
## hypoglyc2:sugar_c  0.10983    0.02057   5.340 6.23e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.472 on 96 degrees of freedom
## Multiple R-squared:  0.2812, Adjusted R-squared:  0.2587 
## F-statistic: 12.52 on 3 and 96 DF,  p-value: 5.619e-07
# Alternate method

Using individual lines as above permanently sets the contrast for the variable. This can be good and bad. For example, if you set it to effect coding and then skip down to run a line past a point where you changed it to dummy, but didn’t execute the dummy set line, you can forget that you still have it effect coded.

A good alternative is to set the contrast with the lm call:

summary(lm_dummy <- lm(mood ~ hypoglyc * sugar_c, contrasts = list(hypoglyc = contr.treatment), dat))
## 
## Call:
## lm(formula = mood ~ hypoglyc * sugar_c, data = dat, contrasts = list(hypoglyc = contr.treatment))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.8450 -0.8047 -0.2115  0.8587  4.0387 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        6.39024    0.23329  27.392  < 2e-16 ***
## hypoglyc2         -0.62957    0.32365  -1.945   0.0547 .  
## sugar_c           -0.04155    0.01611  -2.579   0.0114 *  
## hypoglyc2:sugar_c  0.10983    0.02057   5.340 6.23e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.472 on 96 degrees of freedom
## Multiple R-squared:  0.2812, Adjusted R-squared:  0.2587 
## F-statistic: 12.52 on 3 and 96 DF,  p-value: 5.619e-07
summary(lm_dummy <- lm(mood ~ hypoglyc * sugar_c, contrasts = list(hypoglyc = contr.sum), dat))
## 
## Call:
## lm(formula = mood ~ hypoglyc * sugar_c, data = dat, contrasts = list(hypoglyc = contr.sum))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.8450 -0.8047 -0.2115  0.8587  4.0387 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        6.07546    0.16183  37.543  < 2e-16 ***
## hypoglyc1          0.31479    0.16183   1.945   0.0547 .  
## sugar_c            0.01336    0.01028   1.300   0.1969    
## hypoglyc1:sugar_c -0.05492    0.01028  -5.340 6.23e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.472 on 96 degrees of freedom
## Multiple R-squared:  0.2812, Adjusted R-squared:  0.2587 
## F-statistic: 12.52 on 3 and 96 DF,  p-value: 5.619e-07
summary(lm_dummy <- lm(mood ~ hypoglyc * sugar_c, contrasts = list(hypoglyc = c(1, -1)), dat))
## 
## Call:
## lm(formula = mood ~ hypoglyc * sugar_c, data = dat, contrasts = list(hypoglyc = c(1, 
##     -1)))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.8450 -0.8047 -0.2115  0.8587  4.0387 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        6.07546    0.16183  37.543  < 2e-16 ***
## hypoglyc1          0.31479    0.16183   1.945   0.0547 .  
## sugar_c            0.01336    0.01028   1.300   0.1969    
## hypoglyc1:sugar_c -0.05492    0.01028  -5.340 6.23e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.472 on 96 degrees of freedom
## Multiple R-squared:  0.2812, Adjusted R-squared:  0.2587 
## F-statistic: 12.52 on 3 and 96 DF,  p-value: 5.619e-07
summary(lm_dummy <- lm(mood ~ hypoglyc * sugar_c, contrasts = list(hypoglyc = c(.5, -.5)), dat))
## 
## Call:
## lm(formula = mood ~ hypoglyc * sugar_c, data = dat, contrasts = list(hypoglyc = c(0.5, 
##     -0.5)))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.8450 -0.8047 -0.2115  0.8587  4.0387 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        6.07546    0.16183  37.543  < 2e-16 ***
## hypoglyc1          0.62957    0.32365   1.945   0.0547 .  
## sugar_c            0.01336    0.01028   1.300   0.1969    
## hypoglyc1:sugar_c -0.10983    0.02057  -5.340 6.23e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.472 on 96 degrees of freedom
## Multiple R-squared:  0.2812, Adjusted R-squared:  0.2587 
## F-statistic: 12.52 on 3 and 96 DF,  p-value: 5.619e-07