Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases in r with repeated mesures ANOVA

Tags:

r

anova

When I use anova_test() function(from rstatix package) to do two-way repeated measures ANOVA, an error occur:

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases

I check my data and there is no missing value.

BTW, in my data, not all the people have 8 times outcome. Some people have maximum 3 times, some 8 times and so on.

I refer to this website to do my two-way repeated measures ANOVA :

https://www.datanovia.com/en/lessons/repeated-measures-anova-in-r/

I have upload my dataset to github.

mydata :https://github.com/lizhiwei1994/testRepo/blob/master/mydata.csv

My code:

# load packages
library("tidyverse")
library("ggpubr")
library("rstatix")

# load data and check missing value
mydata <- read.csv(
  url("https://raw.githubusercontent.com/lizhiwei1994/testRepo/master/mydata.csv")
) %>% convert_as_factor(id, time, treatment)
glimpse(mydata)
sum(is.na(mydata))

# error occurring
res.aov <- anova_test(
  data = mydata, dv = outcome, wid = id,
  within = c(treatment, time)
)


get_anova_table(res.aov)

like image 455
zhiwei li Avatar asked Sep 16 '25 01:09

zhiwei li


1 Answers

For repeated measure anova, you need complete observations for each time point, before and after treatment, before you do the anova, it's always good to check the observations:

tab = table(mydata$time,mydata$treatment,mydata$id)
#subject = 1
tab[,,"1"]

    control2 treat2
  1        1      0
  2        1      0
  3        1      0
  4        1      0
  5        1      0
  6        1      0
  7        1      0
  8        0      0

So this subject has only control2 observations but no treatment2 observations. If there is something wrong with the entry of subject, please correct it. Below I can show you an example of when it will work:

test = expand.grid(id=1:2,time=1:8,treatment=c("a","b"))
test$outcome=rnorm(nrow(test))
table(test$time,test$treatment,test$id)
, ,  = 1


    a b
  1 1 1
  2 1 1
  3 1 1
  4 1 1
  5 1 1
  6 1 1
  7 1 1
  8 1 1

, ,  = 2


    a b
  1 1 1
  2 1 1
  3 1 1
  4 1 1
  5 1 1
  6 1 1
  7 1 1
  8 1 1

anova_test(data=test,dv=outcome,wid=id,within=c("treatment","time"))
ANOVA Table (type III tests)

          Effect DFn DFd       F     p p<.05   ges
1      treatment   1   1 424.283 0.031     * 0.078
2           time   7   7   1.596 0.276       0.422
3 treatment:time   7   7   1.571 0.283       0.422

If your dataset is such, according to the formula you provided in the comment aov(outcome ~ time*treatment + Error(id), data = mydata), it is a mixed anova, but for this you need individuals that have undergone both treatment and control, to control for the individual effect, and this is absent from your dataset.

The only anova you can perform in this situation is a two-way anova:

anova_test(data=test,dv=outcome,between=c(time,treatment))
Coefficient covariances computed by hccm()
ANOVA Table (type II tests)

          Effect DFn DFd     F     p p<.05   ges
1           time   7  16 1.668 0.187       0.422
2      treatment   1  16 1.350 0.262       0.078
3 time:treatment   7  16 1.668 0.187       0.422
like image 199
StupidWolf Avatar answered Sep 18 '25 16:09

StupidWolf