Search code examples
rancova

afex in R: Error: Empty cells in within-subjects design (i.e., bad data structure). But the data structure is fine


I'm attempting to run an ANCOVA with 1 between-subjects variable and 2 within-subjects variables and I'm running into an error that makes no sense to me. My data looks like this:

Scan ID Region ALFF Age Resp
1 20 AID 0.826 Adol 77.25
2 20 AID 1.116 Adol 73.18
1 22 AID 0.362 Adult 78.70
2 22 AID 0.849 Adult 72.58
1 20 MDM 0.826 Adol 79.25
2 20 MDM 1.116 Adol 71.18
1 22 MDM 0.778 Adult 79.70
2 22 MDM 0.291 Adult 73.58

My ANCOVA code is:

Full_Anova_ALFF<- AlFF_Resp %>% group_by(Region) %>% do(fit=aov_car(ALFF ~ AgeScan+Error(ID/ScanResp), data = .))

and I get this error when I run it:

Converting to factor: Age

Error: Empty cells in within-subjects design (i.e., bad data structure). table(data[c("Scan", "Resp")])

 Resp
Scan X77.25 X73.1777777766667 X63.1944444433333 X70.3333333333333 X78.7 X72.5833333333333
X1 1 0 0 0 1 0
X2 0 1 0 0 0 1
X3 0 0 1 0 0 1
X4 0 0 0 1 0 0
 Resp
Scan X72.4833333333333 X78.25 X65.1833333333333 X71.9166666666667 X57.333333335 X65.55
X1 0 0 1 0 0 1
X2 0 0 0 1 0 0
X3 1 0 0 0 0 0
X4 0 1 0

Scan is factor variable and resp is numeric and I have no idea why this error is occurring. There're no empty cells! And this weird table that was outputted as a part of the error message is also quite strange. It appears to be treating respiration as a factor maybe? But I've definitely told it that respiration is numeric. When I take respiration out of the model, it runs completely fine. However, unfortunately, I do need to include respiration.

Anybody have any idea what's going wrong? Or even just a workaround I can use to get this done?

Thanks in advance for your help!!


Solution

  • For those interested, I figured it out! I ended up using a linear mixed-model instead of what I have above because my covariate was at the scan-level rather than a between-subjects variable.

    My command ended up being:

    Full_Anova_ALFF<- AlFF_Resp %>% group_by(Region) %>% do(fit=mixed(ALFF~Resp+Age*Scan+(1|ID)+(1|Scan),data=.))

    Hope this helps someone in the future!