0

Good afternoon,

I want to create graphs of the predicted probabilities from a multilevel model, which I do with the code below:

jvalues1 <- with(sanctions.data.scaled, seq(from = min(lagtradeshareeu100), 
to = max(lagtradeshareeu100), length.out = 100))

pp1 <- lapply(jvalues1, function(j) {tmpdat$lagtradeshareeu100 <- j
predict(multi.sanctions.bust, newdata = tmpdat, type = "response")})

sapply(pp1[c(0, 10, 20, 30, 40, 50, 60, 70, 80)], mean)

plotdat1 <- t(sapply(pp1, function(x) {
c(M = mean(x), quantile(x, c(0.25, 0.75)))
}))

plotdat1 <- as.data.frame(cbind(plotdat1, jvalues1))
colnames(plotdat1) <- c("PredictedProbability", "Lower", "Upper", 
"LagTradeShareEU")
head(plotdat1)
ggplot(plotdat1, aes(x = LagTradeShareEU, y = PredictedProbability)) + 
geom_line(size = 2, color="red") +
ylim(c(0, .25)) + + geom_linerange(aes(ymin = Lower, ymax = Upper))

The header from plotdat1 is here:

    PredictedProbability   Lower     Upper      LagTradeShareEU
1            0.1968789 0.006504205 0.2161600       -2.017769
2            0.1948356 0.006298856 0.2107395       -1.978162
3            0.1928083 0.006099950 0.2054193       -1.938554
4            0.1907968 0.005907288 0.2001993       -1.898947
5            0.1888011 0.005720675 0.1950795       -1.859339
6            0.1868207 0.005539925 0.1900594       -1.819732

My problem is that the confidence intervals seem too large when I add them to the graph; they range from 0 to a few points above the predicted probability. I am guess that because the variables are scaled with a mean of zero that using the mean to generate the quantiles doesn't make sense? Is there another way to do it? I could have them without the confidence intervals, but I am sure that for my article they will be required.

My last question is that I'd like to make sure that the predicted probabilities are using all observed data, and not the mean of the other independent variables, but I can't be sure it is doing that so I thought someone with more of an eye for R code would help.

(edited) Picture of my plot

Keith
  • 193
  • 1
  • 10
  • To make this [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), can you edit this down to just what we need to help debug? For example, if we don't need the model itself, just the data that you get out of it, you can remove most of that code and then post a sample of the data needed for plotting. – camille Aug 06 '18 at 17:52
  • ^ I suppose that comment applies more to the first question and less to the second. I'm not totally sure what you mean there, but it's something where we would need the data – camille Aug 06 '18 at 17:54
  • Thanks, I added a sample of the data. – Keith Aug 06 '18 at 18:04
  • What are you doing to include confidence intervals on the chart? Please include the code here needed to reproduce your issue. – camille Aug 06 '18 at 18:22
  • Sorry, I took it out because it wasn't displaying properly. I put it back. – Keith Aug 06 '18 at 18:27
  • It seems like the actual question has nothing to do with ggplot, just with understanding the conf intervals from your model - I'd suggest removing ggplot from your title. – Gregor Thomas Aug 06 '18 at 18:31
  • Corrected; I apologize. – Keith Aug 06 '18 at 18:33

0 Answers0