简体   繁体   中英

Repeated measures ANOVA Error: contrasts only apply to factors

I'm working as a TA for an intro statistics course and am trying to create a practice question for a repeated measures ANOVA using R. When calling the car::Anova() function, I run into the following error:

Error in `contrasts<-`(`*tmp*`, value = if (is.ordered(idata[, i])) icontrasts[2] else icontrasts[1]): contrasts apply only to factors

traceback:

6.stop("contrasts apply only to factors")
5.`contrasts<-`(`*tmp*`, value = if (is.ordered(idata[, i])) icontrasts[2] else icontrasts[1])
4.Anova.III.mlm(mod, SSPE, error.df, idata, idesign, icontrasts, imatrix, test, ...)
3.Anova.II.mlm(mod, SSPE, error.df, idata, idesign, icontrasts, imatrix, test.statistic, ...)
2.Anova.mlm(lmaov, idata = idata, idesign = ~Time)
1.Anova(lmaov, idata = idata, idesign = ~Time)

I've found a similar problem on Stackoverflow, but that had to do with the factor having only 1 level or NA's in the data, which both are not the case for my problem. I've also tried changing the Time variable in the idata tibble to an ordered factor, but that did not help either. The Time variable in the idata tibble is definately of the factor variable type. The data I use is the BodyWeight data sourced from the nlme package.

my data:

> as.matrix(data) returns the following:

       1   8  15  22  29  36  43  44  50  57  64
[1,] 240 250 255 260 262 258 266 266 265 272 278
[2,] 225 230 230 232 240 240 243 244 238 247 245
[3,] 245 250 250 255 262 265 267 267 264 268 269
[4,] 260 255 255 265 265 268 270 272 274 273 275
[5,] 255 260 255 270 270 273 274 273 276 278 280
[6,] 260 265 270 275 275 277 278 278 284 279 281
[7,] 275 275 260 270 273 274 276 271 282 281 284
[8,] 245 255 260 268 270 265 265 267 273 274 278

> idata returns the following:

# A tibble: 11 x 1
   Time 
   <fct>
 1 1    
 2 8    
 3 15   
 4 22   
 5 29   
 6 36   
 7 43   
 8 44   
 9 50   
10 57   
11 64 

My code:

library(nlme)
library(car)
library(tidyverse)
data <- BodyWeight %>% 
  #mutate(Time = as.factor(Time)) %>% 
  filter(Diet == 1) %>% ##keep it simple
  pivot_wider(names_from = Time, values_from = weight) %>% #move to wide format, particpants x timepoints
  select(-Diet, -Rat) ##Removing unnecessary columns

time <- BodyWeight %>% 
  pull(Time) %>% 
  as.factor() %>% 
  unique() #%>% ordered()

#time <- factor(time, levels=rev(levels(time)))

idata <- tibble(Time = time)

lmaov <- lm(as.matrix(data)~1)
test <- Anova(lmaov,idata=idata,idesign=~Time)
summary(test, multivariate = FALSE)

Session Info:

R version 4.0.1 (2020-06-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252 
[2] LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
 [1] forcats_0.5.1   stringr_1.4.0   dplyr_1.0.7     purrr_0.3.4    
 [5] readr_2.0.1     tidyr_1.1.3     tibble_3.1.4    ggplot2_3.3.5  
 [9] tidyverse_1.3.1 car_3.0-11      carData_3.0-4   nlme_3.1-153   

loaded via a namespace (and not attached):
 [1] fs_1.5.0             lubridate_1.7.10     httr_1.4.2          
 [4] tools_4.0.1          backports_1.2.1      utf8_1.2.2          
 [7] R6_2.5.1             rpart_4.1-15         DBI_1.1.1           
[10] colorspace_2.0-2     nnet_7.3-14          withr_2.4.2         
[13] tidyselect_1.1.1     curl_4.3.2           compiler_4.0.1      
[16] cli_3.0.1            rvest_1.0.1          xml2_1.3.2          
[19] scales_1.1.1         digest_0.6.27        foreign_0.8-80      
[22] rmarkdown_2.10       rio_0.5.27           pkgconfig_2.0.3     
[25] htmltools_0.5.2      parallelly_1.28.1    dbplyr_2.1.1        
[28] fastmap_1.1.0        rlang_0.4.11         readxl_1.3.1        
[31] rstudioapi_0.13      generics_0.1.0       jsonlite_1.7.2      
[34] ModelMetrics_1.2.2.2 zip_2.2.0            magrittr_2.0.1      
[37] Matrix_1.2-18        Rcpp_1.0.7           munsell_0.5.0       
[40] fansi_0.5.0          abind_1.4-5          lifecycle_1.0.0     
[43] stringi_1.7.4        pROC_1.18.0          yaml_2.2.1          
[46] MASS_7.3-54          plyr_1.8.6           recipes_0.1.17      
[49] grid_4.0.1           parallel_4.0.1       listenv_0.8.0       
[52] crayon_1.4.1         lattice_0.20-41      haven_2.4.3         
[55] splines_4.0.1        hms_1.1.0            knitr_1.34          
[58] pillar_1.6.2         corpcor_1.6.9        future.apply_1.8.1  
[61] reshape2_1.4.4       codetools_0.2-16     stats4_4.0.1        
[64] reprex_2.0.1         glue_1.4.2           evaluate_0.14       
[67] data.table_1.14.0    modelr_0.1.8         vctrs_0.3.8         
[70] tzdb_0.1.2           foreach_1.5.1        cellranger_1.1.0    
[73] gtable_0.3.0         future_1.22.1        assertthat_0.2.1    
[76] xfun_0.25            gower_0.2.2          openxlsx_4.2.4      
[79] prodlim_2019.11.13   broom_0.7.9          rstatix_0.7.0       
[82] class_7.3-17         survival_3.2-13      timeDate_3043.102   
[85] Hotelling_1.0-8      iterators_1.0.13     lava_1.6.10         
[88] globals_0.14.0       ellipsis_0.3.2       caret_6.0-90        
[91] ipred_0.9-12        

Thanks! Any insights are appreciated!

@TrainingPizza found the solution!

Appearantly for this case tibble(Time = time) doesnt work well with car::Anova() . using data.frame(Time = time) fixed the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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