Felix Proessl
Felix Proessl

Reputation: 1

Anova_test() error: variable has only one level

I am trying to run a three-way repeated measures ANOVA using the anova_test() function. Unfortunately, I prompted that the within factors only have one level (see error code below), even though when assessing the structure of the dataframe, I am shown that it has multiple levels. Can anybody help?

agn.rmanova <- anova_test(
  data = df_final, dv = rt, wid = subject,
  within = c(sleep, drug, target.type)
)

Error in assertthat_iv_has_enough_levels(.) : Variable sleephas only one level. Remove it from the model.

str(df_final)
Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 321 obs. of  8 variables:
 $ id         : num  1 1 1 1 1 1 1 1 1 1 ...
 $ supplement : chr  "A" "A" "A" "A" ...
 $ condition  : chr  "50" "50" "50" "normal" ...
 $ target.type: Factor w/ 4 levels "","negative",..: 2 3 4 2 3 4 2 3 4 2 ...
 $ rt         : num  503 647 513 506 587 ...
 $ subject    : Factor w/ 18 levels "1","4","7","8",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ sleep      : Factor w/ 2 levels "50","normal": 1 1 1 2 2 2 1 1 1 2 ...
 $ drug       : Factor w/ 3 levels "A","B","C": 1 1 1 1 1 1 2 2 2 2 ...

I even specifically specified the levels in my first attempts to troubleshoot (see below), but all without success.

df_final <- summary %>%
  filter(id != 2)%>%
  filter(id != 6) %>%
  mutate(sleep = factor(condition, levels = c("50", "normal")),
         drug = factor(supplement, levels = c("A", "B", "C")),
         target.type = factor(target.type, levels = c("negative","neutral","positive")),
         subject = factor(subject, levels = c("1","4","7","8","14","16","19","20",
                                              "21","23","24","25","27","29","32",
                                              "33","34","35")))

Any help would be really appreciated. Thanks!

Error code

Levels

Upvotes: 0

Views: 1008

Answers (1)

UW bruce
UW bruce

Reputation: 11

I was having this problem as well. My coworker helped solve it by subsetting the data frame to only include relevant data, and I also had the non-time variable in the wrong category (within instead of it should have been between). Not sure what solved it here but thought I'd share since this is unsolved!

Here's my original code:

    rowtotaltr.aov <- anova_test(
      data = weedcts_row_cm, dv = cm_RowTotalWeeds.tr, wid = id,
      within = c(RowMulch, Date)
      )

And here's what made it work for some reason:

    test<-as.data.frame(weedcts_row_cm[,c(1,3:5,12, 13)])
    test<-test[,c(1,3:5,12, 13)]
    test$Plot<-as.factor(test$Plot) 
    t<-anova_test(data = test, dv=cm_RowTotalWeeds.tr, wid=id, between=c(RowMulch), 
    within=c(Date))

I had treated my plot column as a factor in the original code as well, necessary because it was just numbered 1-27

Upvotes: 1

Related Questions