绘制估计函数?

Plot estimated function?

我需要帮助解释这个(这是我坚持的作业的一部分):

"为每个主题创建一个图,在其中绘制估计函数(在 target.contrast 范围内)模型的拟合值(使用 glm)模型 correct 依赖于 target.contrast."

变量:

数据集的长度为 5,000 行。

现在回答我的问题:您认为 'estimated function' 是什么意思?随机拦截?估计值?我很困惑。

到目前为止,我已经做到了:

# modelling correct as dependent on target.contrast
model1 <- glmer(correct ~ target.contrast + (1 | subject), data = exp2_staircase, family = binomial)

plot(exp2_staircase$target.contrast, model1$fitted.values, xlab='Target contrast',
     ylab='Probability of being correct/incorrect',
     main='Logistic regression - fitted values')

示例数据

#   subject    target.contrast correct
# 1     001         0.04370244       1
# 2     002         0.04370244       1
# 3     003         0.06284581       0
# 4     004         0.14405043       1 
# 5     005         0.10045633       0
# 6     006         0.06274573       1

由于要求为每个组(此处:制造商)使用 glm,我们可以这样做:

library(tidyverse)

estimates <-
  mpg %>%
  nest(-manufacturer) %>%
  mutate(
    model = data %>% map(~  glm(displ ~ hwy, data = .x)),
    estimates = model %>% map2(data, ~ .x %>% predict(newdata = .y))
  ) %>%
  select(manufacturer, data, estimates) %>%
  unnest(estimates) %>%
  unnest(data) %>%
  mutate(displ = estimates) %>%
  distinct(manufacturer, hwy, .keep_all = TRUE)
#> Warning: All elements of `...` must be named.
#> Did you want `data = c(model, displ, year, cyl, trans, drv, cty, hwy, fl, class)`?
    
bind_rows(
  mpg %>% mutate(type = "truth"),
  estimates %>% mutate(type = "predicted")
) %>%
  filter(manufacturer %in% c("audi", "volkswagen")) %>%
  ggplot(aes(hwy, displ, color = type, shape = manufacturer)) +
    geom_point()

reprex package (v2.0.1)

于 2021-10-03 创建

如果预测是基于模型尚未发现的新组,则仅使用随机因素。