问题
I am trying to get the type 3 ANOVA table with emmeans::joint_tests()
from a list with the following code. I don't fully understand the error message.
The code that tutors me came from
http://pages.stat.wisc.edu/~yandell/R_for_data_sciences/curate/tidyverse.html
library(dplyr)
library(nlme)
library(emmeans)
data("diamonds")
diamonds %>%
split(.$cut) %>%
map(~ gls(price ~ x + y + z,
weights = varIdent(form = ~ 1|color),
data = .))%>%
map(summary)
The error message seems to suggest that I save my individual models somehow and then apply joint_tests
. What I don't understand is why the workflow works for summary
but not for joint_tests
. When we fit single models, it's summary(model)
or joint_tests(model)
that prints the summary table or the ANOVA table, respectively.
data("diamonds")
diamonds %>%
split(.$cut) %>%
map(~ gls(price ~ x + y + z,
weights = varIdent(form = ~ 1|color),
data = .))%>%
map(joint_tests)
Error in (function (object, at, cov.reduce = mean, cov.keep = get_emm_option("cov.keep"), : Perhaps a 'data' or 'params' argument is needed
Using map(~ joint_tests)
gave a weird list like this
$Fair
function (object, by = NULL, show0df = FALSE, cov.reduce = range,
...)
{
if (!inherits(object, "emmGrid")) {
args = .zap.args(object = object, cov.reduce = cov.reduce,
..., omit = "submodel")
object = do.call(ref_grid, args)
}
facs = setdiff(names(object@levels), by)
do.test = function(these, facs, result, ...) {
if ((k <- length(these)) > 0) {
emm = emmeans(object, these, by = by, ...)
tst = test(contrast(emm, interaction = "consec"),
joint = TRUE, status = TRUE)
tst = cbind(ord = k, `model term` = paste(these,
collapse = ":"), tst)
result = rbind(result, tst)
last = max(match(these, facs))
}
else last = 0
if (last < (n <- length(facs)))
for (i in last + seq_len(n - last)) result = do.test(c(these,
facs[i]), facs, result, ...)
result
}
result = suppressMessages(do.test(character(0), facs, NULL,
...))
result = result[order(result[[1]]), -1, drop = FALSE]
if (!show0df)
result = result[result$df1 > 0, , drop = FALSE]
class(result) = c("summary_emm", "data.frame")
attr(result, "estName") = "F.ratio"
attr(result, "by.vars") = by
if (any(result$note != "")) {
msg = character(0)
if (any(result$note %in% c(" d", " d e")))
msg = .dep.msg
if (any(result$note %in% c(" e", " d e")))
msg = c(msg, .est.msg)
attr(result, "mesg") = msg
}
else result$note = NULL
result
}
<bytecode: 0x7ff68eb4a0a8>
<environment: namespace:emmeans>
$Good
function (object, by = NULL, show0df = FALSE, cov.reduce = range,
...)
{
Here is how I did joint_tests
without a list.
diamond.gls <- gls(price ~ x + y + z,
weights = varIdent(form = ~ 1|color),
data = diamonds)
joint_tests(diamond.gls)
model term df1 df2 F.ratio p.value
x 1 14311.72 4898.859 <.0001
y 1 12964.08 46.231 <.0001
z 1 8380.71 24.576 <.0001
Please see how I can fix it. Thank you.
回答1:
For reasons I will investigate, joint_tests()
needs the data
argument when it is a gls
model, at least when called within the body of a function. To overcome this, we need to write a function that fits the model and runs joint_tests()
. Here is a parallel illustration:
mod_jt = function(dat) {
mod = gls(breaks ~ tension, data = dat)
joint_tests(mod, data = dat)
}
warpbreaks %>% split(.$wool) %>% map(mod_jt)
... and we get the results:
$A
model term df1 df2 F.ratio p.value
tension 2 24 7.288 0.0034
$B
model term df1 df2 F.ratio p.value
tension 2 24 4.059 0.0303
I think the code you had will work with an lm
model, at least with the newest version of emmeans*
回答2:
We can set up dataset with examples that work:
dat = droplevels(subset(diamonds,cut %in% c("Ideal","Premium","Good")))
dat$X = cut(dat$z,c(-0.5,4,11))
dat$clarity = factor(dat$clarity,ordered=FALSE)
dat$color = factor(dat$color,ordered=FALSE)
fit = gls(price ~ X*clarity, weights = varIdent(form = ~ 1|color),
data=subset(dat,cut=="Ideal"))
joint_tests(fit)
model term df1 df2 F.ratio p.value
X 1 15002.85 12145.835 <.0001
clarity 7 11834.99 351.899 <.0001
X:clarity 7 11834.99 352.344 <.0001
So that works ok for a subset, we need to get it to work. The reason you run into the error is that joint_tests()
looks into your environment for the data.frame again, and inside map() function this is not possible.
One straight forward way is to use a for loop and store the results in a list:
fits = list()
for(i in unique(dat$cut)){
f = gls(price ~ X*clarity,
weights = varIdent(form = ~ 1|color),
data = subset(dat,cut==i))
res = joint_tests(f)
fits[[i]] = list(f=f,res=res)
}
来源:https://stackoverflow.com/questions/65282669/map-joint-tests-to-a-list-after-fitting-a-gls-model