scale_y_probit() causes geom_smooth() to fail with error: NA/NaN/Inf in foreign function call (arg 1)

170 views
Skip to first unread message

mimicmu

unread,
Sep 20, 2011, 6:44:50 AM9/20/11
to ggplot2
I am trying to plot lines of best fit to a cumulative density
I am representing the results using a reciprobit plot (log latency vs
probit cumulative probability).

grp<-c("g1","g1","g1","g1","g2","g2","g2","g2","g3","g3","g3","g3")
lat<-c(1, 4, 6, 8, 2, 3, 7, 9, 1, 4, 8, 8)

data<-data.frame(grp,lat)

d.f <- arrange(data,grp,lat) # sort data into ascending values
d.f.ecdf <- ddply(d.f, .(grp), transform, ecdf=ecdf(lat)(lat) ) #
calculate ecdf
p <- ggplot( d.f.ecdf, aes(lat, ecdf, colour = grp) )

p+geom_point()+
scale_x_log10()+
scale_y_probit()

All ok up to this point but if I add

p+scale_y_probit()+geom_smooth()
OR
p+scale_y_probit()+stat_smooth()

i get the error:
Error: NA/NaN/Inf in foreign function call (arg 1)

It works with most other distributions, for example
p+geom_point()+
scale_x_log10()+
scale_y_inverse()+
geom_smooth()

Is there any way around this issue?

xuwang

unread,
Oct 21, 2011, 5:59:50 PM10/21/11
to ggplot2
I have been getting this error also from ddply, though in a different
context.

If you find out what's going on, please let me know. Thanks!

Hadley Wickham

unread,
Oct 30, 2011, 7:18:18 PM10/30/11
to mimicmu, ggplot2
On Tue, Sep 20, 2011 at 8:44 PM, mimicmu
<insecuret...@googlemail.com> wrote:
> I am trying to plot lines of best fit to a cumulative density
> I am representing the results using a reciprobit plot (log latency vs
> probit cumulative probability).
>
> grp<-c("g1","g1","g1","g1","g2","g2","g2","g2","g3","g3","g3","g3")
> lat<-c(1, 4, 6, 8, 2, 3, 7, 9, 1, 4, 8, 8)
>
> data<-data.frame(grp,lat)
>
> d.f <- arrange(data,grp,lat)  # sort data into ascending values
> d.f.ecdf <- ddply(d.f, .(grp), transform, ecdf=ecdf(lat)(lat) )  #
> calculate ecdf
> p <- ggplot( d.f.ecdf, aes(lat, ecdf, colour = grp) )
>
> p+geom_point()+
> scale_x_log10()+
> scale_y_probit()
>
> All ok up to this point but if I add
>
> p+scale_y_probit()+geom_smooth()
> OR
> p+scale_y_probit()+stat_smooth()
>
> i get the error:
> Error: NA/NaN/Inf in foreign function call (arg 1)

There are two problems with your data.

You can see the first one by looking at the smoother on untransformed axes:

p + geom_point() + geom_smooth()

The standard errors are huge because you only have 4 points in each
line. You can fix that with:

p + geom_point() + geom_smooth(se = F)

But the plot on the probit scale still fails:

p + geom_point() + geom_smooth(se = F) + scale_y_probit()

If you look careful at the data on the probit scale you'll notice
there are a couple of points at the very top edge of the plot:

p + geom_point() + scale_y_probit()

This indicates that they're getting transformed to +infinity by the
probit scale. Removing them fixes one problem but creates another -
you only have 3 points to fit a smooth to:

ggplot(subset(d.f.ecdf, ecdf < 1), aes(lat, ecdf, colour = grp) ) +
geom_point() +
geom_smooth() +
scale_y_probit()

That's best resolved by not fitting a smooth:

ggplot(subset(d.f.ecdf, ecdf < 1), aes(lat, ecdf, colour = grp) ) +
geom_point() +
geom_line() +
scale_y_probit()

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages