简体   繁体   English

比较 R 中两个连续变量和一个分类变量之间的三向交互作用的治疗效果

[英]Compare treatment effects in three way interaction between two continuous variables and one categorical variable in R

I am trying to run a linear regression model which contains continuous variable A * continuous variables B * categorical variable (treatments with 4 levels).我正在尝试运行线性回归 model,其中包含连续变量 A * 连续变量 B * 分类变量(4 个级别的处理)。 Data can be download here .数据可以在这里下载。

Model<-lm(H2O2~Treatment*(A*B), data=mydata)

Now I want to compare different treatment effects.现在我想比较不同的治疗效果。

I know that lstrends can deal with continuous variable * categorical variable in linear model, but it could not work in my situation.我知道lstrends可以处理线性 model 中的连续变量 * 分类变量,但在我的情况下它无法工作。 I also tried to divide the data based on different treatment groups and created 4 different linear models to compare, that did not work either.我还尝试根据不同的治疗组划分数据,并创建了 4 个不同的线性模型进行比较,但这也不起作用。

The equation you're estimating is:你估计的方程是:

在此处输入图像描述

There are six different treatment effects in which you could be interested - they comprise the pairwise differences among treatment categories given fixed values of A and B .您可能会对六种不同的处理效果感兴趣 - 它们包含给定AB固定值的处理类别之间的成对差异。 Three of these are represented by comparisons of estimated categories versus the reference category.其中三个由估计类别与参考类别的比较来表示。 For example, to figure out the effect of HF versus HC (the reference), you would calculate:例如,要计算出HFHC (参考)的影响,您可以计算:

Looking at the coefficients from your model:查看 model 中的系数:

b <- coef(Model)
b
    (Intercept)     TreatmentHF     TreatmentLF     TreatmentMF               A               B 
  -1.4318658015    1.5744952961    1.7649475644   -0.6971275663    0.0334782841    0.1528682774 
            A:B   TreatmentHF:A   TreatmentLF:A   TreatmentMF:A   TreatmentHF:B   TreatmentLF:B 
  -0.0022753098   -0.0313728254   -0.0342105088    0.0173173280   -0.1430777577   -0.1214230927 
  TreatmentMF:B TreatmentHF:A:B TreatmentLF:A:B TreatmentMF:A:B 
   0.0212295284    0.0025811227    0.0023565223   -0.0007721532 

You would want in R, something like你会想要 R,像

b[2] + b[8]*A + b[11]*B + b[14]*A*B

You would want to substitute in a wide range of combinations of A and B , which you could do by making a sequence of values of each going from the minimum to the maximum, and then crossing them.您可能希望替换AB的各种组合,您可以通过将每个值的序列从最小值到最大值,然后将它们交叉来实现。

a_seq <- seq(min(mydata$A), max(mydata$A), length=25)
b_seq <- seq(min(mydata$B), max(mydata$B), length=25)
eg <- expand.grid(A=a_seq, B=b_seq)
head(eg)
#          A    B
# 1  5.03000 4.34
# 2 10.01292 4.34
# 3 14.99583 4.34
# 4 19.97875 4.34
# 5 24.96167 4.34
# 6 29.94458 4.34

You could then make the treatment effect in this dataset.然后,您可以在此数据集中制作处理效果。

library(dplyr)
eg <- eg %>% mutate(treat_HC_HF = b[2] + b[8]*A + b[11]*B + b[14]*A*B)

Then, you could plot it using a heatmap or similar.然后,您可以使用热图或类似方法 plot 它。

ggplot(eg, aes(x=A, y=B, fill=treat_HC_HF)) + 
  geom_tile() + 
  scale_fill_viridis_c() + 
  theme_classic() + 
  labs(fill="Treatment\nEffect")

在此处输入图像描述

You could do this for the other comparisons as well.您也可以对其他比较执行此操作。 There are two things that you don't get from this directly that are.有两件事你不能直接从中得到。 First, this doesn't tell you anything about where you actually observe A and B. Second it doesn't tell you whether any of these effects is statistically significant.首先,这并不能告诉您实际观察到 A 和 B 的位置。其次,它不能告诉您这些影响中的任何一个是否具有统计显着性。 The first problem you could solve more or less by only plotting those hypothetical values of A and B that are in the convex hull of A and B in the data.第一个问题可以通过仅绘制数据中 A 和 B 的凸包中的 A 和 B 的假设值来或多或少地解决。

library(geometry)
ch <- convhulln(mydata[,c("A", "B")])
eg <- eg %>% 
  mutate(inhull = inhulln(ch, cbind(A,B)))

eg %>% 
  filter(inhull) %>% 
  ggplot(aes(x=A, y=B, fill=treat_HC_HF)) + 
  geom_tile() + 
  scale_fill_viridis_c(limits = c(min(eg$treat_HC_HF), max(eg$treat_HC_HF))) + 
  theme_classic() + 
  labs(fill="Treatment\nEffect")

在此处输入图像描述

To calculate whether or not these are significant, you would have to do a bit more work.要计算这些是否重要,您必须做更多的工作。 First, you'd have to get the standard error of each comparison.首先,您必须获得每次比较的标准误差。 What you need is a matrix we'll call M that collects the values you multiply the coefficients by to get the treatment effect.你需要的是一个我们称之为M的矩阵,它收集你乘以系数以获得治疗效果的值。 So, in the above example, we would have the three pieces of information:因此,在上面的示例中,我们将拥有三个信息:

在此处输入图像描述

In R, we could get these with:在 R 中,我们可以通过以下方式获得这些:

b_t <- b[c(2,8,11,14)]
V_t <- vcov(Model)[c(2,8,11,14), c(2,8,11,14)]
M <- cbind(1, eg$A, eg$B, eg$A*eg$B)

Then, we could calculate the standard error of the treatment effect as:然后,我们可以计算治疗效果的标准误差为:

In R, we could do this and identify which treatment effects are significant (two-tailed 95% test) with:在 R 中,我们可以这样做并确定哪些治疗效果显着(双尾 95% 检验):

eg <- eg %>% 
  mutate(se = sqrt(diag(M %*% V_t %*% t(M))), 
         sig = abs(treat_HC_HF/se) > pt(0.975, Model$df.residual))

Then we could plot only those effects that are in the convex hull and significant:然后我们可以 plot 只有那些在凸包中且显着的效果:

eg %>% 
  filter(inhull, sig) %>% 
  ggplot(aes(x=A, y=B, fill=treat_HC_HF)) + 
  geom_tile() + 
  scale_fill_viridis_c(limits = c(min(eg$treat_HC_HF), max(eg$treat_HC_HF))) + 
  theme_classic() + 
  labs(fill="Treatment\nEffect")

在此处输入图像描述

You would have to do this for each one of the six paired comparisons of levels of the treatment effects.对于治疗效果水平的六对配对比较中的每一个,您都必须这样做。 This seems like a lot of work, but the model, despite the simplicity of estimating it, is quite complicated to interpret.这似乎需要做很多工作,但是 model 尽管估算起来很简单,但解释起来却相当复杂。

暂无
暂无

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

相关问题 可视化 R 中两个连续变量和一个分类变量之间的三向交互 - Visualising a three way interaction between two continuous variables and one categorical variable in R R中连续变量和分类变量之间的相互作用:是否可以包含所有类别? - Interaction between continuous and categorical variable in R: is there a way to include all categories? R中两个分类变量之间的相互作用 - Interaction between two categorical variables in R 是否有一种简单的方法可以将分类变量与连续变量分成R中的两个数据集 - Is there an easy way to separate categorical vs continuous variables into two dataset in R 如何在 R 中可视化多个分类变量与一个连续变量 - How to visualize multiple categorical variables vs one continuous variable in R R lm 如何选择与分类变量和连续变量之间的交互作用的对比? - How does R lm choose contrasts with interaction between a categorical and continuous variables? 在 R 中,如何比较两个连续变量并在图表中指示两个分类变量 - In R, How can I compare two continuous variables and indicating two categorical variables in a chart 具有类别和平方连续变量的R lm交互项 - R lm interaction terms with categorical and squared continuous variables R:将2个连续变量重新编码为1个分类变量 - R: Recode 2 continuous variables into 1 categorical variable R-分析类别变量对连续变量的影响 - R - Analyse impact of categorical variables on continuous variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM