r - Fit data with proportional dependent variable -
here's simplified version of question example data:
each year, find 40 balls in yard. proportion of them red. i'd model proportion of red balls on time.
library(tidyverse) library(modelr) # generate proportion data changes 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 model model = glm(propred ~ year, xxx_what_goes_here_xxx, data) # graph model's prediction , data tibble(year = 2000:2030) %>% modelr::add_predictions(model, "propred") %>% ggplot() + aes(y=propred, x=year) + geom_line() + geom_point(data=data)
this case can use logistic regression, using cbind(successes, failures)
option in formula interface 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)
Comments
Post a Comment