这是我的问题的简化版本,包含示例数据:
每年,我在院子里找到40个球。其中一定比例是红色的。我想随着时间的推移对红球的比例进行建模。
library(tidyverse)
library(modelr)
# generate some proportion data that changes by year
data = tibble(
year = 2011:2020,
reds = 1:10, # red balls
total = 40, # total number of balls
propRed = reds / total # proportion of red balls each year
)
# fit to a model
model = glm(propRed ~ year, XXX_WHAT_GOES_HERE_XXX, data)
# graph the model's prediction and the data
tibble(year = 2000:2030) %>%
modelr::add_predictions(model, "propRed") %>%
ggplot() +
aes(y=propRed, x=year) +
geom_line() +
geom_point(data=data)
答案 0 :(得分:3)
我们可以使用cbind(successes, failures)
的公式界面中的glm
选项来使用逻辑回归:
model <- glm(cbind(reds, total - reds) ~ year, family = 'binomial', data = data)
tibble(year = 2000:2030) %>%
mutate(propRed = predict(model, newdata = ., type = 'response')) %>%
ggplot() +
aes(y=propRed, x=year) +
geom_line() +
geom_point(data=data)