我是一门介绍统计课程的助教,并试图用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的因素有关,这两种情况都不是我的问题。我也尝试过将Time变量在idata tibble中更改为一个有序因子,但这也没有帮助。Time变量在idata tibble中被确定为因子变量类型。我使用的数据是来自nlme包的nlme数据。
我的数据:
> 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> 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 谢谢!任何洞察力都会受到赞赏!
发布于 2021-12-13 20:21:37
@TrainingPizza找到了解决方案!
显然,在这种情况下,tibble(Time = time)与car::Anova()的工作不太好。使用data.frame(Time = time)解决了这个问题。
https://stackoverflow.com/questions/70339516
复制相似问题