尽管超过 2 个级别,R 中的 rstatix anova_test 仍会出现对比度错误

rstatix anova_test in R gives contrast error despite more than 2 levels

以下是我的数据集的子集:

structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 
5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L), .Label = c("5356", 
"5357", "5358", "5359", "5360", "5363", "5373", "5381", "5383", 
"5386", "5395", "5397", "5400", "5401", "5444", "5445", "5446", 
"5448", "5450", "5451", "5454", "5472", "5473", "5475", "5476", 
"5477", "5478", "5480", "5481", "5483", "5487", "5494", "5495", 
"5504", "5505", "5506", "5507", "5508", "5509", "5513", "5514", 
"5515", "5516", "5517", "5518", "5519", "5521", "5523", "5524", 
"5526", "5527", "5528", "5544", "5545", "5546", "5547", "5551", 
"5552", "5553", "5554", "5555", "5558", "5559", "5560", "5562", 
"5564", "5566", "5573", "5574", "5575", "5576", "5577", "5578", 
"5579", "5584", "5585", "5587", "5588", "5589", "5591", "5594", 
"5595", "5604", "5611", "5612", "5613", "5615", "5616", "5619", 
"5620", "5621", "5622", "5626", "5627", "5628", "5631", "5632", 
"5634", "5635", "5643", "5652", "5653", "5654", "5655", "5656", 
"5657", "5659", "5660", "5661", "5664", "5665", "5666", "5669", 
"5671", "5672", "5673", "5678", "5680", "5688", "5689", "5690", 
"5691", "5692", "5698", "5699", "5700", "5702", "5703", "5704", 
"5706", "5708", "5709", "5710", "5730", "5731", "5732", "5733", 
"5734", "5735", "5739", "5740", "5741", "5742", "5743", "5744", 
"5745", "5746", "5747", "5748", "5749", "5750", "5753", "5754", 
"5755", "5766", "5767", "5776", "5777", "5778", "5779", "5780", 
"5781", "5787", "5788", "5789", "5790", "5791", "5792", "5793", 
"5797", "5798", "5799", "5800", "5801", "5810", "5811", "5812", 
"5813", "5814", "5819", "5820", "5821", "5822", "5823", "5824", 
"5825", "5827", "5828", "5829", "5830", "5857", "5859", "5874", 
"5875", "5876", "5877", "5878", "5879", "5883", "5884", "5886", 
"5887", "5888", "5889", "5890", "5892", "5893", "5896", "5899", 
"5900", "5909", "5910", "5918", "5919", "5920", "5921", "5922", 
"5923", "5927", "5929", "5931", "5932", "5933", "5934", "5936", 
"5937", "5941", "5943", "5944", "5949", "5950", "5951", "5952", 
"5956", "5957", "5958", "5959", "5971", "5972", "5973", "5976", 
"5979", "5980", "5981", "6001", "6002", "6003", "6004", "6005", 
"6009", "6027", "6028", "6033", "6042", "6054", "6063", "6067", 
"6073", "6076", "6077", "6078", "6079", "6080", "6081", "6082", 
"6083", "6098", "6102", "6103", "6104", "6105", "6106", "6107", 
"6111", "6119", "6133", "6146", "6147", "6157", "6158", "6160", 
"6161", "6162", "6163", "6164", "6165", "6166", "6167", "6168", 
"6169", "6170", "6171", "6172", "6173", "6174", "6175", "6190", 
"6193", "6195", "6196", "6197", "6208", "6228", "6229", "6232", 
"6255", "6268", "6269", "6270", "6275"), class = "factor"), Gender = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("Male", "Female"), class = "factor"), 
    Age = structure(c(2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 
    2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L), .Label = c("Young", 
    "Old"), class = "factor"), FA = c(0.335363597, 0.275185704, 
    0.381515294, 0.603494346, 0.615962446, 0.534368277, 0.378798842, 
    0.262283027, 0.463445097, 0.479340732, 0.390655279, 0.30846712, 
    0.38545981, 0.402050078, 0.466983199, 0.456424206, 0.402167588, 
    0.295976758, 0.442705005, 0.445807546, 0.607728601, 0.589604795, 
    0.372361958, 0.296392322, 0.311423391, 0.416039616, 0.534221292, 
    0.564736903, 0.411948681, 0.289896786, 0.487889856, 0.559963286, 
    0.577650726, 0.484459132, 0.454060167, 0.280332953), leftright = structure(c(1L, 
    2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
    2L, 1L, 2L, 1L, 2L), .Label = c("left", "right"), class = "factor"), 
    ROI_Coded = structure(c(2L, 2L, 1L, 1L, 3L, 3L, 2L, 2L, 1L, 
    1L, 2L, 2L, 1L, 1L, 3L, 3L, 2L, 2L, 1L, 1L, 3L, 3L, 2L, 2L, 
    1L, 1L, 3L, 3L, 2L, 2L, 1L, 1L, 3L, 3L, 2L, 2L), .Label = c("LC", 
    "Noradrenergic", "FPT"), class = "factor")), row.names = c(NA, 
-36L), class = c("tbl_df", "tbl", "data.frame"), na.action = structure(c(`11` = 11L, 
`12` = 12L, `54` = 54L, `208` = 208L, `347` = 347L, `348` = 348L, 
`1089` = 1089L, `1263` = 1263L, `1456` = 1456L), class = "omit"))

其中的一个子集如下所示:

  # A tibble: 36 x 6
   ID    Gender Age      FA leftright ROI_Coded    
   <fct> <fct>  <fct> <dbl> <fct>     <fct>        
 1 5356  Male   Old   0.335 left      Noradrenergic
 2 5356  Male   Old   0.275 right     Noradrenergic
 3 5356  Male   Old   0.382 left      LC           
 4 5356  Male   Old   0.603 right     LC           
 5 5356  Male   Young   0.616 left      FPT          
 6 5356  Male   Young   0.534 right     FPT          
 7 5357  Male   Old   0.379 left      Noradrenergic
 8 5357  Male   Old   0.262 right     Noradrenergic
 9 5357  Male   Old   0.463 left      LC           
10 5357  Male   Old   0.479 right     LC          

我想 运行 使用 rstatix anova_test 函数的混合方差分析。我已尝试 运行以下操作:

res.aov <- rstatix::anova_test(
  data = df2, dv = FA, wid = ID,
  between = c(ROI_Coded,leftright), within = c(Age,Gender)
)

但反复收到以下输出和错误:

The 'wid' column contains duplicate ids across between-subjects variables. Automatic unique id will be created
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels

但是当我检查我的数据时,我没有 NA 和至少 2 个因素:

sapply(fctr, nlevels)
sapply(df2, function(x) sum(is.na(x)))

NAs:
ID    Gender       Age        FA leftright ROI_Coded 
0         0         0         0         0         0

Factors:
ID    Gender       Age leftright ROI_Coded 
310         2         2         2         3 

我遵循调试对比错误 但仍然收到错误。

我不确定为什么或如何继续。

我想我按照指示找到了解决方案 here

afex::aov_car(FA ~ Age*Gender*ROI_Coded*leftright + Error(id/ROI_Coded*leftright), data=df2)

输出结果以及:

Missing values for following ID(s):
5357, 5383, 5505, 5552, 5821, 5909, 5980
Removing those cases from the analysis.

仔细查看数据,这些 ID 并没有 ROI_Coded 的每个级别。所以来自 rstatix 的因子警告有点误导,因为每个 ID 都有 >=2 个因子。