To answer these questions with R code, use the following:
1. How can I test the difference between slopes?
Answer: Examine the ANOVA p-value from the interaction of Petal.Width by Species, then compare the slopes using lsmeans::lstrends, as follows.
library(lsmeans)
m.interaction <- lm(Sepal.Length ~ Petal.Width*Species, data = iris)
anova(m.interaction)
Analysis of Variance Table
Response: Sepal.Length
Df Sum Sq Mean Sq F value Pr(>F)
Petal.Width 1 68.353 68.353 298.0784 <2e-16 ***
Species 2 0.035 0.017 0.0754 0.9274
Petal.Width:Species 2 0.759 0.380 1.6552 0.1947
Residuals 144 33.021 0.229
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# Obtain slopes
m.interaction$coefficients
m.lst <- lstrends(m.interaction, "Species", var="Petal.Width")
Species Petal.Width.trend SE df lower.CL upper.CL
setosa 0.9301727 0.6491360 144 -0.3528933 2.213239
versicolor 1.4263647 0.3459350 144 0.7425981 2.110131
virginica 0.6508306 0.2490791 144 0.1585071 1.143154
# Compare slopes
pairs(m.lst)
contrast estimate SE df t.ratio p.value
setosa - versicolor -0.4961919 0.7355601 144 -0.675 0.7786
setosa - virginica 0.2793421 0.6952826 144 0.402 0.9149
versicolor - virginica 0.7755341 0.4262762 144 1.819 0.1669
2. How can I test the difference between residual variances?
If I understand the question, you can compare Pearson correlations with a Fisher transform, also called a "Fisher's r-to-z", as follows.
library(psych)
library(data.table)
iris <- as.data.table(iris)
# Calculate Pearson's R
m.correlations <- iris[, cor(Sepal.Length, Petal.Width), by = Species]
m.correlations
# Compare R values with Fisher's R to Z
paired.r(m.correlations[Species=="setosa", V1], m.correlations[Species=="versicolor", V1],
n = iris[Species %in% c("setosa", "versicolor"), .N])
paired.r(m.correlations[Species=="setosa", V1], m.correlations[Species=="virginica", V1],
n = iris[Species %in% c("setosa", "virginica"), .N])
paired.r(m.correlations[Species=="virginica", V1], m.correlations[Species=="versicolor", V1],
n = iris[Species %in% c("virginica", "versicolor"), .N])
3. What is a simple, effective way to present these comparisons?
"We used linear regression to compare the relationship of Sepal Length to Petal Width for each Species. We did not find a significant interaction in the relationships of Sepal Length to Petal Width for I. Setosa (B = 0.9), I. Versicolor (B = 1.4), nor I. Virginica (B = 0.6); F (2, 144) = 1.6, p = 0.19. A Fisher's r-to-z comparison indicated that the Pearson correlation for I. Setosa (r = 0.28) was significantly lower (p = 0.02) than I. Versicolor (r = 0.55). Similarly, the correlation for I. Virginica (r = 0.28) was significantly weaker (p = 0.02) than the one observed for I. Versicolor."
Finally, always visualize your results!
plotly_interaction <- function(data, x, y, category, colors = col2rgb(viridis(nlevels(as.factor(data[[category]])))), ...) {
# Create Plotly scatter plot of x vs y, with separate lines for each level of the categorical variable.
# In other words, create an interaction scatter plot.
# The "colors" must be supplied in a RGB triplet, as produced by col2rgb().
require(plotly)
require(viridis)
require(broom)
groups <- unique(data[[category]])
p <- plot_ly(...)
for (i in 1:length(groups)) {
groupData = data[which(data[[category]]==groups[[i]]), ]
p <- add_lines(p, data = groupData,
y = fitted(lm(data = groupData, groupData[[y]] ~ groupData[[x]])),
x = groupData[[x]],
line = list(color = paste('rgb', '(', paste(colors[, i], collapse = ", "), ')')),
name = groups[[i]],
showlegend = FALSE)
p <- add_ribbons(p, data = augment(lm(data = groupData, groupData[[y]] ~ groupData[[x]])),
y = groupData[[y]],
x = groupData[[x]],
ymin = ~.fitted - 1.96 * .se.fit,
ymax = ~.fitted + 1.96 * .se.fit,
line = list(color = paste('rgba','(', paste(colors[, i], collapse = ", "), ', 0.05)')),
fillcolor = paste('rgba', '(', paste(colors[, i], collapse = ", "), ', 0.1)'),
showlegend = FALSE)
p <- add_markers(p, data = groupData,
x = groupData[[x]],
y = groupData[[y]],
symbol = groupData[[category]],
marker = list(color=paste('rgb','(', paste(colors[, i], collapse = ", "))))
}
p <- layout(p, xaxis = list(title = x), yaxis = list(title = y))
return(p)
}
plotly_interaction(iris, "Sepal.Length", "Petal.Width", "Species")