【问题标题】:Repeated measures ANOVA Error: contrasts only apply to factors重复测量方差分析误差:对比仅适用于因子
【发布时间】:2022-01-17 05:05:39
【问题描述】:

我正在担任介绍性统计课程的助教,并尝试使用 R 为重复测量方差分析创建练习题。在调用 car::Anova() 函数时,我遇到以下错误:

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

追溯:

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)

我在 Stackoverflow 上发现了一个类似的问题,但这与数据中只有 1 个级别或 NA 的因素有关,这两种情况都不是我的问题。我还尝试将idata tibble 中的Time 变量更改为有序因子,但这也无济于事。 idata tibble 中的 Time 变量肯定是因子变量类型。我使用的数据是来自nlme 包的BodyWeight 数据。

我的数据:

&gt; as.matrix(data) 返回以下内容:

       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

&gt; idata 返回以下内容:

# 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 

我的代码:

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)

会话信息:

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        

谢谢!任何见解都值得赞赏!

【问题讨论】:

  • data 看起来像什么?运行您的代码时出现错误。
  • @cazman BodyWeight 是来自 nlme 包的数据,它对我来说运行良好
  • @cazman 我编辑了帖子以提供数据打印。
  • @TrainingPizza 你的意思是你不能重现错误吗?或者直到car::Anova() 之前的代码运行良好?
  • @Bart Kramer 我收到错误消息,我只是在确认数据没问题。您是否尝试过使用idata 作为data.frame 而不是tibble 来运行它? tibble 并不总是适用于 tidyverse 之外的功能。在过滤饮食、将 Time/Rat 转换为因子并使用 aov(formula = weight ~ Time + Error(Rat), data = dat) 之后,将数据保持为长格式似乎也更直接一些

标签: r anova


【解决方案1】:

@TrainingPizza 找到了解决方案!

对于这种情况,tibble(Time = time) 显然不适用于car::Anova()。使用data.frame(Time = time) 解决了这个问题。

【讨论】:

    猜你喜欢
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 2022-12-17
    • 2015-12-28
    • 2015-12-06
    相关资源
    最近更新 更多