要使用R代码回答这些问题,请使用以下命令:
1.如何测试斜率之间的差异?
答案:通过按物种进行的Petal.Width相互作用检查ANOVA p值,然后使用lsmeans :: lstrends比较斜率,如下所示。
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.如何测试残差之间的差异?
如果我理解这个问题,可以将Pearson相关性与Fisher变换(也称为“ Fisher的r至z”)进行比较,如下所示。
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.介绍这些比较的一种简单有效的方法是什么?
“我们使用线性回归比较了每种物种的萼片长度与花瓣宽度之间的关系。对于I. Setosa(B = 0.9),I。Versicolor(B = 1.4),也不是维珍妮卡岛(B = 0.6); F(2,144)= 1.6,p = 0.19。Fisher的r-z-z比较表明,I。Setosa(r = 0.28)的皮尔逊相关系数为显着低于(I = Versicolor)(p = 0.02)(r = 0.55)。同样,I。Virginica(r = 0.28)的相关性也较I. Versicolor显着弱(p = 0.02)。。”
最后,始终将结果可视化!
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")