简体   繁体   English

生成和编码方案

[英]generating a sum coding scheme

I have a dataframe which looks like this:我有一个看起来像这样的数据框:

df <- data.frame(id= rep(seq(1:125),3),
  timpoint= c(rep("T1", 125), rep("T2", 125), rep("T3", 125)),
                 treatment=c(rep("A",25),rep("B",25),rep("C",25),rep("D",25),rep("E",25)))
interaction.col <- paste(df$timpoint, df$treatment, sep = "_")  

df <- cbind(df, interaction.col)

I am trying to generate a sum coding scheme for the interaction column which is a combination of the first two columns.我正在尝试为交互列生成一个总和编码方案,它是前两列的组合。 According to this paper I should get a matrix of (a−1)×(b−1) columns and n rows(in this case 375)根据这篇论文,我应该得到一个 (a−1)×(b−1) 列和 n 行的矩阵(在这种情况下为 375)

I have read up on using contrasts:我已经阅读了使用对比:

 contrasts(df$interaction.col) <-  "contr.sum"
     df.c <- contrasts(df$interaction.col)

However, somehow the output is a 15x14 matrix, while it should be a 375 x8.然而,不知何故,输出是一个 15x14 的矩阵,而它应该是一个 375 x8。 Also, only the very last row is set to -1, which shouldn't be the case.此外,只有最后一行设置为 -1,这不应该是这种情况。 For all the ID's of the last treatment (E) the interaction column should be set to -1 for the corresponding timepoint.对于上次处理 (E) 的所有 ID,相应时间点的交互列应设置为 -1。 The last ID in treatment group E should be -1 for all columns.对于所有列,处理组 E 中的最后一个 ID 应为 -1。 What am i doing wrong here?我在这里做错了什么?

Depending on what effects you are interested in and how you will fit the model, you will end up with different number of effects.根据您感兴趣的效果以及您将如何拟合模型,您最终会得到不同数量的效果。 For example, in the case whereby you fit the main and interaction effects, you should end up with 8 columns for the interactions ie (a-1) x (b-1).例如,在拟合主效应和交互效应的情况下,最终应该有 8 列用于交互,即 (a-1) x (b-1)。 In the case you do not fit the main effects you end up with a*b - 1:如果您不适合主效应,您最终会得到 a*b - 1:

Here is how to create your matrix:以下是创建矩阵的方法:

With main effects:主要影响:

model.matrix(~treatment * timpoint, df, list(treatment = contr.sum, timpoint=contr.sum))

In this case, the last 8 columns are the ones you are interested in在这种情况下,最后 8 列是您感兴趣的列

Without main effects:无主效应:

model.matrix(~treatment:timpoint, df, list(treatment = contr.sum, timpoint=contr.sum))

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

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