1

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.

Malcolm Wells
  • 19
  • 2
  • 8
  • Your image of text [isn't very helpful](//meta.unix.stackexchange.com/q/4086). It can't be read aloud or copied into an editor, and it doesn't index very well, meaning that other users with the same problem are less likely to find the answer here. Please [edit] your post to incorporate the relevant text directly (preferably using copy+paste to avoid transcription errors). – Toby Speight Jun 20 '18 at 14:15
  • @TobySpeight thanks for the advice! Does this edit look a little more agreeable? – Malcolm Wells Jun 20 '18 at 14:26
  • 1
    you're going to want to dput your data..https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – TheSciGuy Jun 20 '18 at 16:45
  • @NickDylla I used the dput() function in tandem with the head() function. Is this edit more desirable? I really appreciate the advice – Malcolm Wells Jun 20 '18 at 17:40
  • unfortunately, we can't replicate with just the `head()` (although sometimes that provides enough information to clarify the problem). Can you show us `summary(trymodel4)` ? – Ben Bolker Jun 20 '18 at 17:49

1 Answers1

2

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  
> 
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Thanks for the helpful tip! I made the suggested adjustment and unfortunately still got the same error message. From an online source... "The first step is to reorganize the data into a matrix form where rows are subjects, and columns are levels of the repeated measures factor"... I suspect this does not describe the organization of my data set, but I am not sure how to make the necessary adjustments. – Malcolm Wells Jun 20 '18 at 17:45
  • are you sure?? now that I've added the correlation argument, I can get your exact error (and solve it by changing the data set name). – Ben Bolker Jun 20 '18 at 17:59
  • Oh my! That totally did remedy the issue, I was changing the name of the model not the data set. Silly me! Regarding your comment about the online advice I posted, do you have any suggestions to ensure that an online or tutorial is credible? I usually look at the institution or publisher but that is not always enough – Malcolm Wells Jun 20 '18 at 18:37
  • Unfortunately I don't. Maybe it was reasonable advice that you accidentally took a little bit out of context? – Ben Bolker Jun 20 '18 at 18:43
  • I think you're exactly right. Lack of understanding caused me to drag it out of context. Thanks for helping me get back on track! – Malcolm Wells Jun 20 '18 at 18:50