Interaction between a continuous variable and a 2 category variable (comparison of slopes)
Here we examine the special case where we wish to test the interaction between a continuous variable (x) and a two category variable (group). This is equivalent to comparing the slopes of x between the two groups.
This can be formulated as a t-statistic of the regression estimate for the x by group interaction (x by group product) term (fitted in a regression also containing x and group). We usually code the group as a dichotomous 0-1 variable. The t-statistic for the regression coefficient of the x by group interaction is used to assess if the strength of the relationship between x and y differs between the two groups.
Cribari-Neto (2004) recommends using a variant of the t-statistic which he calls the HC4-based quasi-t test which adjusts the standard error of the interaction regression coefficient. This may be computed using this spreadsheet. This spreadsheet also computes an extension of the HC4-based quasi-t recommended by Ng and Wilcox (2010) which is more robust than both the classic t-test (also computed by the spreadsheet) and the HC4 quasi-t test to departures from assumptions underlying the computing of the variance of the regression estimates - specifically the assumption that the regression model is equally precise in both groups for all values of x (homoscedasticity). Ng and Wilcox call their estimate the HC4-based wild bootstrap quasi-t test which additionally accounts for sampling variability by sampling a large number of times (with replacement) from the current sample to assess the variation in the t value (the 'bootstrapping' bit!) It outputs a single p-value for the x by group effect. Details of how to compute this using the above spreadsheet using complete cases are given here.
The asymptotic HC4 quasi-t test, its bootstrap version and the classic asymptotic t-test for the x by group interaction may also be computed using R 2.10 and later. To compute the HC4 quasi-t and classic t tests you will need to add-in the libraries 'foreign' and 'sandwich' which contain the macros you will need to evaluate the variances of the x by group interaction term (if you have not installed these already). An example SPSS file containing the data used below is given here. Note: You will need to change the file location as necessary, to the one on your PC, in the read.spss() command example given below. The data for this example may be found in this SPSS file.
The following syntax produces tval (the quasi-t-value for the asymptotic HC4 estimate of the group by x interaction), its degrees of freedom (df) and its 2-sided p-value (pout).
library(foreign) library(sandwich) dat <- read.spss("C:\\Documents and Settings\\peterw\\Desktop\\My Documents\\My Documents2\\BARNEY MODERATION + R HC4 CODE\\HC4 test data on WIKI.sav") attach(dat) dat <- na.omit(dat) vc <- vcovHC(fm,type="HC4") tval <- fm$coeff/sqrt(vc[4,4]) df <- length(dat$A)-4 pout <- 2*pt(-abs(tval),df)
The classic asymptotic t-test may be outputted using
vc <- vcov(fm) tval <- fm$coeff/sqrt(vc[4,4]) df <- length(dat$A)-4 pout <- 2*pt(-abs(tval),df)
The HC4 bootstrap version, which produces a p-value for the HC4 quasi-t based upon replacement sampling, may be computed by installing the reg2ci macro. Details with a worked example are in the appendix of Ng and Wilcox. The classic t-test, HC4 quasi-t and HC4 wild bootstrap correspond to estimates 2.1, 2.2 and 2.3 respectively of Ng and Wilcox's paper.
Cribari-Neto, F. (2004). Asymptotic inference under heteroscedasticity of unknown form. Computational Statistics and Data Analysis 45 215-233.
Ng, M. and Wilcox, R.R. (2010). Comparing the regression slopes of independent groups. British Journal of Mathematical and Statistical Psychology 63 319-340.