[R] Plotting Confidence Intervals into a density plot
Elysa Mitova
elysa.mitova at gmail.com
Fri Dec 2 09:18:44 CET 2016
Hi, thank you!
I've constructed the upper and lower bounds with
a <- 2.505766
s <- 0.7789832
n <- 607
error <- qnorm(0.975)*s/sqrt(n)
left <- a-error
right <- a+error
left
right
Now, I have the numbers I need, but I have no idea how to plot them. I was
thinking of using a polygon, but somehow it doesn't work out, because my
y-axis shows only density and is in itself not a variable?
xx <- data
fit1 <- density(data,na.rm=TRUE)
fit2 <- replicate(10000, { x <- sample(xx, replace=TRUE);
density(x, na.rm=TRUE, from=min(fit1$x), to=max(fit1$x))$y } )
fit3 <- apply(fit2, 1, quantile, c(0.025,0.975) ) - Probably herein
lies the problem?
plot(fit1, ylim=range(fit3))
polygon( c(fit1$x, rev(fit1$x)), c(fit3[1,], rev(fit3[2,])),
col='grey', border=F)
lines(fit1)
I tried working with this solution I found on the internet, but
somehow now the lines the shaded areas sporadically everywhere around
my density plot? I just want a polygon spreading from 2.44 to 2.57
along the x-axis.
Any tipps?
On Fri, Dec 2, 2016 at 1:24 AM, David Winsemius <dwinsemius at comcast.net>
wrote:
>
> > On Dec 1, 2016, at 12:10 PM, Elysa Mitova <elysa.mitova at gmail.com>
> wrote:
> >
> > Hi,
> >
> > I am desperately looking for a way to plot confidence intervals into a
> > density plot of only one variable (not a scatter plot etc.)
> >
> > Have you any advice how to do this?
> >
> > I've only found manual ways to do with "abline", but this is a rather
> > bothersome method and only works with ggplot (and not ggplot2).
>
> This makes it appear that you expect this to be done in ggplot2
> automagically. I suspect you must instead first find the right approach to
> construction of those upper and lower bounds before plotting. It's not
> clear what methods you expect to be needed. Your desperation is not a
> guide. Perhaps trying a bit of searching?
>
> install.packages("sos")
> library(sos)
> findFn("confidence intervals density estimates")
>
>
> Delivers quite a few results. Then searching on the text within that
> webpage you find
>
>
> 208 2 27 54 nprobust kdrobust 2016-11-14
> 16:41:50 27 Kernel Density Estimation with Robust Confidence
> Intervals
> 209 2 27 54 nprobust lprobust 2016-11-14
> 16:41:50 27 Local-Polynomial Estimation with Robust Confidence
> Intervals
>
> Is that what you seek?
>
> >
> > Thank you!
> >
> > [[alternative HTML version deleted]]
> I know you just subscribed, so now is the time to read the Posing Guide.
>
> ==
>
> David Winsemius
> Alameda, CA, USA
>
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list