简体   繁体   English

是否有 R function 来执行 Ancova 事后测试以检查回归斜率的同质性

[英]Is there a R function to perform an Ancova post-hoc test to check homogeneity of regression slopes

After adjust some linear models I want, first, to test for homogeneity of regression slopes.在调整了一些线性模型后,我首先要测试回归斜率的同质性。 The second step, and here is my doubt, I want to employ a post-hoc test to compare slopes two by two.第二步,这是我的疑问,我想使用事后测试来两个两个比较斜率

Here goes an example modified from https://www.datanovia.com/en/lessons/ancova-in-r/这是一个从https://www.datanovia.com/en/lessons/ancova-in-r/修改的示例

get data获取数据

data("anxiety", package = "datarium")
anxiety <- anxiety[,c("id","group","t1","t3")]
names(anxiety)[c(3,4)] <- c("pretest","posttest")

plot regression lines plot 回归线

ggscatter(anxiety,x="pretest",y="posttest",color="group",add="reg.line")+
    stat_regline_equation(aes(label=paste(..eq.label.., ..rr.label.., sep = "~~~~"),color = group))

check homogeneity of regression slopes检查回归斜率的同质性

anova_test(anxiety,posttest~group*pretest)

Here we can see a not statistically significant p-value of 4.15e-01在这里,我们可以看到统计上不显着的 p 值 4.15e-01

The post-hoc test emmeans_test perform pairwise comparisons to identify which groups are different.事后测试 emmeans_test 执行成对比较以确定哪些不同。 Nevertheless I want to employ a multiple-comparison procedure to determine which B 's ( slopes ) are different from which others.不过,我想采用多重比较程序来确定哪些B斜率)与其他哪些不同。

Is there a function for this?这个有 function 吗? Thanks in advance.提前致谢。

After reading and search more I prepared an example of the analysis I was trying to do.在阅读和搜索更多之后,我准备了一个我试图做的分析的例子。 I hope it is useful.我希望它是有用的。 An important source was https://cran.r-project.org/web/packages/emmeans/vignettes/interactions.html一个重要的来源是https://cran.r-project.org/web/packages/emmeans/vignettes/interactions.html

## packages
library(ggpubr)
library(rstatix)
library(emmeans)
library(data.table)

## prepare the example data
rm(list = ls())
set.seed(321)

a1 <- 0
b1 <- 1
a2 <- 1
b2 <- 1.7

x <- c(1:10)
y1 <- (a1+b1*x)+rnorm(10,0,.6)
y2 <- (a1+b1*x)+rnorm(10,0,.6)
y3 <- (a2+b1*x)+rnorm(10,0,.6)
y4 <- (a2+b2*x)+rnorm(10,0,.6)

dat <- data.frame(x=rep(x,4),y=c(y1,y2,y3,y4),group=rep(c("A","B","C","D"),each=10))

## regression and coefficients
lm.dat <- lm(y~x*group,dat)
summary(lm.dat)

## coeficients, confidence intervals and R2 by group
as.data.table(dat)[,as.list(coef(lm(y~x))),by=group]
as.data.table(dat)[,as.list(confint(lm(y~x))),by=group]
as.data.table(dat)[,list(r2=summary(lm(y~x))$r.squared),by=group]

## plots
emmip(lm.dat,group~x,cov.reduce=range)
ggscatter(dat,x="x",y="y",color="group",add="reg.line")+
    stat_regline_equation(aes(label=paste(..eq.label.., ..rr.label.., sep = "~~~~"),color = group))

## anova
anova(lm.dat)
anova_test(dat,y~x*group)

## interactions with covariates
## slopes for each group and pairwise comparisons
emtrends(lm.dat,pairwise~group,var="x")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM