Reputation: 312
I could get results when using purrr::map()
to run lmer()
analysis. But I received error messages when I wanted to do the same for nlme::lme()
:
Error in `map()`:
ℹ In index: 1.
ℹ With name: p_h.
Caused by error:
! object '.x' not found
This is the toy data that can be used to reproduce my analysis:
testdata <- structure(list(subject = c("B001", "B001", "B001", "B001", "B001",
"B002", "B002", "B002", "B002", "B002", "B003", "B003", "B003",
"B003", "B003", "B004", "B004", "B004", "B004", "B004", "B005",
"B005", "B005", "B005", "B005"), time_point = structure(c(1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L,
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), levels = c("Wk0", "Wk4", "Wk8",
"Wk12", "Wk16"), class = "factor"), sfa = c(62.895, 84.705, 83.49,
66.64, 72.19, 56.195, 93.945, 92.635, 88.51, 83.505, 92.67, 90.81,
83.37, 90.205, 84.195, 88.065, 53.69, 93.14, 52.57, 95.995, 63.505,
92.59, 80.87, 89.125, 67.305), mufa = c(14.455, 11.71, 12.58,
21.135, 26.175, 13.285, 4.91, 6.08, 6.735, 12.745, 7.325, 7.605,
15.735, 7.985, 12.32, 7.92, 42.045, 4.57, 24.305, 4.505, 18.585,
5.955, 14.815, 8.775, 20.295), pufa = c(22.65, 3.58, 3.935, 12.23,
1.635, 30.525, 1.135, 1.275, 4.76, 3.75, 0, 1.595, 0.885, 1.82,
3.495, 4.01, 4.26, 2.29, 23.125, 0, 17.905, 1.455, 4.305, 2.1,
12.4)), row.names = c(NA, -25L), class = c("tbl_df", "tbl", "data.frame"
))
This works
For lmer()
library(lme4)
library(nlme)
library(lmerTest)
library(broom.mixed)
testdata |>
dplyr::select(sfa, mufa, pufa) |>
purrr::map(~ lmer(.x ~ time_point + (1| subject), data = testdata))
This does not work
For nlme::lme()
library(lme4)
library(nlme)
library(lmerTest)
library(broom.mixed)
testdata |>
dplyr::select(sfa, mufa, pufa) |>
purrr::map(~ lme(.x ~ time_point, random = ~ 1 | subject,
data = testdata, method = 'ML'))
Any help is very much appreciated.
Upvotes: 1
Views: 90
Reputation: 226741
This may not be quite as pretty, but it works:
list("sfa", "mufa", "pufa") |>
purrr::map(~ reformulate("time_point", response = .)) |>
purrr::map(~ lme(., random = ~ 1 | subject,
data = testdata, method = 'ML'))
It's almost always better to set things up so that all the variables in a statistical model are being drawn from the same source. This is only sort of true in your example; the response is coming from the variable passed through map
, while the predictor is coming from the data specified in the data=
argument. (In your case they are both ultimately coming from testdata
, but they wouldn't have to be.) This doesn't explain exactly why lmer
and lme
behave differently, though (arm-waving: they use slightly different rules to evaluate the variables in the formula ...)
Upvotes: 3