Reputation: 19
I am attempting to run an analysis of co-variance on a mixed effect model. My data set is as follows
> str(try)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 864 obs. of 7 variables:
$ Site : chr "BISC1" "BISC1" "BISC1" "BISC1" ...
$ SET : Factor w/ 3 levels "SET1","SET2",..: 1 1 1 1 1 1 1 1 1 1 ...
$ ARM : chr "A_0001" "A_0001" "A_0001" "A_0001" ...
$ Pin : num 1 2 3 4 5 6 7 8 9 1 ...
$ SETarmpin : chr "SET1_A_0001_1" "SET1_A_0001_2" "SET1_A_0001_3"
"SET1_A_0001_4" ...
$ Days : num 145 145 145 145 145 145 145 145 145 145 ...
$ AbsPinDiff: num -1 -4 7 -12 -5 0 -5 -1 0 -22 ...
> dput(head(try))
structure(list(Site = c("BISC1", "BISC1", "BISC1", "BISC1", "BISC1",
"BISC1"), SET = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("SET1",
"SET2", "SET3"), class = "factor"), ARM = c("A_0001", "A_0001",
"A_0001", "A_0001", "A_0001", "A_0001"), Pin = c(1, 2, 3, 4,
5, 6), SETarmpin = c("SET1_A_0001_1", "SET1_A_0001_2", "SET1_A_0001_3",
"SET1_A_0001_4", "SET1_A_0001_5", "SET1_A_0001_6"), Days = c(145,
145, 145, 145, 145, 145), AbsPinDiff = c(-1, -4, 7, -12, -5,
0)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))
My mixed effect model is as below :
trymodel<-lme(AbsPinDiff~Days+SET, random = ~1|SETarmpin,
correlation = corAR1(form=~Days|SETarmpin),
data = try, na.action = na.exclude, method="REML")
I am using the Anova function within the 'car' package. Yet when I run the function on the above model I get the following error message as below :
> Anova(trymodel4)
Error in terms.formula(object, data = data) :
'data' argument is of the wrong type
I am a bit confused because from what I can surmise, the Anova function does not have a 'data' argument.
Upvotes: 1
Views: 884
Reputation: 226162
Most of the time when you give your dataset the same name as a built-in R object (e.g. try
), R is clever and figures out that it needs an object that is not a function. This seems to be one of the cases where it gets confused - leading to the general advice to not name your objects this way ...
There is a bizarre interaction (that I haven't figured out yet) with loading the ggplot2
package (reported here). I get an error in either case, but if ggplot2
is loaded (either before or after nlme
, which sometimes makes a difference) then I get the error you report; otherwise I get a different error. (Note that if you're going to experiment with this you need to make sure you start each test in a clean R session.)
In either case changing the name of the data set makes car::Anova()
work for me.
test <- FALSE
test_before <- TRUE
if (test_before) library(ggplot2)
library(nlme)
if (test) library(ggplot2)
data("sleepstudy",package="lme4")
try <- sleepstudy ## rename data set
m1 <- lme(Reaction~Days,random=~1|Subject,
correlation=corAR1(form=~Days|Subject),data=try)
car::Anova(m1)
## with only nlme: cannot coerce class "function" to a data.frame
car::Anova(update(m1,data=sleepstudy)) ## works
This might be fixable internally in the car
package (there are ways to tell R only to look for non-function objects).
My session info:
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] nlme_3.1-137 ggplot2_2.2.1.9000
loaded via a namespace (and not attached):
[1] Rcpp_0.12.17 bindr_0.1.1 magrittr_1.5 tidyselect_0.2.4
[5] munsell_0.5.0 colorspace_1.3-2 lattice_0.20-35 R6_2.2.2
[9] rlang_0.2.1 carData_3.0-1 car_3.0-0 plyr_1.8.4
[13] dplyr_0.7.5 tools_3.6.0 grid_3.6.0 data.table_1.11.4
[17] gtable_0.2.0 rio_0.5.10 withr_2.1.2 abind_1.4-5
[21] readxl_1.1.0 lazyeval_0.2.1 assertthat_0.2.0 tibble_1.4.2
[25] zip_1.0.0 bindrcpp_0.2.2 purrr_0.2.5 curl_3.2
[29] glue_1.2.0 haven_1.1.1 openxlsx_4.1.0 cellranger_1.1.0
[33] compiler_3.6.0 pillar_1.2.3 forcats_0.3.0 scales_0.5.0.9000
[37] foreign_0.8-70 pkgconfig_2.0.1
>
Upvotes: 2