commfish / coho_known_age_study

1 stars 1 forks source link

marginal plots for predictive intervals #13

Open fssem1 opened 5 years ago

fssem1 commented 5 years ago

https://cran.r-project.org/web/packages/sjPlot/vignettes/plot_marginal_effects.html

Instead of the plot 'model_pred_binom' I think you want a marginal model plot of the different variables. Or the function marginalmodel plots here: https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4885900/ Another example: coho_scales_fulldata <- coho_scales_fulldata %>% filter(Location != "AL") coho_scales_fulldata <- coho_scales_fulldata %>% filter(Location == "HS")

binomfit <- glm((Age-1)~Q9abs+Q2, data=coho_scales_fulldata, family="binomial") newdata <- expand.grid(Q9abs=seq(min(coho_scales_bothriv$Q9abs), max(coho_scales_bothriv$Q9abs), length.out = 100), Q2=seq(min(coho_scales_bothriv$Q2), max(coho_scales_bothriv$Q2), length.out = 100))

newdata$prob = predict(binomfit, newdata, type="response")

ggplot(newdata, aes(Q9abs, Q2, fill=prob)) + geom_tile() + scale_fill_gradient2(low="red",mid="yellow",high="blue",midpoint=0.5, limits=c(0,1))

justinpriest commented 5 years ago

Hi Sara,

I'm unclear how the code above is different than what I did. I run the code, and it looks the same as the output from what I did, just with color. I purposefully did not add color because I was trying to make it publication worthy and only grayscale. If I add lines to this at the 0.005 and 0.995 probabilities, I get the same results as before:

image You could also add the following line to the ggplot that you've produced above geom_contour(aes(x=Q9abs, y=Q2, z=prob), color="black", breaks = c(0.005, 0.1, 0.5, 0.9, 0.995))

As you can see, it makes lines that are parallel to the original break. As I understand it (and I'm still kind of struggling to make exact sense of it all), since this is the "response" value, it is the probability of being a certain age (i.e., probability of being age-2). And since it is a probability (not error or confidence intervals), the contours of these would be parallel to the 50% probability.

The marginal plots from above only show one variable at a time. In my opinion, we would still want something similar to the Figure 4 plots that I've created, with both variables as the axes, and the actual ages plotted (correct/incorrect).

What do you think?

fssem1 commented 5 years ago

What you did was great, but I still think we need to include these plots.

https://cran.r-project.org/web/packages/sjPlot/vignettes/plot_marginal_effects.html

library(sjPlot) library(ggplot2) library(margins) theme_set(theme_sjplot()) binomfit <- glm((Age-1)~Location+Q2+Q9abs, data=coho_scales_bothriv, family="binomial") plot_model(binomfit, type = "pred", terms = "Q9abs") plot_model(binomfit, type = "pred", terms = "Q2") cplot(binomfit, "Q9abs") cplot(binomfit, "Q2")

justinpriest commented 5 years ago

Those cplots are really fascinating! I'll add them.